Skip to content

Commit 15e17b2

Browse files
committed
auth
1 parent 68e0b4b commit 15e17b2

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

src/content/docs/agents/api-reference/calling-agents.mdx

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ The `routeAgentRequest` helper documented earlier in this guide exposes two usef
115115
<TypeScriptExample>
116116

117117
```ts
118-
import { Agent, AgentNamespace, getAgentByName, routeAgentRequest } from 'agents-sdk';
118+
import { Agent, AgentNamespace, routeAgentRequest } from 'agents-sdk';
119119

120120
interface Env {
121121
MyAgent: AgentNamespace<MyAgent>;
@@ -152,6 +152,39 @@ export default {
152152

153153
If you are using `getAgentByName` or the underlying Durable Objects routing API, you should authenticate incoming requests or WebSocket connections before calling `getAgentByName`.
154154

155+
For example, if you are using [Hono](https://hono.dev/), you can authenticate in the middleware before calling an Agent and passing a request (or a WebSocket connection) to it:
156+
157+
<TypeScriptExample>
158+
159+
```ts
160+
import { Agent, AgentNamespace, getAgentByName } from 'agents-sdk';
161+
import { Hono } from 'hono';
162+
163+
const app = new Hono<{ Bindings: Env }>();
164+
165+
app.use('/code-review/*', async (c, next) => {
166+
// Perform auth here
167+
// e.g. validate a Bearer token, a JWT, use your preferred auth library
168+
// return Response.json({ msg: 'unauthorized' }, { status: 401 });
169+
await next(); // continue on if valid
170+
});
171+
172+
app.get('/code-review/:id', async (c) => {
173+
const id = c.req.param('teamId');
174+
if (!id) return Response.json({ msg: 'missing id' }, { status: 400 });
175+
176+
// Call the Agent, creating it with the name/identifier from the ":id" segment
177+
// of our URL
178+
const agent = await getAgentByName<Env, MyAgent>(c.env.MyAgent, id);
179+
180+
// Pass the request to our Agent instance
181+
return await agent.fetch(c.req.raw);
182+
});
183+
```
184+
185+
</TypeScriptExample>
186+
187+
155188
### Next steps
156189

157190
* Review the [API documentation](/agents/api-reference/sdk/) for the Agents class to learn how to define

0 commit comments

Comments
 (0)