Skip to content

The Non-realtime WebSockets API allows you to establish persistent connections for AI requests without requiring repeated handshakes. This approach is ideal for applications that do not require real-time interactions but still benefit from reduced latency and continuous communication.

Set up WebSockets API

  1. Generate an AI Gateway token with appropriate AI Gateway Run and opt in to using an authenticated gateway.
  2. Modify your Universal Endpoint URL by replacing https:// with wss:// to initiate a WebSocket connection:
    wss://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}
  3. Open a WebSocket connection authenticated with a Cloudflare token with the AI Gateway Run permission.

Example request

JavaScript
import WebSocket from "ws";
const ws