用途:面向构建浏览器端或移动端软电话(softphone)应用的开发者,通过 Telnyx JavaScript SDK 管理 WebRTC 相关的电话凭证(On-demand Credentials)和移动推送通知凭证,用于软电话注册、鉴权及 iOS/Android 推送配置。
安装与初始化:
npm install telnyx
import Telnyx from 'telnyx'; const client = new Telnyx({ apiKey: process.env['TELNYX_API_KEY'] });
主要能力(对应 REST 接口的 SDK 封装): 电话凭证:列表 GET /telephony_credentials、创建 POST /telephony_credentials(必填 connection_id,可选 expires_at、name、tag)、获取 GET /telephony_credentials/{id}、更新 PATCH /telephony_credentials/{id}、删除 DELETE /telephony_credentials/{id},返回字段含 sip_username、sip_password、expired、expires_at、resource_id、user_id 等。 移动推送凭证:列表 GET /mobile_push_credentials、创建 POST /mobile_push_credentials(必填 type、certificate、private_key、alias)、获取 GET /mobile_push_credentials/{push_credential_id}、删除 DELETE /mobile_push_credentials/{push_credential_id},返回字段含 alias、certificate、private_key、record_type 等。
用法要点:列表方法返回自动分页迭代器,可用 for await (const item of result) { ... } 逐页遍历。创建电话凭证示例:client.telephonyCredentials.create({ connection_id: '1234567890' })。
错误处理:调用可能返回 401 无效 API Key、403 权限不足、404 资源不存在、422 校验错误、429 限流(建议指数退避重试)。SDK 提供 APIConnectionError、RateLimitError、APIError 等类型,429 时可读取 err.headers?.['retry-after'] 后重试。
限制:仅覆盖凭证与推送设置的 API 操作;实际音视频通话流程及其他产品计费、定价信息资料暂未提供。