telnyx-webrtc-javascript

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese
<!-- Auto-generated from Telnyx OpenAPI specs. Do not edit. -->
<!-- 从Telnyx OpenAPI规范自动生成,请勿编辑。 -->

Telnyx Webrtc - JavaScript

Telnyx Webrtc - JavaScript

Installation

安装

bash
npm install telnyx
bash
npm install telnyx

Setup

初始化设置

javascript
import Telnyx from 'telnyx';

const client = new Telnyx({
  apiKey: process.env['TELNYX_API_KEY'], // This is the default and can be omitted
});
All examples below assume
client
is already initialized as shown above.
javascript
import Telnyx from 'telnyx';

const client = new Telnyx({
  apiKey: process.env['TELNYX_API_KEY'], // 这是默认配置,可以省略
});
以下所有示例均假设
client
已按上述方式完成初始化。

Error 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:
javascript
try {
  const result = await client.messages.send({ to: '+13125550001', from: '+13125550002', text: 'Hello' });
} catch (err) {
  if (err instanceof Telnyx.APIConnectionError) {
    console.error('Network error — check connectivity and retry');
  } else if (err instanceof Telnyx.RateLimitError) {
    // 429: rate limited — wait and retry with exponential backoff
    const retryAfter = err.headers?.['retry-after'] || 1;
    await new Promise(r => setTimeout(r, retryAfter * 1000));
  } else if (err instanceof Telnyx.APIError) {
    console.error(`API error ${err.status}: ${err.message}`);
    if (err.status === 422) {
      console.error('Validation error — check required fields and formats');
    }
  }
}
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).
所有API调用都可能因网络错误、速率限制(429)、验证错误(422)或身份验证错误(401)失败。在生产代码中请务必处理错误:
javascript
try {
  const result = await client.messages.send({ to: '+13125550001', from: '+13125550002', text: 'Hello' });
} catch (err) {
  if (err instanceof Telnyx.APIConnectionError) {
    console.error('网络错误 —— 检查网络连接后重试');
  } else if (err instanceof Telnyx.RateLimitError) {
    // 429: 触发速率限制 —— 等待后使用指数退避策略重试
    const retryAfter = err.headers?.['retry-after'] || 1;
    await new Promise(r => setTimeout(r, retryAfter * 1000));
  } else if (err instanceof Telnyx.APIError) {
    console.error(`API错误 ${err.status}: ${err.message}`);
    if (err.status === 422) {
      console.error('验证错误 —— 检查必填字段和格式是否正确');
    }
  }
}
常见错误码:
401
API密钥无效,
403
权限不足,
404
资源不存在,
422
验证错误(检查字段格式),
429
触发速率限制(使用指数退避策略重试)。

Important Notes

重要说明

  • Pagination: List methods return an auto-paginating iterator. Use
    for await (const item of result) { ... }
    to iterate through all pages automatically.
  • 分页: 列表类方法返回支持自动分页的迭代器,使用
    for await (const item of result) { ... }
    即可自动遍历所有页面数据。

List mobile push credentials

列出移动推送凭证

GET /mobile_push_credentials
javascript
// Automatically fetches more pages as needed.
for await (const pushCredential of client.mobilePushCredentials.list()) {
  console.log(pushCredential.id);
}
Returns:
alias
(string),
certificate
(string),
created_at
(date-time),
id
(string),
private_key
(string),
project_account_json_file
(object),
record_type
(string),
type
(string),
updated_at
(date-time)
GET /mobile_push_credentials
javascript
// 按需自动拉取更多页面数据
for await (const pushCredential of client.mobilePushCredentials.list()) {
  console.log(pushCredential.id);
}
返回参数:
alias
(字符串)、
certificate
(字符串)、
created_at
(日期时间)、
id
(字符串)、
private_key
(字符串)、
project_account_json_file
(对象)、
record_type
(字符串)、
type
(字符串)、
updated_at
(日期时间)

Creates a new mobile push credential

创建新的移动推送凭证

POST /mobile_push_credentials
— Required:
type
,
certificate
,
private_key
,
alias
javascript
const pushCredentialResponse = await client.mobilePushCredentials.create({
  createMobilePushCredentialRequest: {
    alias: 'LucyIosCredential',
    certificate:
      '-----BEGIN CERTIFICATE----- MIIGVDCCBTKCAQEAsNlRJVZn9ZvXcECQm65czs... -----END CERTIFICATE-----',
    private_key:
      '-----BEGIN RSA PRIVATE KEY----- MIIEpQIBAAKCAQEAsNlRJVZn9ZvXcECQm65czs... -----END RSA PRIVATE KEY-----',
    type: 'ios',
  },
});

