Workers RPC, Service Bindings and Durable Object Methods
How Cloudflare Workers RPC, service bindings, and Durable Object stubs let you call methods across Workers without public HTTP.
This page summarizes Cloudflare Workers documentation on Workers RPC: calling public methods on another Worker or Durable Object through bindings on the same account, usually via Service bindings, instead of routing traffic over the public Internet.
Workers RPC: what it is and when you need it
Section titled “Workers RPC: what it is and when you need it”Workers RPC is a built-in, JavaScript-native remote procedure call system. You expose public methods on a WorkerEntrypoint subclass or on a DurableObject subclass; a caller Worker invokes those methods on a binding as if they lived in the same script.
To use RPC, define a compatibility date of 2024-04-03 or higher, or include rpc in your compatibility flags.
— Cloudflare, Remote-procedure call (RPC)
Workers provide a built-in, JavaScript-native RPC (Remote Procedure Call) system, allowing you to: Define public methods on your Worker that can be called by other Workers on the same Cloudflare account, via Service Bindings; Define public methods on Durable Objects that can be called by other workers on the same Cloudflare account that declare a binding to it.
— Cloudflare, Remote-procedure call (RPC)
You typically reach for RPC when one Worker (or a Pages Function) should call another service without a public URL, custom auth between services, or hand-built HTTP routing. For true HTTP request/response flows, or legacy compatibility dates before 2024-04-03, fetch() on the binding or Durable Object stub remains appropriate (HTTP service bindings, invoke methods).
| Approach | Transport | Best for |
|---|---|---|
| RPC method call | Cap’n Proto–based internal RPC | Typed methods, shared logic, Durable Object state updates |
env.BINDING.fetch(request) |
HTTP semantics on the binding | Proxies, existing Request/Response pipelines |
Public fetch() to workers.dev |
Internet | Third parties or when Service bindings are not configured |
Service bindings: configure Wrangler and call another Worker
Section titled “Service bindings: configure Wrangler and call another Worker”A Service binding lets Worker A call Worker B (or forward a Request) without a publicly accessible URL. Configuration lives on the caller’s Wrangler file.
A service binding allows you to send HTTP requests to another Worker without those requests going over the Internet. The request immediately invokes the downstream Worker, reducing latency as compared to a request to a third-party service.
— Cloudflare, Service bindings
Service bindings enable you to call a Worker from within your Pages Function.
— Cloudflare, Bindings
How to add a Service binding in Wrangler
Section titled “How to add a Service binding in Wrangler”| Field | Meaning |
|---|---|
binding |
Name on env (for example WORKER_B) |
service |
Target Worker name; use worker-name-environment for a specific environment |
entrypoint |
Optional named WorkerEntrypoint class; omit to use the default export |
Example from RPC (WorkerEntrypoint):
{ "services": [ { "binding": "WORKER_B", "service": "worker-b" } ]}For RPC, the callee extends WorkerEntrypoint and declares public methods. The caller awaits env.WORKER_B.add(1, 2). TypeScript projects can run npx wrangler types with both Workers’ config files so WORKER_B is typed as a Service with those methods (Remote-procedure call (RPC)).
Service bindings vs public fetch() between Workers
Section titled “Service bindings vs public fetch() between Workers”When calling Workflows or other Workers from Pages, Cloudflare recommends Service bindings for Workers in your own account:
We recommend using Service Bindings when calling a Worker in your own account. Service Bindings don’t require you to expose a public endpoint from your Worker, don’t require you to configure authentication, and allow you to call methods on your Worker directly, avoiding the overhead of managing HTTP requests and responses.
— Cloudflare, Call Workflows from Pages
Use Service Bindings to send requests from one Worker to another on your account without going over the Internet.
— Cloudflare, Limits
About Service bindings: Service bindings are fast. When you use Service Bindings, there is zero overhead or added latency. By default, both Workers run on the same thread of the same Cloudflare server.
— Cloudflare, About Service bindings
Common mistakes: deploying the caller before the callee exists (binding validation fails); forgetting to await RPC calls (the callee may terminate early) (Service bindings lifecycle); assuming Cloudflare Access ctx.access propagates to the downstream Worker—it does not.
Named entrypoints and ctx.props
Section titled “Named entrypoints and ctx.props”One Worker can export several WorkerEntrypoint classes (admin vs user APIs) and bind with entrypoint (RPC (WorkerEntrypoint)). Static props in Wrangler become this.ctx.props on the callee—useful for permissions or scoping a binding to one resource (Context (ctx)). The same Worker can also build custom bindings via ctx.exports and dynamic props when the caller is trusted (Context (ctx)).
Durable Object RPC: stubs, methods, and fetch()
Section titled “Durable Object RPC: stubs, methods, and fetch()”Durable Objects do not accept traffic directly from the Internet; a Worker with a binding creates a stub and calls methods on it (Getting started).
By writing a Durable Object class which inherits from the built-in type DurableObject, public methods are exposed as RPC methods, which developers can call using a DurableObjectStub from a Worker.
— Cloudflare, Agent class internals
All new projects and existing projects with a compatibility date greater than or equal to 2024-04-03 should prefer to invoke Remote Procedure Call (RPC) methods defined on a Durable Object class.
— Cloudflare, Invoke methods
Creating a stub with getByName() does not start the object; the lifecycle begins when you invoke a method on the stub (Lifecycle of a Durable Object).
The Build a counter example shows increment(), decrement(), and getCounterValue() called on env.COUNTERS.getByName(name). Read-modify-write in the object is safe because input gates serialize concurrent RPC to the same instance.
Durable Objects can take a Request from a Worker and send a Response back. This can only be done through the fetch method (which the developer must implement).
— Cloudflare, Agent class internals
Durable Objects support RPC calls as of compatibility date 2024-04-03. RPC methods are preferred over fetch() when your application does not follow HTTP request/response flow.
— Cloudflare, Durable Object Base Class
On a stub, fetch() follows the Fetch API, not arbitrary RPC semantics—redirect handling and client-side Request properties behave like global fetch() (Reserved Methods). Method names such as dup, constructor, and (on DurableObject / WorkerEntrypoint) alarm and WebSocket handlers are reserved (Reserved Methods).
Durable Object stub ordering and failures
Section titled “Durable Object stub ordering and failures”When you make multiple calls to the same Durable Object, it is guaranteed that the calls will be delivered to the remote Durable Object in the order in which you made them.
— Cloudflare, Durable Object Stub
If an exception is thrown by a Durable Object stub all in-flight calls and future calls will fail with exceptions. To continue invoking methods on a remote Durable Object a Worker must recreate the stub.
— Cloudflare, Durable Object Stub
RpcTarget, return values, and Agent @callable
Section titled “RpcTarget, return values, and Agent @callable”Custom class instances sent over RPC must extend RpcTarget; plain application classes are not cloneable over RPC (Remote-procedure call (RPC)). Functions and RpcTarget instances become stubs on the receiver; the implementation still runs in the provider isolate (Remote-procedure call (RPC)).
For Agents, external browser clients use @callable over WebSocket; internal Worker-to-Agent traffic should use Durable Object RPC:
| RPC type | Transport | Use case |
|---|---|---|
@callable |
WebSocket | External clients (browsers, apps) |
| Durable Object RPC | Internal | Worker to Agent, Agent to Agent |
Durable Object RPC is more efficient for internal calls since it does not go through WebSocket serialization.
— Cloudflare, Callable methods
Capability patterns for Workers for Platforms pass an RPC stub through ctx.props so user code can call limited methods without seeing platform secrets; stubs received that way are valid only for the current request (Dynamic dispatch Worker).
Cross-language RPC (Python and JavaScript Workers)
Section titled “Cross-language RPC (Python and JavaScript Workers)”Python and JavaScript Workers can call each other’s public methods through Service bindings:
You can now call methods between Python and JavaScript Workers using Workers RPC. This works through Service bindings without extra dependencies, schema definitions, or serialization code. Cross-language RPC calls behave like ordinary function calls.
— Cloudflare, Python and JavaScript Workers can now call each other via RPC
Point the binding at a named entrypoint when the TypeScript side exports a class such as RpcService (Changelog).
RPC lifecycle: using, disposal, and limits
Section titled “RPC lifecycle: using, disposal, and limits”RPC stubs keep server-side RpcTarget instances alive until the client disposes them. Prefer Explicit Resource Management:
When you obtain a stub, you can ensure that it is automatically disposed when you exit scope it was created in:
using user = await env.USER_SERVICE.findUser(id);— Cloudflare, Lifecycle
Stubs are often disposed automatically at the end of a fetch() handler’s execution context; returned object values get disposers that release nested stubs—store RPC results in using when the callee might return objects (Lifecycle). Use stub.dup() when one consumer disposes a stub you still need (Lifecycle).
Limits and caveats:
- Maximum serialized RPC payload 32 MiB; prefer
ReadableStreamfor large data (Remote-procedure call (RPC)). - Smart Placement is ignored for RPC: the callee may run locally to the caller even when placement is enabled (Remote-procedure call (RPC)).
- With the
rpccompatibility flag, bindings appear to expose every method name—code that auto-detects binding types by enumerating methods may mis-detect Service bindings (Compatibility flags).
RPC errors and visibility between Workers
Section titled “RPC errors and visibility between Workers”Thrown errors propagate to the caller; with enhanced_error_serialization, name, message, and serializable own properties (including cause) may be preserved—do not rely on instanceof custom error classes across the boundary (Error handling).
The RPC security model is capability-based: each side only invokes stubs it received (Visibility and Security Model). Arrow functions on WorkerEntrypoint are not exposed; use prototype methods. Instance fields on RpcTarget / entrypoint classes are not directly visible unless exposed via getters (Visibility and Security Model).
How to check this on your site
Section titled “How to check this on your site”- Confirm each Worker uses compatibility date 2024-04-03 or later (or the
rpcflag) per Remote-procedure call (RPC) and Compatibility flags. - Deploy the callee Worker first, then the caller, matching Service bindings deployment order.
- Run local multi-Worker dev with both Wrangler configs (for example
npx wrangler dev -c ./worker-a/wrangler.jsonc -c ./worker-b/wrangler.jsoncfrom Remote-procedure call (RPC)) and verify RPC methods return expected values. - For Durable Objects, hit routes that call stub methods (not only
getByName) and confirm state updates match Build a counter. - You can check automatically in Answerwell (our tool): https://answerwell.org/workspace
Report an error or ask a question
Section titled “Report an error or ask a question”If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact statement you are asking about.