|
| 1 | +--- |
| 2 | +pcx_content_type: configuration |
| 3 | +title: Realtime WebSockets API |
| 4 | +sidebar: |
| 5 | + order: 2 |
| 6 | +--- |
| 7 | + |
| 8 | +Some AI providers support real-time, low-latency interactions over WebSockets. AI Gateway allows seamless integration with these APIs, supporting multimodal interactions such as text, audio, and video. |
| 9 | + |
| 10 | +## Supported Providers |
| 11 | + |
| 12 | +- [OpenAI](https://platform.openai.com/docs/guides/realtime-websocket) |
| 13 | +- [Google AI Studio](https://ai.google.dev/gemini-api/docs/multimodal-live) |
| 14 | +- [Cartesia](https://docs.cartesia.ai/api-reference/tts/tts) |
| 15 | +- [ElevenLabs](https://elevenlabs.io/docs/conversational-ai/api-reference/conversational-ai/websocket) |
| 16 | + |
| 17 | +## Authentication |
| 18 | + |
| 19 | +For real-time WebSockets, authentication can be done using: |
| 20 | + |
| 21 | +- Headers (for non-browser environments) |
| 22 | +- `sec-websocket-protocol` (for browsers) |
| 23 | + |
| 24 | +## Examples |
| 25 | + |
| 26 | +### OpenAI |
| 27 | + |
| 28 | +```javascript |
| 29 | +import WebSocket from "ws"; |
| 30 | + |
| 31 | +const url = |
| 32 | + "wss://gateway.ai.cloudflare.com/v1/<account_id>/<gateway>/openai?model=gpt-4o-realtime-preview-2024-12-17"; |
| 33 | +const ws = new WebSocket(url, { |
| 34 | + headers: { |
| 35 | + "cf-aig-authorization": process.env.CLOUDFLARE_API_KEY, |
| 36 | + Authorization: "Bearer " + process.env.OPENAI_API_KEY, |
| 37 | + "OpenAI-Beta": "realtime=v1", |
| 38 | + }, |
| 39 | +}); |
| 40 | + |
| 41 | +ws.on("open", () => console.log("Connected to server.")); |
| 42 | +ws.on("message", (message) => console.log(JSON.parse(message.toString()))); |
| 43 | + |
| 44 | +ws.send( |
| 45 | + JSON.stringify({ |
| 46 | + type: "response.create", |
| 47 | + response: { modalities: ["text"], instructions: "Tell me a joke" }, |
| 48 | + }), |
| 49 | +); |
| 50 | +``` |
| 51 | + |
| 52 | +### Google AI Studio |
| 53 | + |
| 54 | +```javascript |
| 55 | +const ws = new WebSocket( |
| 56 | + "wss://gateway.ai.cloudflare.com/v1/<account_id>/<gateway>/google?api_key=<google_api_key>", |
| 57 | + ["cf-aig-authorization.<cloudflare_token>"], |
| 58 | +); |
| 59 | + |
| 60 | +ws.on("open", () => console.log("Connected to server.")); |
| 61 | +ws.on("message", (message) => console.log(message.data)); |
| 62 | + |
| 63 | +ws.send( |
| 64 | + JSON.stringify({ |
| 65 | + setup: { |
| 66 | + model: "models/gemini-2.0-flash-exp", |
| 67 | + generationConfig: { responseModalities: ["TEXT"] }, |
| 68 | + }, |
| 69 | + }), |
| 70 | +); |
| 71 | +``` |
| 72 | + |
| 73 | +### Cartesia |
| 74 | + |
| 75 | +```javascript |
| 76 | +const ws = new WebSocket( |
| 77 | + "wss://gateway.ai.cloudflare.com/v1/<account_id>/<gateway>/cartesia?cartesia_version=2024-06-10&api_key=<cartesia_api_key>", |
| 78 | + ["cf-aig-authorization.<cloudflare_token>"], |
| 79 | +); |
| 80 | + |
| 81 | +ws.on("open", function open() { |
| 82 | + console.log("Connected to server."); |
| 83 | +}); |
| 84 | + |
| 85 | +ws.on("message", function incoming(message) { |
| 86 | + console.log(message.data); |
| 87 | +}); |
| 88 | + |
| 89 | +ws.send( |
| 90 | + JSON.stringify({ |
| 91 | + model_id: "sonic", |
| 92 | + transcript: "Hello, world! I'm generating audio on ", |
| 93 | + voice: { mode: "id", id: "a0e99841-438c-4a64-b679-ae501e7d6091" }, |
| 94 | + language: "en", |
| 95 | + context_id: "happy-monkeys-fly", |
| 96 | + output_format: { |
| 97 | + container: "raw", |
| 98 | + encoding: "pcm_s16le", |
| 99 | + sample_rate: 8000, |
| 100 | + }, |
| 101 | + add_timestamps: true, |
| 102 | + continue: true, |
| 103 | + }), |
| 104 | +); |
| 105 | +``` |
| 106 | + |
| 107 | +### ElevenLabs |
| 108 | + |
| 109 | +```javascript |
| 110 | +const ws = new WebSocket( |
| 111 | + "wss://gateway.ai.cloudflare.com/v1/<account_id>/<gateway>/elevenlabs?agent_id=<elevenlabs_agent_id>", |
| 112 | + [ |
| 113 | + "xi-api-key.<elevenlabs_api_key>", |
| 114 | + "cf-aig-authorization.<cloudflare_token>", |
| 115 | + ], |
| 116 | +); |
| 117 | + |
| 118 | +ws.on("open", function open() { |
| 119 | + console.log("Connected to server."); |
| 120 | +}); |
| 121 | + |
| 122 | +ws.on("message", function incoming(message) { |
| 123 | + console.log(message.data); |
| 124 | +}); |
| 125 | + |
| 126 | +ws.send( |
| 127 | + JSON.stringify({ |
| 128 | + text: "This is a sample text ", |
| 129 | + voice_settings: { stability: 0.8, similarity_boost: 0.8 }, |
| 130 | + generation_config: { chunk_length_schedule: [120, 160, 250, 290] }, |
| 131 | + }), |
| 132 | +); |
| 133 | +``` |
0 commit comments