Checking Caller Identity at the Cloudflare Agent Route Entry
A checklist for Worker-level authentication and per-instance authorization before requests reach an Agent, from the official Authentication page.
Prerequisites and input · Engineering checks · 1. List every entry that reaches an Agent
At a glance
Anyone who knows an Agent's URL can send it requests, so identity checks belong at the Worker routing entry, before the request reaches the Durable Object; clients supply the credentials, and the Agent object can further limit connection rights. This guide splits HTTP and WebSocket entries, credential location, instance ownership and trusted headers into separate checks. The only basis is the Authentication page; its example authentication function is a placeholder and must not be mistaken for a real verification implementation.
Runtime and applicability
- Article type
- Engineering practice
- Verified
- Applies to
- Cloudflare Agents SDK Checking Caller Identity at the Cloudflare Agent Route Entry
Execution locations and responsibilities
- Worker routing entry authenticates and authorizes instances; clients supply credentials and the Agent manages connection rights Use the workflow below to distinguish responsibilities of the caller, runtime, and external actions.
Arrows show the order of checks in the guide. Follow each step’s prerequisites, branches and stop conditions; unexecuted steps are not completed work.
When to use
routeAgentRequest()runsonBeforeConnectbefore forwarding a WebSocket upgrade andonBeforeRequestbefore forwarding an HTTP request; both run in the Worker, ahead of the Durable Object. A hook can return aResponseto reject, return a newRequestto replace the original, or return nothing to forward unchanged. Therouteargument carriesclassNameand the object'sname.
When not to use
- The page's
authenticateexample only checks that a header exists and returns a fixed identifier; real session, JWT or API key verification must be implemented separately, and this guide supplies no verification algorithm - Passing the hook only means the request is let through; limits on state changes and method calls inside the object belong to a different layer such as WebSocket connection rights, and route checks do not replace them
- Taking the name from the fourth path segment in the pre-check depends on the
/agents/:binding/:nameshape; recheck it if the path format changes or other routes are used - A token in a query parameter may appear in logs and history; the page does not discuss this risk, so this guide makes no claim about it
Prerequisites and input
- A Worker that forwards requests with
routeAgentRequest(), and a list of the paths that can reach an Agent - A decided source of user identity, such as a session cookie, JWT or API key, and a rule mapping users to Agent instance names
Engineering checks
Entry layer: hooks and forwarding in the Worker — routeAgentRequest() runs onBeforeConnect before forwarding a WebSocket upgrade and onBeforeRequest before forwarding an HTTP request; both run in the Worker, ahead of the Durable Object. A hook can return a Response to reject, return a new Request to replace the original, or return nothing to forward unchanged. The route argument carries className and the object's name.
Authorization layer: identity and instance — Authentication answers who is calling; you still have to decide which instance they may reach. The page's example compares the user identifier with route.name, returning 401 when unauthenticated and 403 when the name does not match. Alternatively the server can pick the object name from the session so the client never names it; see the Custom routing page for that.
Trust layer: headers and connection rights — A header added in the hook (the example uses x-user-id) is trustworthy only if clients cannot reach the object any other way, so every request to the object must pass the same check. Inside the object (the Agent side), the WebSockets capability can also make a connection readonly, so viewers receive state but cannot change it.
1. List every entry that reaches an Agent
Where: the Worker's fetch handler. Input: the existing routes and paths. Action: separate HTTP requests, WebSocket upgrades and routes that routeAgentRequest() does not handle. Completion check: each entry names the check that covers it. Stop condition: a path reaches the object without passing any check; close it before continuing.
2. Pick where the check runs
Where: the Worker. Input: the entry list. Action: for requests forwarded by routeAgentRequest(), configure the same check as both onBeforeConnect and onBeforeRequest; for other routes, check before calling routeAgentRequest(). Completion check: both kinds of request use the same decision logic. Stop condition: only one of the two hooks is configured; add the other.
3. Fix the WebSocket credential source
Where: the connected clients and onBeforeConnect in the Worker. Input: the credentials each client type already holds. Action: the page says browsers cannot set an Authorization header on a WebSocket upgrade, so browser clients should pass a token through a cookie or query parameter that onBeforeConnect reads; useAgent and AgentClient accept a query option. The page states no such limit for non-browser clients, so confirm and record how each of those transports its credential. Completion check: every client type has a named credential location. Stop condition: a browser client's credential can only travel in the Authorization header; change the location rather than assume it will work.
4. Verify the user and compare the instance name
Where: inside the hook in the Worker. Input: the request and route. Action: genuinely verify the session, JWT or API key first and return 401 on failure; then compare route.name with the instance name derived from the user, returning 403 on mismatch. In the pre-check variant, the name is the fourth segment of the path /agents/:binding/:name. Completion check: the conditions for 401 and 403 are written separately. Stop condition: if the authentication function is still the page's placeholder, do not ship.
5. Decide whether the server picks the name
Where: routing design. Input: the instance name clients currently send. Action: evaluate whether the server can choose the object from the session so clients no longer name it; the details belong to Custom routing, which must be read separately. Completion check: a conclusion with its reason is recorded. Stop condition: do not design the selection logic without reading the Custom routing page.
6. Verify trusted headers and connection rights
Where: the Worker and the Agent object. Input: the headers the hook adds and the user roles. Action: confirm the object trusts only hook-added headers and that no other path reaches it; for view-only roles, evaluate readonly connections. Completion check: every path to the object passes the check and each role's rights are listed. Stop condition: an unchecked entry exists, so do not rely on that header.
Task example
This is a synthetic walkthrough, not an actual run. Suppose a client with identifier user-a and valid credentials requests the instance named user-a under /agents/. Expected: the hook accepts the credentials, the names match, a header is added and the request is forwarded. The same user requesting the instance named user-b should get 403, and a request with no credentials should get 401. Then picture a browser WebSocket passing a token through the query option and onBeforeConnect reading it from the query parameter. These outcomes are projections and need confirming with real requests in a real environment.
Completion checks
- Every path that reaches the object is listed and mapped to a hook or a pre-check
onBeforeConnectandonBeforeRequestshare the same logic, and the 401 and 403 conditions are distinct- Browser WebSocket clients supply credentials via cookie or query parameter, not an
Authorizationheader, and the credential location of other client types is recorded separately - The instance-name comparison uses the verified user identity, not a value the client claims
- The no-other-path premise for trusted headers has been checked and readonly connections have been assessed
Verification scope
This guide comes from reading the official Authentication page (updated 29 October 2026). Installation, login, API execution and output were not tested, and every observation in the example is hypothetical.
Sources
All links are official pages; verification dates are listed per source, and links point to the version checked.