telnyx-voice-streaming-curl
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinese<!-- Auto-generated from Telnyx OpenAPI specs. Do not edit. -->
<!-- Auto-generated from Telnyx OpenAPI specs. Do not edit. -->
Telnyx Voice Streaming - curl
Telnyx 语音流 - curl
Installation
安装
text
undefinedtext
undefinedcurl is pre-installed on macOS, Linux, and Windows 10+
curl is pre-installed on macOS, Linux, and Windows 10+
undefinedundefinedSetup
环境配置
bash
export TELNYX_API_KEY="YOUR_API_KEY_HERE"All examples below use for authentication.
$TELNYX_API_KEYbash
export TELNYX_API_KEY="YOUR_API_KEY_HERE"以下所有示例都使用进行身份验证。
$TELNYX_API_KEYError Handling
错误处理
All API calls can fail with network errors, rate limits (429), validation errors (422),
or authentication errors (401). Always handle errors in production code:
bash
undefined所有API调用都可能因网络错误、速率限制(429)、校验错误(422)或身份验证错误(401)失败。生产环境代码中请务必做好错误处理:
bash
undefinedCheck HTTP status code in response
Check HTTP status code in response
response=$(curl -s -w "\n%{http_code}"
-X POST "https://api.telnyx.com/v2/messages"
-H "Authorization: Bearer $TELNYX_API_KEY"
-H "Content-Type: application/json"
-d '{"to": "+13125550001", "from": "+13125550002", "text": "Hello"}')
-X POST "https://api.telnyx.com/v2/messages"
-H "Authorization: Bearer $TELNYX_API_KEY"
-H "Content-Type: application/json"
-d '{"to": "+13125550001", "from": "+13125550002", "text": "Hello"}')
http_code=$(echo "$response" | tail -1)
body=$(echo "$response" | sed '$d')
case $http_code in
2*) echo "Success: $body" ;;
422) echo "Validation error — check required fields and formats" ;;
429) echo "Rate limited — retry after delay"; sleep 1 ;;
401) echo "Authentication failed — check TELNYX_API_KEY" ;;
*) echo "Error $http_code: $body" ;;
esac
Common error codes: `401` invalid API key, `403` insufficient permissions,
`404` resource not found, `422` validation error (check field formats),
`429` rate limited (retry with exponential backoff).response=$(curl -s -w "\n%{http_code}"
-X POST "https://api.telnyx.com/v2/messages"
-H "Authorization: Bearer $TELNYX_API_KEY"
-H "Content-Type: application/json"
-d '{"to": "+13125550001", "from": "+13125550002", "text": "Hello"}')
-X POST "https://api.telnyx.com/v2/messages"
-H "Authorization: Bearer $TELNYX_API_KEY"
-H "Content-Type: application/json"
-d '{"to": "+13125550001", "from": "+13125550002", "text": "Hello"}')
http_code=$(echo "$response" | tail -1)
body=$(echo "$response" | sed '$d')
case $http_code in
2*) echo "Success: $body" ;;
422) echo "Validation error — check required fields and formats" ;;
429) echo "Rate limited — retry after delay"; sleep 1 ;;
401) echo "Authentication failed — check TELNYX_API_KEY" ;;
*) echo "Error $http_code: $body" ;;
esac
常见错误码:`401` 无效API密钥,`403` 权限不足,`404` 资源不存在,`422` 校验错误(请检查字段格式),`429` 触发速率限制(请使用指数退避策略重试)。Forking start
启动分流
Call forking allows you to stream the media from a call to a specific target in realtime. This stream can be used to enable realtime audio analysis to support a
variety of use cases, including fraud detection, or the creation of AI-generated audio responses. Requests must specify either the attribute or the and attributes.
targetrxtxPOST /calls/{call_control_id}/actions/fork_startOptional: (string), (string), (string), (enum: decrypted), (string)
client_statecommand_idrxstream_typetxbash
curl \
-X POST \
-H "Authorization: Bearer $TELNYX_API_KEY" \
-H "Content-Type: application/json" \
"https://api.telnyx.com/v2/calls/v3:550e8400-e29b-41d4-a716-446655440000_gRU1OGRkYQ/actions/fork_start"Returns: (string)
result通话分流功能允许你将通话中的媒体实时传输到指定目标。该流可用于实现实时音频分析,支持多种场景,包括欺诈检测,或者生成AI音频响应。请求必须指定属性,或者和属性。
targetrxtxPOST /calls/{call_control_id}/actions/fork_start可选参数: (字符串), (字符串), (字符串), (枚举: decrypted), (字符串)
client_statecommand_idrxstream_typetxbash
curl \
-X POST \
-H "Authorization: Bearer $TELNYX_API_KEY" \
-H "Content-Type: application/json" \
"https://api.telnyx.com/v2/calls/v3:550e8400-e29b-41d4-a716-446655440000_gRU1OGRkYQ/actions/fork_start"返回值: (字符串)
resultForking stop
停止分流
Stop forking a call. Expected Webhooks:
call.fork.stopped
POST /calls/{call_control_id}/actions/fork_stopOptional: (string), (string), (enum: raw, decrypted)
client_statecommand_idstream_typebash
curl \
-X POST \
-H "Authorization: Bearer $TELNYX_API_KEY" \
-H "Content-Type: application/json" \
"https://api.telnyx.com/v2/calls/v3:550e8400-e29b-41d4-a716-446655440000_gRU1OGRkYQ/actions/fork_stop"Returns: (string)
result停止通话分流。预期触发的Webhooks:
call.fork.stopped
POST /calls/{call_control_id}/actions/fork_stop可选参数: (字符串), (字符串), (枚举: raw, decrypted)
client_statecommand_idstream_typebash
curl \
-X POST \
-H "Authorization: Bearer $TELNYX_API_KEY" \
-H "Content-Type: application/json" \
"https://api.telnyx.com/v2/calls/v3:550e8400-e29b-41d4-a716-446655440000_gRU1OGRkYQ/actions/fork_stop"返回值: (字符串)
resultStreaming start
启动流传输
Start streaming the media from a call to a specific WebSocket address or Dialogflow connection in near-realtime. Audio will be delivered as base64-encoded RTP payload (raw audio), wrapped in JSON payloads. Please find more details about media streaming messages specification under the link.
POST /calls/{call_control_id}/actions/streaming_startOptional: (string), (string), (array[object]), (object), (boolean), (string), (enum: PCMU, PCMA, G722, OPUS, AMR-WB, L16), (enum: mp3, rtp), (enum: 8000, 16000, 22050, 24000, 48000), (enum: both, self, opposite), (enum: PCMU, PCMA, G722, OPUS, AMR-WB, L16, default), (enum: inbound_track, outbound_track, both_tracks), (string)
client_statecommand_idcustom_parametersdialogflow_configenable_dialogflowstream_auth_tokenstream_bidirectional_codecstream_bidirectional_modestream_bidirectional_sampling_ratestream_bidirectional_target_legsstream_codecstream_trackstream_urlbash
curl \
-X POST \
-H "Authorization: Bearer $TELNYX_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"stream_url": "wss://example.com/audio-stream"
}' \
"https://api.telnyx.com/v2/calls/v3:550e8400-e29b-41d4-a716-446655440000_gRU1OGRkYQ/actions/streaming_start"Returns: (string)
result近乎实时地将通话媒体传输到指定的WebSocket地址或Dialogflow连接。音频将以base64编码的RTP payload(原始音频)格式封装在JSON payload中传输。你可以通过以下链接查看媒体流消息规范的更多详情。
POST /calls/{call_control_id}/actions/streaming_start可选参数: (字符串), (字符串), (对象数组), (对象), (布尔值), (字符串), (枚举: PCMU, PCMA, G722, OPUS, AMR-WB, L16), (枚举: mp3, rtp), (枚举: 8000, 16000, 22050, 24000, 48000), (枚举: both, self, opposite), (枚举: PCMU, PCMA, G722, OPUS, AMR-WB, L16, default), (枚举: inbound_track, outbound_track, both_tracks), (字符串)
client_statecommand_idcustom_parametersdialogflow_configenable_dialogflowstream_auth_tokenstream_bidirectional_codecstream_bidirectional_modestream_bidirectional_sampling_ratestream_bidirectional_target_legsstream_codecstream_trackstream_urlbash
curl \
-X POST \
-H "Authorization: Bearer $TELNYX_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"stream_url": "wss://example.com/audio-stream"
}' \
"https://api.telnyx.com/v2/calls/v3:550e8400-e29b-41d4-a716-446655440000_gRU1OGRkYQ/actions/streaming_start"返回值: (字符串)
resultStreaming stop
停止流传输
Stop streaming a call to a WebSocket. Expected Webhooks:
streaming.stopped
POST /calls/{call_control_id}/actions/streaming_stopOptional: (string), (string), (uuid)
client_statecommand_idstream_idbash
curl \
-X POST \
-H "Authorization: Bearer $TELNYX_API_KEY" \
-H "Content-Type: application/json" \
"https://api.telnyx.com/v2/calls/v3:550e8400-e29b-41d4-a716-446655440000_gRU1OGRkYQ/actions/streaming_stop"Returns: (string)
result停止向WebSocket传输通话流。预期触发的Webhooks:
streaming.stopped
POST /calls/{call_control_id}/actions/streaming_stop可选参数: (字符串), (字符串), (uuid)
client_statecommand_idstream_idbash
curl \
-X POST \
-H "Authorization: Bearer $TELNYX_API_KEY" \
-H "Content-Type: application/json" \
"https://api.telnyx.com/v2/calls/v3:550e8400-e29b-41d4-a716-446655440000_gRU1OGRkYQ/actions/streaming_stop"返回值: (字符串)
resultTranscription start
启动实时转写
Start real-time transcription. Transcription will stop on call hang-up, or can be initiated via the Transcription stop command. Expected Webhooks:
call.transcription
POST /calls/{call_control_id}/actions/transcription_startOptional: (string), (string), (enum: Google, Telnyx, Deepgram, Azure, A, B), (object), (string)
client_statecommand_idtranscription_enginetranscription_engine_configtranscription_tracksbash
curl \
-X POST \
-H "Authorization: Bearer $TELNYX_API_KEY" \
-H "Content-Type: application/json" \
"https://api.telnyx.com/v2/calls/v3:550e8400-e29b-41d4-a716-446655440000_gRU1OGRkYQ/actions/transcription_start"Returns: (string)
result启动实时语音转写。转写将在通话挂断时自动停止,也可以通过转写停止命令手动终止。预期触发的Webhooks:
call.transcription
POST /calls/{call_control_id}/actions/transcription_start可选参数: (字符串), (字符串), (枚举: Google, Telnyx, Deepgram, Azure, A, B), (对象), (字符串)
client_statecommand_idtranscription_enginetranscription_engine_configtranscription_tracksbash
curl \
-X POST \
-H "Authorization: Bearer $TELNYX_API_KEY" \
-H "Content-Type: application/json" \
"https://api.telnyx.com/v2/calls/v3:550e8400-e29b-41d4-a716-446655440000_gRU1OGRkYQ/actions/transcription_start"返回值: (字符串)
resultTranscription stop
停止实时转写
Stop real-time transcription.
POST /calls/{call_control_id}/actions/transcription_stopOptional: (string), (string)
client_statecommand_idbash
curl \
-X POST \
-H "Authorization: Bearer $TELNYX_API_KEY" \
-H "Content-Type: application/json" \
"https://api.telnyx.com/v2/calls/v3:550e8400-e29b-41d4-a716-446655440000_gRU1OGRkYQ/actions/transcription_stop"Returns: (string)
result停止实时语音转写。
POST /calls/{call_control_id}/actions/transcription_stop可选参数: (字符串), (字符串)
client_statecommand_idbash
curl \
-X POST \
-H "Authorization: Bearer $TELNYX_API_KEY" \
-H "Content-Type: application/json" \
"https://api.telnyx.com/v2/calls/v3:550e8400-e29b-41d4-a716-446655440000_gRU1OGRkYQ/actions/transcription_stop"返回值: (字符串)
resultWebhooks
Webhooks
Webhook Verification
Webhook签名验证
Telnyx signs webhooks with Ed25519. Each request includes
and headers. Always verify signatures in production:
telnyx-signature-ed25519telnyx-timestampbash
undefinedTelnyx使用Ed25519对Webhooks进行签名。每个请求都包含和请求头。生产环境中请务必验证签名:
telnyx-signature-ed25519telnyx-timestampbash
undefinedTelnyx signs webhooks with Ed25519 (asymmetric — NOT HMAC/Standard Webhooks).
Telnyx signs webhooks with Ed25519 (asymmetric — NOT HMAC/Standard Webhooks).
Headers sent with each webhook:
Headers sent with each webhook:
telnyx-signature-ed25519: base64-encoded Ed25519 signature
telnyx-signature-ed25519: base64-encoded Ed25519 signature
telnyx-timestamp: Unix timestamp (reject if >5 minutes old for replay protection)
telnyx-timestamp: Unix timestamp (reject if >5 minutes old for replay protection)
Get your public key from: Telnyx Portal > Account Settings > Keys & Credentials
Get your public key from: Telnyx Portal > Account Settings > Keys & Credentials
Use the Telnyx SDK in your language for verification (client.webhooks.unwrap).
Use the Telnyx SDK in your language for verification (client.webhooks.unwrap).
Your endpoint MUST return 2xx within 2 seconds or Telnyx will retry (up to 3 attempts).
Your endpoint MUST return 2xx within 2 seconds or Telnyx will retry (up to 3 attempts).
Configure a failover URL in Telnyx Portal for additional reliability.
Configure a failover URL in Telnyx Portal for additional reliability.
The following webhook events are sent to your configured webhook URL.
All webhooks include `telnyx-timestamp` and `telnyx-signature-ed25519` headers for Ed25519 signature verification. Use `client.webhooks.unwrap()` to verify.
| Event | Description |
|-------|-------------|
| `callForkStarted` | Call Fork Started |
| `callForkStopped` | Call Fork Stopped |
| `callStreamingFailed` | Call Streaming Failed |
| `callStreamingStarted` | Call Streaming Started |
| `callStreamingStopped` | Call Streaming Stopped |
| `transcription` | Transcription |
以下Webhook事件会发送到你配置的Webhook地址。所有Webhooks都包含`telnyx-timestamp`和`telnyx-signature-ed25519`头用于Ed25519签名验证。你可以使用`client.webhooks.unwrap()`方法进行验证。
| 事件 | 描述 |
|-------|-------------|
| `callForkStarted` | 通话分流已启动 |
| `callForkStopped` | 通话分流已停止 |
| `callStreamingFailed` | 通话流传输失败 |
| `callStreamingStarted` | 通话流传输已启动 |
| `callStreamingStopped` | 通话流传输已停止 |
| `transcription` | 转写结果 |Webhook payload fields
Webhook payload字段
callForkStarted| Field | Type | Description |
|---|---|---|
| enum: event | Identifies the type of the resource. |
| enum: call.fork.started | The type of event being delivered. |
| uuid | Identifies the type of resource. |
| date-time | ISO 8601 datetime of when the event occurred. |
| string | Call Control App ID (formerly Telnyx connection ID) used in the call. |
| string | Unique ID for controlling the call. |
| string | ID that is unique to the call and can be used to correlate webhook events. |
| string | ID that is unique to the call session and can be used to correlate webhook events. |
| string | State received from a command. |
| enum: decrypted | Type of media streamed. |
callForkStopped| Field | Type | Description |
|---|---|---|
| enum: event | Identifies the type of the resource. |
| enum: call.fork.stopped | The type of event being delivered. |
| uuid | Identifies the type of resource. |
| date-time | ISO 8601 datetime of when the event occurred. |
| string | Call Control App ID (formerly Telnyx connection ID) used in the call. |
| string | Unique ID for controlling the call. |
| string | ID that is unique to the call and can be used to correlate webhook events. |
| string | ID that is unique to the call session and can be used to correlate webhook events. |
| string | State received from a command. |
| enum: decrypted | Type of media streamed. |
callStreamingFailed| Field | Type | Description |
|---|---|---|
| enum: event | Identifies the resource. |
| enum: streaming.failed | The type of event being delivered. |
| uuid | Identifies the type of resource. |
| date-time | ISO 8601 datetime of when the event occurred. |
| string | Call ID used to issue commands via Call Control API. |
| string | Call Control App ID (formerly Telnyx connection ID) used in the call. |
| string | ID that is unique to the call and can be used to correlate webhook events. |
| string | ID that is unique to the call session and can be used to correlate webhook events. |
| string | State received from a command. |
| string | A short description explaning why the media streaming failed. |
| uuid | Identifies the streaming. |
| enum: websocket, dialogflow | The type of stream connection the stream is performing. |
callStreamingStarted| Field | Type | Description |
|---|---|---|
| enum: event | Identifies the type of the resource. |
| enum: streaming.started | The type of event being delivered. |
| uuid | Identifies the type of resource. |
| date-time | ISO 8601 datetime of when the event occurred. |
| string | Call ID used to issue commands via Call Control API. |
| string | Call Control App ID (formerly Telnyx connection ID) used in the call. |
| string | ID that is unique to the call and can be used to correlate webhook events. |
| string | ID that is unique to the call session and can be used to correlate webhook events. |
| string | State received from a command. |
| string | Destination WebSocket address where the stream is going to be delivered. |
callStreamingStopped| Field | Type | Description |
|---|---|---|
| enum: event | Identifies the type of the resource. |
| enum: streaming.stopped | The type of event being delivered. |
| uuid | Identifies the type of resource. |
| date-time | ISO 8601 datetime of when the event occurred. |
| string | Call ID used to issue commands via Call Control API. |
| string | Call Control App ID (formerly Telnyx connection ID) used in the call. |
| string | ID that is unique to the call and can be used to correlate webhook events. |
| string | ID that is unique to the call session and can be used to correlate webhook events. |
| string | State received from a command. |
| string | Destination WebSocket address where the stream is going to be delivered. |
transcription| Field | Type | Description |
|---|---|---|
| enum: event | Identifies the type of the resource. |
| enum: call.transcription | The type of event being delivered. |
| uuid | Identifies the type of resource. |
| date-time | ISO 8601 datetime of when the event occurred. |
| string | Unique identifier and token for controlling the call. |
| string | ID that is unique to the call and can be used to correlate webhook events. |
| string | ID that is unique to the call session and can be used to correlate webhook events. |
| string | Use this field to add state to every subsequent webhook. |
| string | Call Control App ID (formerly Telnyx connection ID) used in the call. |
callForkStarted| 字段 | 类型 | 描述 |
|---|---|---|
| 枚举: event | 标识资源类型。 |
| 枚举: call.fork.started | 所传递的事件类型。 |
| uuid | 标识资源类型。 |
| date-time | 事件发生的ISO 8601格式时间戳。 |
| 字符串 | 通话所使用的Call Control App ID(原Telnyx connection ID)。 |
| 字符串 | 用于控制通话的唯一ID。 |
| 字符串 | 通话的唯一ID,可用于关联Webhook事件。 |
| 字符串 | 通话会话的唯一ID,可用于关联Webhook事件。 |
| 字符串 | 从命令中获取的状态。 |
| 枚举: decrypted | 所传输的媒体类型。 |
callForkStopped| 字段 | 类型 | 描述 |
|---|---|---|
| 枚举: event | 标识资源类型。 |
| 枚举: call.fork.stopped | 所传递的事件类型。 |
| uuid | 标识资源类型。 |
| date-time | 事件发生的ISO 8601格式时间戳。 |
| 字符串 | 通话所使用的Call Control App ID(原Telnyx connection ID)。 |
| 字符串 | 用于控制通话的唯一ID。 |
| 字符串 | 通话的唯一ID,可用于关联Webhook事件。 |
| 字符串 | 通话会话的唯一ID,可用于关联Webhook事件。 |
| 字符串 | 从命令中获取的状态。 |
| 枚举: decrypted | 所传输的媒体类型。 |
callStreamingFailed| 字段 | 类型 | 描述 |
|---|---|---|
| 枚举: event | 标识资源类型。 |
| 枚举: streaming.failed | 所传递的事件类型。 |
| uuid | 标识资源类型。 |
| date-time | 事件发生的ISO 8601格式时间戳。 |
| 字符串 | 用于通过Call Control API下发命令的通话ID。 |
| 字符串 | 通话所使用的Call Control App ID(原Telnyx connection ID)。 |
| 字符串 | 通话的唯一ID,可用于关联Webhook事件。 |
| 字符串 | 通话会话的唯一ID,可用于关联Webhook事件。 |
| 字符串 | 从命令中获取的状态。 |
| 字符串 | 媒体流传输失败的简短原因描述。 |
| uuid | 流传输的唯一标识。 |
| 枚举: websocket, dialogflow | 当前使用的流连接类型。 |
callStreamingStarted| 字段 | 类型 | 描述 |
|---|---|---|
| 枚举: event | 标识资源类型。 |
| 枚举: streaming.started | 所传递的事件类型。 |
| uuid | 标识资源类型。 |
| date-time | 事件发生的ISO 8601格式时间戳。 |
| 字符串 | 用于通过Call Control API下发命令的通话ID。 |
| 字符串 | 通话所使用的Call Control App ID(原Telnyx connection ID)。 |
| 字符串 | 通话的唯一ID,可用于关联Webhook事件。 |
| 字符串 | 通话会话的唯一ID,可用于关联Webhook事件。 |
| 字符串 | 从命令中获取的状态。 |
| 字符串 | 流传输的目标WebSocket地址。 |
callStreamingStopped| 字段 | 类型 | 描述 |
|---|---|---|
| 枚举: event | 标识资源类型。 |
| 枚举: streaming.stopped | 所传递的事件类型。 |
| uuid | 标识资源类型。 |
| date-time | 事件发生的ISO 8601格式时间戳。 |
| 字符串 | 用于通过Call Control API下发命令的通话ID。 |
| 字符串 | 通话所使用的Call Control App ID(原Telnyx connection ID)。 |
| 字符串 | 通话的唯一ID,可用于关联Webhook事件。 |
| 字符串 | 通话会话的唯一ID,可用于关联Webhook事件。 |
| 字符串 | 从命令中获取的状态。 |
| 字符串 | 流传输的目标WebSocket地址。 |
transcription| 字段 | 类型 | 描述 |
|---|---|---|
| 枚举: event | 标识资源类型。 |
| 枚举: call.transcription | 所传递的事件类型。 |
| uuid | 标识资源类型。 |
| date-time | 事件发生的ISO 8601格式时间戳。 |
| 字符串 | 用于控制通话的唯一标识和令牌。 |
| 字符串 | 通话的唯一ID,可用于关联Webhook事件。 |
| 字符串 | 通话会话的唯一ID,可用于关联Webhook事件。 |
| 字符串 | 可通过该字段为后续所有Webhook添加状态信息。 |
| 字符串 | 通话所使用的Call Control App ID(原Telnyx connection ID)。 |