console.log(pushCredentialResponse.data);
Returns:
alias
(string),
certificate
(string),
created_at
(date-time),
id
(string),
private_key
(string),
project_account_json_file
(object),
record_type
(string),
type
(string),
updated_at
(date-time)
POST /mobile_push_credentials
—— 必填参数:
type
certificate
private_key
alias
javascript
const pushCredentialResponse = await client.mobilePushCredentials.create({
  createMobilePushCredentialRequest: {
    alias: 'LucyIosCredential',
    certificate:
      '-----BEGIN CERTIFICATE----- MIIGVDCCBTKCAQEAsNlRJVZn9ZvXcECQm65czs... -----END CERTIFICATE-----',
    private_key:
      '-----BEGIN RSA PRIVATE KEY----- MIIEpQIBAAKCAQEAsNlRJVZn9ZvXcECQm65czs... -----END RSA PRIVATE KEY-----',
    type: 'ios',
  },
});

console.log(pushCredentialResponse.data);
返回参数:
alias
(字符串)、
certificate
(字符串)、
created_at
(日期时间)、
id
(字符串)、
private_key
(字符串)、
project_account_json_file
(对象)、
record_type
(字符串)、
type
(字符串)、
updated_at
(日期时间)

Retrieves a mobile push credential

查询移动推送凭证

Retrieves mobile push credential based on the given
push_credential_id
GET /mobile_push_credentials/{push_credential_id}
javascript
const pushCredentialResponse = await client.mobilePushCredentials.retrieve(
  '0ccc7b76-4df3-4bca-a05a-3da1ecc389f0',
);

console.log(pushCredentialResponse.data);
Returns:
alias
(string),
certificate
(string),
created_at
(date-time),
id
(string),
private_key
(string),
project_account_json_file
(object),
record_type
(string),
type
(string),
updated_at
(date-time)
根据传入的
push_credential_id
查询对应的移动推送凭证
GET /mobile_push_credentials/{push_credential_id}
javascript
const pushCredentialResponse = await client.mobilePushCredentials.retrieve(
  '0ccc7b76-4df3-4bca-a05a-3da1ecc389f0',
);

console.log(pushCredentialResponse.data);
返回参数:
alias
(字符串)、
certificate
(字符串)、
created_at
(日期时间)、
id
(字符串)、
private_key
(字符串)、
project_account_json_file
(对象)、
record_type
(字符串)、
type
(字符串)、
updated_at
(日期时间)

Deletes a mobile push credential

删除移动推送凭证

Deletes a mobile push credential based on the given
push_credential_id
DELETE /mobile_push_credentials/{push_credential_id}
javascript
await client.mobilePushCredentials.delete('0ccc7b76-4df3-4bca-a05a-3da1ecc389f0');
根据传入的
push_credential_id
删除对应的移动推送凭证
DELETE /mobile_push_credentials/{push_credential_id}
javascript
await client.mobilePushCredentials.delete('0ccc7b76-4df3-4bca-a05a-3da1ecc389f0');

List all credentials

列出所有凭证

List all On-demand Credentials.
GET /telephony_credentials
javascript
// Automatically fetches more pages as needed.
for await (const telephonyCredential of client.telephonyCredentials.list()) {
  console.log(telephonyCredential.id);
}
Returns:
created_at
(string),
expired
(boolean),
expires_at
(string),
id
(string),
name
(string),
record_type
(string),
resource_id
(string),
sip_password
(string),
sip_username
(string),
updated_at
(string),
user_id
(string)
列出所有按需凭证
GET /telephony_credentials
javascript
// 按需自动拉取更多页面数据
for await (const telephonyCredential of client.telephonyCredentials.list()) {
  console.log(telephonyCredential.id);
}
返回参数:
created_at
(字符串)、
expired
(布尔值)、
expires_at
(字符串)、
id
(字符串)、
name
(字符串)、
record_type
(字符串)、
resource_id
(字符串)、
sip_password
(字符串)、
sip_username
(字符串)、
updated_at
(字符串)、
user_id
(字符串)

Create a credential

创建凭证

Create a credential.
POST /telephony_credentials
— Required:
connection_id
Optional:
expires_at
(string),
name
(string),
tag
(string)
javascript
const telephonyCredential = await client.telephonyCredentials.create({
  connection_id: '1234567890',
});

