Applying for and Using the OpenAI Live API

The OpenAI Live API uses gpt-live-1 to provide real-time voice conversations, supporting simultaneous listening and speaking, two-way captions, and background task delegation. This article describes the complete integration process, from obtaining an API Token and configuring a session to sending and receiving audio and ending a call.

Application Process

  1. Open the Ace Data Cloud console, open General Application after logging in, and click the copy button to the right of the API KEY field.
  2. Save the Token in your server-side environment variables. One general-purpose API Token can call all platform services; if you use service-specific credentials, make sure they have permission to call the OpenAI Live API.
  3. Make sure your account has available credits; if your balance is insufficient, click the “Recharge” button.

Obtain an Ace Data Cloud API Key

Full parameter documentation: OpenAI Live API. Browser applications should use restricted credentials and avoid hardcoding long-lived API Tokens into the frontend.

Basic Usage

Enter Session Parameters

Open the OpenAI Live API parameter page, select or enter authorization, and then edit the session.start message in the request body. The JavaScript and Python examples on the right update with the parameters.

Field Type Description
type string The first message must be session.start.
session object Required; session configuration.
session.model string Model, fixed at gpt-live-1.
session.instructions string Optional; behavior and response requirements for the voice assistant.
session.audio.format object Audio format, fixed at {"type":"audio/pcm","rate":24000}, meaning 24 kHz mono PCM16.
session.audio.output.voice string Optional; select a voice when starting the call, defaults to marin; see the parameter page for supported voices.
session.delegation object Background task delegation method, fixed at {"type":"client"}; your application handles background tasks.
session.store boolean Whether to save the recording, currently fixed at false.

This interface sends and receives JSON messages over WebSocket. HTTP GET is used only to establish the connection; the request body on the page is the first WebSocket message sent after the connection is established. Clicking “Connect” establishes a session and sends that message; audio capture and playback must be implemented in your application.

Establish a Connection

General-purpose or OpenAI service credentials use wss://realtime.acedata.cloud/v1/live/sessions; chat service credentials use wss://realtime.acedata.cloud/aichat2/live. The server authenticates through the Authorization: Bearer <token> request header. Browsers pass credentials using the WebSocket subprotocols live and acedata-token.<token>.

The following example uses a general-purpose API Token. Replace <YOUR_API_TOKEN> with the Token you obtained:

const token = '<YOUR_API_TOKEN>';
const socket = new WebSocket('wss://realtime.acedata.cloud/v1/live/sessions', [
  'live', `acedata-token.${token}`
]);
socket.onopen = () => socket.send(JSON.stringify({
  type: 'session.start',
  session: {
    model: 'gpt-live-1',
    instructions: '简洁友好地回答,使用用户的语言。复杂问题交给后台处理。',
    audio: { format: { type: 'audio/pcm', rate: 24000 }, output: { voice: 'marin' } },
    delegation: { type: 'client' },
    store: false
  }
}));

socket.onmessage = ({ data }) => {
  const event = JSON.parse(data);
  if (event.type === 'session.started') {
    console.log('会话已开始,可以发送音频');
  } else if (event.type === 'session.input_transcript.delta' ||
             event.type === 'session.output_transcript.delta') {
    console.log(event.type, event.delta);
  } else if (event.type === 'session.output_audio.delta') {
    // 将 event.delta 的 Base64 解码为 PCM16,按顺序加入音频播放队列。
  } else if (event.type === 'session.closed') {
    console.log('最终用量', event.usage);
    socket.close();
  } else if (event.type === 'error') {
    console.error(event.error);
  }
};

Send Audio and Receive Results

After waiting for session.started, continuously send session.input_audio.append, where audio is the Base64 encoding of raw 24 kHz, mono, 16-bit little-endian PCM audio, without a WAV header. For example, the application sends a message each time it captures a segment of audio:

function sendAudio(base64Pcm16) {
  socket.send(JSON.stringify({
    type: 'session.input_audio.append',
    audio: base64Pcm16
  }));
}

Send audio at the actual sampling rate, keeping silent segments continuous as well.

Play session.output_audio.delta.delta in order. User and assistant captions come from session.input_transcript.delta and session.output_transcript.delta respectively; retain each segment’s delta, start_ms, and end_ms. Captions for both sides may grow simultaneously. Live does not have an event marking the end of each voice response; update the “responding” status based on the local playback queue.

Background Task Delegation

The client delegation mode is currently supported. After receiving session.delegation.created, the application calls its own backend using the voice transcripts and saved context, and returns results through session.thinking.append or session.commentary.append, carrying the corresponding delegation_id and plain-text content of up to 500 tokens. Results should come from completed background operations; an interruption in speech does not mean the background task has been canceled.

Hosted Responses delegation, recording storage, session.update, session resumption, and automatic reconnection are not currently supported. The model is fixed at gpt-live-1, audio is fixed at 24 kHz PCM16, and the voice is selected when starting the call. The older Realtime API can still be used.

End the Call

When ending the call, send the following message and continue receiving until session.closed is received, then close the connection and release the microphone:

socket.send(JSON.stringify({ type: 'session.close' }));

Forcibly disconnecting may leave final usage unconfirmed; the interface should report this clearly. The connection ends when the balance is insufficient, billing fails, or the session limit is reached. By default, each call lasts at most 30 minutes.

Billing

Voice is billed by active session duration: 0.01 Credits/second, or 0.6 Credits/minute, without rounding up to whole minutes. Time spent with the user speaking, the assistant speaking, silence, both parties silent, and waiting for background tasks is all billed. Background model or tool calls are billed separately according to the corresponding API. The USD price of Credits depends on the recharge package; refer to the current price in the console.

session.usage.updated.usage.seconds is cumulative usage. For example, if 12 seconds is received first and then 15 seconds, the total is 15 seconds. The platform bills only the additional duration and does not add snapshots repeatedly.

Error Handling

Parameter errors are returned through the error event; error.client_event_id corresponds to the failed command. If startup fails, stop recording and display the error to avoid incurring new charges from repeated requests. If credentials are invalid, check whether the Token is complete, whether it has permission for the corresponding service, and whether the connection address used matches the credential type.