Skip to content
Cloudflare Docs

Workers Binding API

VPC Service bindings provide a convenient API for accessing VPC Services from your Worker. Each binding represents a connection to a service in your private network through a Cloudflare Tunnel.

Each request made on the binding will route to the specific service that was configured for the VPC Service, while restricting access to the rest of your private network.

VPC Service binding

A VPC Service binding is accessed via the env parameter in your Worker's fetch handler. It provides a fetch() method for making HTTP requests to your private service.

fetch()

Makes an HTTP request to the private service through the configured tunnel.

JavaScript
const response = await env.VPC_SERVICE_BINDING.fetch(resource, options);

Parameters

  • resource (string | URL | Request) - The URL to fetch. This must be an absolute URL including protocol, host, and path (for example, http://internal-api/api/users)
  • options (optional RequestInit) - Standard fetch options including:
    • method - HTTP method (GET, POST, PUT, DELETE, etc.)
    • headers - Request headers
    • body - Request body
    • signal - AbortSignal for request cancellation

Return value

Returns a Promise<Response> that resolves to a standard Fetch API Response object.

Examples

Basic GET request

JavaScript
export default {
async fetch(request, env) {
const privateRequest = new Request(
"http://internal-api.company.local/users",
);
const response = await env.VPC_SERVICE_BINDING.fetch(privateRequest);
const users = await response.json();
return new Response(JSON.stringify(users), {
headers: { "Content-Type": "application/json" },
});
},
};

POST request with body

JavaScript
export default {
async fetch(request, env) {
const privateRequest = new Request(
"http://internal-api.company.local/users",
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${env.API_TOKEN}`,
},
body: JSON.stringify({
name: "John Doe",
email: "john@example.com",
}),
},
);
const response = await env.VPC_SERVICE_BINDING.fetch(privateRequest);
if (!response.ok) {
return new Response("Failed to create user", { status: response.status });
}
const user = await response.json();
return new Response(JSON.stringify(user), {
headers: { "Content-Type": "application/json" },
});
},
};

Request with HTTPS and IP address

JavaScript
export default {
async fetch(request, env) {
const privateRequest = new Request("https://10.0.1.50/api/data");
const response = await env.VPC_SERVICE_BINDING.fetch(privateRequest);
return response;
},
};

Next steps