console.log(telephonyCredential.data);
Returns:
created_at
(string),
expired
(boolean),
expires_at
(string),
id
(string),
name
(string),
record_type
(string),
resource_id
(string),
sip_password
(string),
sip_username
(string),
updated_at
(string),
user_id
(string)
创建一个新凭证
POST /telephony_credentials
—— 必填参数:
connection_id
可选参数:
expires_at
(字符串)、
name
(字符串)、
tag
(字符串)
javascript
const telephonyCredential = await client.telephonyCredentials.create({
  connection_id: '1234567890',
});

console.log(telephonyCredential.data);
返回参数:
created_at
(字符串)、
expired
(布尔值)、
expires_at
(字符串)、
id
(字符串)、
name
(字符串)、
record_type
(字符串)、
resource_id
(字符串)、
sip_password
(字符串)、
sip_username
(字符串)、
updated_at
(字符串)、
user_id
(字符串)

Get a credential

查询凭证详情

Get the details of an existing On-demand Credential.
GET /telephony_credentials/{id}
javascript
const telephonyCredential = await client.telephonyCredentials.retrieve('550e8400-e29b-41d4-a716-446655440000');

console.log(telephonyCredential.data);
Returns:
created_at
(string),
expired
(boolean),
expires_at
(string),
id
(string),
name
(string),
record_type
(string),
resource_id
(string),
sip_password
(string),
sip_username
(string),
updated_at
(string),
user_id
(string)
获取已有按需凭证的详细信息
GET /telephony_credentials/{id}
javascript
const telephonyCredential = await client.telephonyCredentials.retrieve('550e8400-e29b-41d4-a716-446655440000');

console.log(telephonyCredential.data);
返回参数:
created_at
(字符串)、
expired
(布尔值)、
expires_at
(字符串)、
id
(字符串)、
name
(字符串)、
record_type
(字符串)、
resource_id
(字符串)、
sip_password
(字符串)、
sip_username
(字符串)、
updated_at
(字符串)、
user_id
(字符串)

Update a credential

更新凭证

Update an existing credential.
PATCH /telephony_credentials/{id}
Optional:
connection_id
(string),
expires_at
(string),
name
(string),
tag
(string)
javascript
const telephonyCredential = await client.telephonyCredentials.update('550e8400-e29b-41d4-a716-446655440000');

console.log(telephonyCredential.data);
Returns:
created_at
(string),
expired
(boolean),
expires_at
(string),
id
(string),
name
(string),
record_type
(string),
resource_id
(string),
sip_password
(string),
sip_username
(string),
updated_at
(string),
user_id
(string)
更新已有凭证的信息
PATCH /telephony_credentials/{id}
可选参数:
connection_id
(字符串)、
expires_at
(字符串)、
name
(字符串)、
tag
(字符串)
javascript
const telephonyCredential = await client.telephonyCredentials.update('550e8400-e29b-41d4-a716-446655440000');

console.log(telephonyCredential.data);
返回参数:
created_at
(字符串)、
expired
(布尔值)、
expires_at
(字符串)、
id
(字符串)、
name
(字符串)、
record_type
(字符串)、
resource_id
(字符串)、
sip_password
(字符串)、
sip_username
(字符串)、
updated_at
(字符串)、
user_id
(字符串)

Delete a credential

删除凭证

Delete an existing credential.
DELETE /telephony_credentials/{id}
javascript
const telephonyCredential = await client.telephonyCredentials.delete('550e8400-e29b-41d4-a716-446655440000');

console.log(telephonyCredential.data);
Returns:
created_at
(string),
expired
(boolean),
expires_at
(string),
id
(string),
name
(string),
record_type
(string),
resource_id
(string),
sip_password
(string),
sip_username
(string),
updated_at
(string),
user_id
(string)
删除已有凭证
DELETE /telephony_credentials/{id}
javascript
const telephonyCredential = await client.telephonyCredentials.delete('550e8400-e29b-41d4-a716-446655440000');

console.log(telephonyCredential.data);
返回参数:
created_at
(字符串)、
expired
(布尔值)、
expires_at
(字符串)、
id
(字符串)、
name
(字符串)、
record_type
(字符串)、
resource_id
(字符串)、
sip_password
(字符串)、
sip_username
(字符串)、
updated_at
(字符串)、
user_id
(字符串)