Skip to content
For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending .md to the page URL.
Primary navigation

Connect

Connect to a persistent Responses API WebSocket. Send response.create events to create responses and response.steer events to queue user input containing text, images, or files for a continuation. Receive response stream events and steering acceptance, pending, or failure events over the socket.

WebSocket wss://api.openai.com/v1/responses

Connect

import OpenAI from 'openai';
import { ResponsesWS } from 'openai/resources/responses/ws';

async function main() {
  const connection = new ResponsesWS(new OpenAI());
  const deadline = setTimeout(() => connection.close(), 60_000);
  try {
    for await (const item of connection) {
      if (item.type === 'open') {
        connection.send({
          type: 'response.create',
          model: 'gpt-6.2-astra',
          input: 'Say hello in one sentence.',
        });
      } else if (item.type === 'error') {
        throw item.error;
      } else if (item.type === 'message') {
        const event = item.message;
        if (event.type === 'response.output_text.delta') {
          process.stdout.write(event.delta);
        } else if (event.type === 'response.completed') {
          process.stdout.write('\n');
          return;
        } else if (event.type === 'response.failed' || event.type === 'response.incomplete') {
          throw new Error(`Response ended: ${event.type}`);
        }
      }
    }
    throw new Error('Connection closed or timed out before the response completed.');
  } finally {
    clearTimeout(deadline);
    connection.close();
  }
}

await main();