|
| 1 | +--- |
| 2 | +title: Realtime Agents on Cloudflare |
| 3 | +description: Connect your Agents SDK agents from Cloudflare Workers/workerd using a dedicated transport. |
| 4 | +--- |
| 5 | + |
| 6 | +import { Aside, Steps, Code } from '@astrojs/starlight/components'; |
| 7 | +import cloudflareBasicExample from '../../../../../examples/docs/extensions/cloudflare-basic.ts?raw'; |
| 8 | + |
| 9 | +Cloudflare Workers and other workerd runtimes cannot open outbound WebSockets using the global |
| 10 | +`WebSocket` constructor. To simplify connecting Realtime Agents from these environments, the |
| 11 | +extensions package provides a dedicated transport that performs the `fetch()`-based upgrade |
| 12 | +internally. |
| 13 | + |
| 14 | +<Aside type="caution"> |
| 15 | + This adapter is still in beta. You may run into edge case issues or bugs. |
| 16 | + Please report any issues via [GitHub |
| 17 | + issues](https://github.com/openai/openai-agents-js/issues) and we'll fix |
| 18 | + quickly. For Node.js-style APIs in Workers, consider enabling `nodejs_compat`. |
| 19 | +</Aside> |
| 20 | + |
| 21 | +## Setup |
| 22 | + |
| 23 | +<Steps> |
| 24 | + |
| 25 | +1. **Install the extensions package.** |
| 26 | + |
| 27 | + ```bash |
| 28 | + npm install @openai/agents-extensions |
| 29 | + ``` |
| 30 | + |
| 31 | +2. **Create a transport and attach it to your session.** |
| 32 | + |
| 33 | + <Code lang="typescript" code={cloudflareBasicExample} /> |
| 34 | + |
| 35 | +3. **Connect your `RealtimeSession`.** |
| 36 | + |
| 37 | + ```typescript |
| 38 | + await session.connect({ apiKey: 'your-openai-ephemeral-or-server-key' }); |
| 39 | + ``` |
| 40 | + |
| 41 | +</Steps> |
| 42 | + |
| 43 | +## Notes |
| 44 | + |
| 45 | +- The Cloudflare transport uses `fetch()` with `Upgrade: websocket` under the hood and skips |
| 46 | + waiting for a socket `open` event, matching the workerd APIs. |
| 47 | +- All `RealtimeSession` features (tools, guardrails, etc.) work as usual when using this transport. |
| 48 | +- Use `DEBUG=openai-agents*` to inspect detailed logs during development. |
0 commit comments