用途
Telnyx Verify - JavaScript 是 Telnyx 官方提供的 JavaScript 技能,围绕电话号码验证与号码信息查询两大用途展开。它通过 telnyx npm 包调用 Telnyx 的 Verify 与 Number Lookup 接口,可用于用户手机号验证(OTP)以及电话号码数据补充。
安装与初始化
安装命令:
npm install telnyx
初始化示例:
import Telnyx from 'telnyx';
const client = new Telnyx({
apiKey: process.env['TELNYX_API_KEY'], // This is the default and can be omitted
});
文档中所有示例均假设 client 已按上述方式初始化。
主要能力
电话号码信息查询
调用 client.numberLookup.retrieve('+18665552368'),对应接口 GET /number_lookup/{phone_number}。返回字段包括:caller_name(对象)、carrier(对象)、country_code(字符串)、fraud(字符串或 null)、national_format(字符串)、phone_number(字符串)、portability(对象)、record_type(字符串)。
验证(OTP)相关操作
- 按电话号码列出验证记录:
GET /verifications/by_phone_number/{phone_number},返回 created_at、custom_code、id、phone_number、record_type、status(pending、accepted、invalid、expired、error)、timeout_secs、type(sms、call、flashcall)、updated_at、verify_profile_id。 - 按电话号码校验验证码:
POST /verifications/by_phone_number/{phone_number}/actions/verify,必填 code、verify_profile_id,返回 phone_number 与 response_code(accepted、rejected)。 - 触发语音验证:
POST /verifications/call,必填 phone_number、verify_profile_id,可选 custom_code、extension、timeout_secs。 - 触发闪信验证:
POST /verifications/flashcall,必填 phone_number、verify_profile_id,可选 timeout_secs。 - 触发短信验证:
POST /verifications/sms,必填 phone_number、verify_profile_id,可选 custom_code、timeout_secs。 - 按 ID 获取验证记录:
GET /verifications/{verification_id}。 - 按 ID 校验验证码:
POST /verifications/{verification_id}/actions/verify,可选 code、status(accepted、rejected)。
Verify 配置文件管理
- 列出全部配置文件:
GET /verify_profiles,返回 call、created_at、flashcall、id、language、name、rcs、record_type、sms、updated_at、webhook_failover_url、webhook_url。列表方法返回自动分页迭代器,可用for await (const item of result) { ... }自动遍历所有页。 - 创建配置文件:
POST /verify_profiles,必填 name,可选 call、flashcall、language、rcs、sms、webhook_failover_url、webhook_url。 - 获取单个配置文件:
GET /verify_profiles/{verify_profile_id}。 - 更新配置文件:
PATCH /verify_profiles/{verify_profile_id},可选 call、flashcall、language、name、rcs、sms、webhook_failover_url、webhook_url。 - 删除配置文件:
DELETE /verify_profiles/{verify_profile_id}。
消息模板管理
- 列出模板:
GET /verify_profiles/templates,返回 id、text。 - 创建模板:
POST /verify_profiles/templates,必填 text,例如Your {{app_name}} verification code is: {{code}}.。 - 更新模板:
PATCH /verify_profiles/templates/{template_id},必填 text。
使用方式与错误处理
所有 API 调用都可能因网络错误、限流(429)、校验错误(422)或认证错误(401)而失败,生产代码中应始终处理错误。文档给出的处理模式为:捕获 Telnyx.APIConnectionError(网络错误,检查连通性并重试)、Telnyx.RateLimitError(429,按 retry-after 头等待后以指数退避重试)、Telnyx.APIError(输出状态码与消息,422 时检查必填字段与格式)。常见错误码:401 无效 API 密钥、403 权限不足、404 资源不存在、422 校验错误、429 限流。
适用场景
适用于需要电话号码验证(短信、语音、闪信 OTP)与电话号码数据补充(运营商、号码类型、来电显示名称等)的 JavaScript 应用。
限制与注意事项
电话号码必须为 E.164 格式(例如 +13125550001),包含 + 前缀与国家代码,不能有空格、短横线或括号。列表方法返回自动分页迭代器。关于收费、价格、平台支持范围等信息,资料中暂未提供。