Every PartyServer application needs a way to direct incoming HTTP and WebSocket requests to the right Durable Object instance. TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/cloudflare/partykit/llms.txt
Use this file to discover all available pages before exploring further.
routePartykitRequest function handles that mapping automatically — it parses the URL, finds the matching DO namespace in your env, and forwards the request, including performing the WebSocket upgrade. You configure hooks to intercept requests before they arrive at the server, making routing the natural place to enforce authentication, rewrite headers, or apply data-residency rules.
How URL matching works
routePartykitRequest expects URLs of the form:
prefix— defaults to"parties". Change it with theprefixoption.:server— matched case-insensitively against the kebab-case form of a DO binding name inenv(e.g. the bindingMyServermatches the segmentmy-server).:name— the room or instance name passed toidFromName().
routePartykitRequest returns null, so you can fall through to other handlers.
Basic setup
wrangler.jsonc to declare the binding and migration:
/parties/my-server/room-42 will land in MyServer with this.name === "room-42".
Custom prefix
Setprefix to mount your servers at a different path:
"api/v1/parties".
Intercepting requests with hooks
routePartykitRequest exposes two hook options that run before the request reaches the Durable Object:
| Hook | Fires for |
|---|---|
onBeforeConnect | WebSocket upgrade requests |
onBeforeRequest | Plain HTTP requests |
Request and a lobby object with className (the DO binding name) and name (the room name). They can return:
- A
Requestto substitute a modified request. - A
Responseto short-circuit the routing entirely (e.g. return401). undefined/voidto pass the request through unchanged.
Example: auth checking in onBeforeConnect
Auth must be enforced here — once the WebSocket upgrade completes the
connection is established and cannot be rejected from inside
onConnect.Directly addressing a server with getServerByName
When you need a stub to a specific DO instance outside of the normal routing path — for example, to send an RPC from one server to another — use getServerByName:
getServerByName resolves the name via idFromName, awaits onStart() on the target before returning the stub, and supports the same locationHint, jurisdiction, and routingRetry options as routePartykitRequest.
Geographic routing and data residency
locationHint
Suggests a Cloudflare region for the Durable Object. Best-effort, not
guaranteed. Values:
"wnam", "enam", "sam", "weur", "eeur",
"apac", "oc", "afr", "me".jurisdiction
Hard constraint that restricts a DO to a legal jurisdiction, such as
"eu"
for GDPR compliance. Cannot be combined with per-location weights.Retry options for transient errors
Durable Object routing can occasionally fail with transient infrastructure errors.routingRetry (enabled by default with 3 attempts and jittered exponential backoff) handles these automatically. Only errors with retryable === true are retried; overloaded errors are never retried.
routingRetry: false to disable retries entirely.
CORS support
Enable CORS for all matched routes by passingcors: true for permissive defaults, or supply explicit headers for credentialed requests:
OPTIONS requests are handled automatically for matched routes.