Skip to main content

WebSocket Access Guide

This article describes the endpoint URL, request headers, and interaction flow for accessing the Paraformer Real-Time Speech Recognition service over a WebSocket connection.

This document applies only to the China (Beijing) region. To use the model, you need an API Key from the China (Beijing) region.
Alibaba Cloud Model Studio offers a workspace-specific domain for the China (Beijing) region, which delivers superior performance and higher stability for inference requests. We recommend migrating from dashscope.aliyuncs.com to {WorkspaceId}.cn-beijing.maas.aliyuncs.com.Replace {WorkspaceId} with your actual Workspace ID. The existing domain remains available.
User guide: For model introductions and selection advice, see Speech recognition; for sample code, see Real-time speech recognition. The DashScope SDK currently supports only Java and Python. When using other programming languages, you can communicate with the service over a WebSocket connection.

Endpoint URL

Paraformer is supported only in the China (Beijing) region. The WebSocket URL is fixed as follows: wss://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api-ws/v1/inference When making calls, replace {WorkspaceId} with your actual Workspace ID.
The URL must use the wss:// protocol and is fixed. Authorization is set in the request headers (see Request headers).

Request headers

Add the following information to the request headers:

Parameter

Type

Required

Description

Authorization

string

Yes

The authentication token, in the format Bearer <your_api_key>. When using it, replace "<your_api_key>" with your actual API Key.

user-agent

string

No

The client identifier, which helps the server track the request source.

X-DashScope-WorkSpace

string

No

The Alibaba Cloud Model Studio workspace ID.

X-DashScope-DataInspection

string

No

Whether to enable the data compliance detection feature. By default, this parameter is not passed or is set to enable. Do not enable it unless necessary.

Authorization is verified during the WebSocket handshake. If the API Key is invalid or missing, the handshake fails and returns an HTTP 401/403 error.

Interaction flow

For detailed descriptions of client events and server events, see Client events and Server events.
image
In chronological order, the interaction flow between the client and the server is as follows:
  1. Establish a connection: The client establishes a WebSocket connection with the server.
  2. Start the task: The client sends the run-task instruction to start the task and receives the task-started event returned by the server, indicating that the task has been successfully started and subsequent steps can proceed.
  3. Send the audio stream: The client starts sending binary audio (which must be mono audio) and simultaneously receives the result-generated events continuously returned by the server. These events contain speech recognition results.
  4. Notify the server to end the task: The client sends the finish-task instruction to notify the server to end the task and continues to receive the result-generated events returned by the server.
  5. Task ends: The client receives the task-finished event returned by the server, marking the end of the task.
  6. Close the connection: The client closes the WebSocket connection.

Available SDKs