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 telnyxbash
npm install telnyxSetup
初始化设置
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 is already initialized as shown above.
clientjavascript
import Telnyx from 'telnyx';
const client = new Telnyx({
apiKey: process.env['TELNYX_API_KEY'], // 这是默认配置,可以省略
});以下所有示例均假设已按上述方式完成初始化。
clientError 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: invalid API key, insufficient permissions,
resource not found, validation error (check field formats),
rate limited (retry with exponential backoff).
401403404422429所有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('验证错误 —— 检查必填字段和格式是否正确');
}
}
}常见错误码: API密钥无效, 权限不足, 资源不存在, 验证错误(检查字段格式), 触发速率限制(使用指数退避策略重试)。
401403404422429Important Notes
重要说明
- Pagination: List methods return an auto-paginating iterator. Use to iterate through all pages automatically.
for await (const item of result) { ... }
- 分页: 列表类方法返回支持自动分页的迭代器,使用即可自动遍历所有页面数据。
for await (const item of result) { ... }
List mobile push credentials
列出移动推送凭证
GET /mobile_push_credentialsjavascript
// Automatically fetches more pages as needed.
for await (const pushCredential of client.mobilePushCredentials.list()) {
console.log(pushCredential.id);
}Returns: (string), (string), (date-time), (string), (string), (object), (string), (string), (date-time)
aliascertificatecreated_atidprivate_keyproject_account_json_filerecord_typetypeupdated_atGET /mobile_push_credentialsjavascript
// 按需自动拉取更多页面数据
for await (const pushCredential of client.mobilePushCredentials.list()) {
console.log(pushCredential.id);
}返回参数: (字符串)、 (字符串)、 (日期时间)、 (字符串)、 (字符串)、 (对象)、 (字符串)、 (字符串)、 (日期时间)
aliascertificatecreated_atidprivate_keyproject_account_json_filerecord_typetypeupdated_atCreates a new mobile push credential
创建新的移动推送凭证
POST /mobile_push_credentialstypecertificateprivate_keyaliasjavascript
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: (string), (string), (date-time), (string), (string), (object), (string), (string), (date-time)
aliascertificatecreated_atidprivate_keyproject_account_json_filerecord_typetypeupdated_atPOST /mobile_push_credentialstypecertificateprivate_keyaliasjavascript
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);返回参数: (字符串)、 (字符串)、 (日期时间)、 (字符串)、 (字符串)、 (对象)、 (字符串)、 (字符串)、 (日期时间)
aliascertificatecreated_atidprivate_keyproject_account_json_filerecord_typetypeupdated_atRetrieves a mobile push credential
查询移动推送凭证
Retrieves mobile push credential based on the given
push_credential_idGET /mobile_push_credentials/{push_credential_id}javascript
const pushCredentialResponse = await client.mobilePushCredentials.retrieve(
'0ccc7b76-4df3-4bca-a05a-3da1ecc389f0',
);
console.log(pushCredentialResponse.data);Returns: (string), (string), (date-time), (string), (string), (object), (string), (string), (date-time)
aliascertificatecreated_atidprivate_keyproject_account_json_filerecord_typetypeupdated_at根据传入的查询对应的移动推送凭证
push_credential_idGET /mobile_push_credentials/{push_credential_id}javascript
const pushCredentialResponse = await client.mobilePushCredentials.retrieve(
'0ccc7b76-4df3-4bca-a05a-3da1ecc389f0',
);
console.log(pushCredentialResponse.data);返回参数: (字符串)、 (字符串)、 (日期时间)、 (字符串)、 (字符串)、 (对象)、 (字符串)、 (字符串)、 (日期时间)
aliascertificatecreated_atidprivate_keyproject_account_json_filerecord_typetypeupdated_atDeletes a mobile push credential
删除移动推送凭证
Deletes a mobile push credential based on the given
push_credential_idDELETE /mobile_push_credentials/{push_credential_id}javascript
await client.mobilePushCredentials.delete('0ccc7b76-4df3-4bca-a05a-3da1ecc389f0');根据传入的删除对应的移动推送凭证
push_credential_idDELETE /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_credentialsjavascript
// Automatically fetches more pages as needed.
for await (const telephonyCredential of client.telephonyCredentials.list()) {
console.log(telephonyCredential.id);
}Returns: (string), (boolean), (string), (string), (string), (string), (string), (string), (string), (string), (string)
created_atexpiredexpires_atidnamerecord_typeresource_idsip_passwordsip_usernameupdated_atuser_id列出所有按需凭证
GET /telephony_credentialsjavascript
// 按需自动拉取更多页面数据
for await (const telephonyCredential of client.telephonyCredentials.list()) {
console.log(telephonyCredential.id);
}返回参数: (字符串)、 (布尔值)、 (字符串)、 (字符串)、 (字符串)、 (字符串)、 (字符串)、 (字符串)、 (字符串)、 (字符串)、 (字符串)
created_atexpiredexpires_atidnamerecord_typeresource_idsip_passwordsip_usernameupdated_atuser_idCreate a credential
创建凭证
Create a credential.
POST /telephony_credentialsconnection_idOptional: (string), (string), (string)
expires_atnametagjavascript
const telephonyCredential = await client.telephonyCredentials.create({
connection_id: '1234567890',
});
console.log(telephonyCredential.data);Returns: (string), (boolean), (string), (string), (string), (string), (string), (string), (string), (string), (string)
created_atexpiredexpires_atidnamerecord_typeresource_idsip_passwordsip_usernameupdated_atuser_id创建一个新凭证
POST /telephony_credentialsconnection_id可选参数: (字符串)、 (字符串)、 (字符串)
expires_atnametagjavascript
const telephonyCredential = await client.telephonyCredentials.create({
connection_id: '1234567890',
});
console.log(telephonyCredential.data);返回参数: (字符串)、 (布尔值)、 (字符串)、 (字符串)、 (字符串)、 (字符串)、 (字符串)、 (字符串)、 (字符串)、 (字符串)、 (字符串)
created_atexpiredexpires_atidnamerecord_typeresource_idsip_passwordsip_usernameupdated_atuser_idGet 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: (string), (boolean), (string), (string), (string), (string), (string), (string), (string), (string), (string)
created_atexpiredexpires_atidnamerecord_typeresource_idsip_passwordsip_usernameupdated_atuser_id获取已有按需凭证的详细信息
GET /telephony_credentials/{id}javascript
const telephonyCredential = await client.telephonyCredentials.retrieve('550e8400-e29b-41d4-a716-446655440000');
console.log(telephonyCredential.data);返回参数: (字符串)、 (布尔值)、 (字符串)、 (字符串)、 (字符串)、 (字符串)、 (字符串)、 (字符串)、 (字符串)、 (字符串)、 (字符串)
created_atexpiredexpires_atidnamerecord_typeresource_idsip_passwordsip_usernameupdated_atuser_idUpdate a credential
更新凭证
Update an existing credential.
PATCH /telephony_credentials/{id}Optional: (string), (string), (string), (string)
connection_idexpires_atnametagjavascript
const telephonyCredential = await client.telephonyCredentials.update('550e8400-e29b-41d4-a716-446655440000');
console.log(telephonyCredential.data);Returns: (string), (boolean), (string), (string), (string), (string), (string), (string), (string), (string), (string)
created_atexpiredexpires_atidnamerecord_typeresource_idsip_passwordsip_usernameupdated_atuser_id更新已有凭证的信息
PATCH /telephony_credentials/{id}可选参数: (字符串)、 (字符串)、 (字符串)、 (字符串)
connection_idexpires_atnametagjavascript
const telephonyCredential = await client.telephonyCredentials.update('550e8400-e29b-41d4-a716-446655440000');
console.log(telephonyCredential.data);返回参数: (字符串)、 (布尔值)、 (字符串)、 (字符串)、 (字符串)、 (字符串)、 (字符串)、 (字符串)、 (字符串)、 (字符串)、 (字符串)
created_atexpiredexpires_atidnamerecord_typeresource_idsip_passwordsip_usernameupdated_atuser_idDelete 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: (string), (boolean), (string), (string), (string), (string), (string), (string), (string), (string), (string)
created_atexpiredexpires_atidnamerecord_typeresource_idsip_passwordsip_usernameupdated_atuser_id删除已有凭证
DELETE /telephony_credentials/{id}javascript
const telephonyCredential = await client.telephonyCredentials.delete('550e8400-e29b-41d4-a716-446655440000');
console.log(telephonyCredential.data);返回参数: (字符串)、 (布尔值)、 (字符串)、 (字符串)、 (字符串)、 (字符串)、 (字符串)、 (字符串)、 (字符串)、 (字符串)、 (字符串)
created_atexpiredexpires_atidnamerecord_typeresource_idsip_passwordsip_usernameupdated_atuser_id