用途
telnyx-webrtc-javascript 是 Telnyx 提供的 JavaScript 技能,用于管理 WebRTC 凭证(credential)和移动推送通知(mobile push)设置。根据资料描述,它适用于构建基于浏览器或移动端的软电话(softphone)应用场景。
安装与初始化
使用 npm 安装 telnyx 包,示例版本为 6.74.2:
npm install telnyx@6.74.2
初始化客户端:
import Telnyx from 'telnyx';
const client = new Telnyx({
apiKey: process.env['TELNYX_API_KEY'], // This is the default and can be omitted
});
资料中的示例均假设 client 已按上述方式初始化。
主要能力
移动推送凭证(mobile_push_credentials)
- 列表:GET /mobile_push_credentials,返回字段包括 alias、certificate、created_at、id、private_key、project_account_json_file、record_type、type、updated_at。
- 创建:POST /mobile_push_credentials,必填字段为 type、certificate、private_key、alias。示例中 type 取值 'ios',并提供 certificate 与 private_key 的 PEM 格式内容。
- 查询:GET /mobile_push_credentials/{push_credential_id},按 ID 获取单个凭证。
- 删除:DELETE /mobile_push_credentials/{push_credential_id}。
电话凭证(telephony_credentials)
- 列表:GET /telephony_credentials,返回字段包括 created_at、expired、expires_at、id、name、record_type、resource_id、sip_password、sip_username、updated_at、user_id。
- 创建:POST /telephony_credentials,必填 connection_id,可选 expires_at、name、tag。
- 查询:GET /telephony_credentials/{id}。
- 更新:PATCH /telephony_credentials/{id},可选 connection_id、expires_at、name、tag。
- 删除:DELETE /telephony_credentials/{id}。
错误处理
资料强调所有 API 调用都可能因网络错误、限流(429)、校验错误(422)或认证错误(401)而失败,生产代码中应始终处理错误。示例使用 try/catch 区分:
- Telnyx.APIConnectionError:网络错误,检查连接并重试。
- Telnyx.RateLimitError:429 限流,可按 retry-after 头等待并使用指数退避重试。
- Telnyx.APIError:其他 API 错误,可根据 status 判断,如 422 为校验错误,需检查必填字段与格式。
常见错误码:401 无效 API key,403 权限不足,404 资源不存在,422 校验错误,429 限流。
分页
资料说明列表方法返回自动分页迭代器,可使用 for await (const item of result) { ... } 自动遍历所有页面,无需手动翻页。
限制说明
资料未提供该技能的收费、价格、适用平台的具体范围、性能效果或额外使用步骤。除上述接口与示例外,其他信息暂未提供。