---
description: Build a Worker gateway that routes and load balances across multiple private VPC Services.
title: Route to private services from Workers
image: https://developers.cloudflare.com/og-docs.png
---

[Skip to content](#main-content)

> Documentation Index  
> Fetch the complete documentation index at: https://developers.cloudflare.com/workers-vpc/llms.txt  
> Use this file to discover all available pages before exploring further.

# Route to private services from Workers

Last updated Aug 24, 2026|Copy as Markdown|[View as Markdown](https://docs-durable-objects-instance-replaced-errors.previews.developers.cloudflare.com/workers-vpc/examples/route-across-private-services/index.md)|[Agent setup](https://docs-durable-objects-instance-replaced-errors.previews.developers.cloudflare.com/agent-setup/)

This example shows how to use Workers VPC to create a centralized gateway that routes requests based on URL paths, provides authentication and rate limiting, and load balances across internal services.

## Prerequisites

* Multiple private APIs or services running in your VPC/virtual network (we'll use a user service and orders service)
* Cloudflare Tunnel configured and running (follow the [Get Started guide](https://docs-durable-objects-instance-replaced-errors.previews.developers.cloudflare.com/workers-vpc/get-started/#2-set-up-cloudflare-tunnel) to set up or [create a tunnel from the dashboard ↗](https://dash.cloudflare.com/?to=/:account/workers/vpc/tunnels))
* Workers account with Workers VPC access

## 1\. Create the VPC Services

First, create services for your internal APIs using hostnames:

```bash
# Create user service
npx wrangler vpc service create user-service \
  --type http \
  --tunnel-id <YOUR_TUNNEL_ID> \
  --hostname user-api.internal.example.com

# Create orders service
npx wrangler vpc service create order-service \
  --type http \
  --tunnel-id <YOUR_TUNNEL_ID> \
  --hostname orders-api.internal.example.com
```

Note the service IDs returned for the next step.

## 2\. Configure your Worker

Update your Wrangler configuration file:

```jsonc
{
	"$schema": "./node_modules/wrangler/config-schema.json",
	"name": "api-gateway",
	"main": "src/index.js",
	// Set this to today's date
	"compatibility_date": "2026-08-24",
	"vpc_services": [
		{
			"binding": "USER_SERVICE",
			"service_id": "<YOUR_USER_SERVICE_ID>"
		},
		{
			"binding": "ORDER_SERVICE",
			"service_id": "<YOUR_ORDER_SERVICE_ID>"
		}
	]
}
```

```toml
"$schema" = "./node_modules/wrangler/config-schema.json"
name = "api-gateway"
main = "src/index.js"
# Set this to today's date
compatibility_date = "2026-08-24"

[[vpc_services]]
binding = "USER_SERVICE"
service_id = "<YOUR_USER_SERVICE_ID>"

[[vpc_services]]
binding = "ORDER_SERVICE"
service_id = "<YOUR_ORDER_SERVICE_ID>"
```

## 3\. Implement the Worker

In your Workers code, use the VPC Service bindings to route requests to the appropriate services:

```js
export default {
	async fetch(request, env, ctx) {
		const url = new URL(request.url);

		// Route to internal services
		if (url.pathname.startsWith('/api/users')) {
			const response = await env.USER_SERVICE.fetch("https://user-api.internal.example.com" + url.pathname);
			return response;
		} else if (url.pathname.startsWith('/api/orders')) {
			const response = await env.ORDER_SERVICE.fetch("https://orders-api.internal.example.com" + url.pathname);
			return response;
		}

		return new Response('Not Found', { status: 404 });
	},
};
```

## 4\. Deploy and test

Now, you can deploy and test your Worker:

```bash
npx wrangler deploy
```

```bash
# Test user service requests
curl https://api-gateway.workers.dev/api/users

# Test orders service requests
curl https://api-gateway.workers.dev/api/orders
```

## Next steps

* Add [authentication and authorization](https://docs-durable-objects-instance-replaced-errors.previews.developers.cloudflare.com/workers/examples/auth-with-headers/)
* Implement [rate limiting](https://docs-durable-objects-instance-replaced-errors.previews.developers.cloudflare.com/durable-objects/api/)
* Set up [monitoring and alerting](https://docs-durable-objects-instance-replaced-errors.previews.developers.cloudflare.com/analytics/analytics-engine/)
* Explore [other examples](https://docs-durable-objects-instance-replaced-errors.previews.developers.cloudflare.com/workers-vpc/examples/)

Was this helpful?

YesNo

## On this page

[![](https://docs-durable-objects-instance-replaced-errors.previews.developers.cloudflare.com/_astro/logo.te5VL_aD.svg)Docs](https://docs-durable-objects-instance-replaced-errors.previews.developers.cloudflare.com/)

```json
{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/workers-vpc/examples/route-across-private-services/#page","headline":"Route to private services from Workers · Cloudflare Workers VPC","description":"Build a Worker gateway that routes and load balances across multiple private VPC Services.","url":"https://developers.cloudflare.com/workers-vpc/examples/route-across-private-services/","inLanguage":"en","image":"https://developers.cloudflare.com/og-docs.png","dateModified":"2026-08-24","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"}}
```
