|
1 | 1 | --- |
2 | | -title: AI Gateway launches Realtime WebSockets API |
3 | | -description: AI Gateway now supports end-to-end, client-to-provider WebSockets |
| 2 | +title: AI Gateway launches Realtime WebSockets API |
| 3 | +description: AI Gateway now supports end-to-end, client-to-provider WebSockets |
4 | 4 | date: 2025-03-21T09:00:00Z |
5 | 5 | --- |
6 | | -We are excited to announce that [AI Gateway](/ai-gateway/) now supports real-time AI interactions with the new [Realtime WebSockets API](/ai-gateway/configuration/websockets-api/realtime-api/). |
| 6 | + |
| 7 | +We are excited to announce that [AI Gateway](/ai-gateway/) now supports real-time AI interactions with the new [Realtime WebSockets API](/ai-gateway/websockets-api/realtime-api/). |
7 | 8 |
|
8 | 9 | This new capability allows developers to establish persistent, low-latency connections between their applications and AI models, enabling natural, real-time conversational AI experiences, including speech-to-speech interactions. |
9 | 10 |
|
10 | 11 | The Realtime WebSockets API works with the [OpenAI Realtime API](https://platform.openai.com/docs/guides/realtime#connect-with-websockets), [Google Gemini Live API](https://ai.google.dev/gemini-api/docs/multimodal-live), and supports real-time text and speech interactions with models from [Cartesia](https://docs.cartesia.ai/api-reference/tts/tts), and [ElevenLabs](https://elevenlabs.io/docs/conversational-ai/api-reference/conversational-ai/websocket). |
11 | 12 |
|
12 | 13 | Here's how you can connect AI Gateway to [OpenAI's Realtime API](https://platform.openai.com/docs/guides/realtime#connect-with-websockets) using WebSockets: |
| 14 | + |
13 | 15 | ```javascript title="OpenAI Realtime API example" |
14 | 16 | import WebSocket from "ws"; |
15 | 17 |
|
16 | 18 | const url = |
17 | | - "wss://gateway.ai.cloudflare.com/v1/<account_id>/<gateway>/openai?model=gpt-4o-realtime-preview-2024-12-17"; |
| 19 | + "wss://gateway.ai.cloudflare.com/v1/<account_id>/<gateway>/openai?model=gpt-4o-realtime-preview-2024-12-17"; |
18 | 20 | const ws = new WebSocket(url, { |
19 | | - headers: { |
20 | | - "cf-aig-authorization": process.env.CLOUDFLARE_API_KEY, |
21 | | - Authorization: "Bearer " + process.env.OPENAI_API_KEY, |
22 | | - "OpenAI-Beta": "realtime=v1", |
23 | | - }, |
| 21 | + headers: { |
| 22 | + "cf-aig-authorization": process.env.CLOUDFLARE_API_KEY, |
| 23 | + Authorization: "Bearer " + process.env.OPENAI_API_KEY, |
| 24 | + "OpenAI-Beta": "realtime=v1", |
| 25 | + }, |
24 | 26 | }); |
25 | 27 |
|
26 | 28 | ws.on("open", () => console.log("Connected to server.")); |
27 | 29 | ws.on("message", (message) => console.log(JSON.parse(message.toString()))); |
28 | 30 |
|
29 | 31 | ws.send( |
30 | | - JSON.stringify({ |
31 | | - type: "response.create", |
32 | | - response: { modalities: ["text"], instructions: "Tell me a joke" }, |
33 | | - }), |
| 32 | + JSON.stringify({ |
| 33 | + type: "response.create", |
| 34 | + response: { modalities: ["text"], instructions: "Tell me a joke" }, |
| 35 | + }), |
34 | 36 | ); |
35 | 37 | ``` |
36 | 38 |
|
37 | | -Get started by checking out the [Realtime WebSockets API](/ai-gateway/configuration/websockets-api/realtime-api/) documentation. |
| 39 | +Get started by checking out the [Realtime WebSockets API](/ai-gateway/websockets-api/realtime-api/) documentation. |
0 commit comments