用途
Telnyx Verify - JavaScript 面向需要在 JavaScript 服务端集成电话号码数据查询与用户验证能力的开发者。它同时覆盖两类任务:电话号码信息查询(数据补全),以及通过多种通道下发验证码并校验用户提交的验证码(用户验证)。
安装与初始化
通过 npm 安装指定版本的 SDK:
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.numberLookup.retrieve 可获取指定电话号码的信息,接口路径为 GET /number_lookup/{phone_number}。返回字段包括 caller_name、carrier、country_code、fraud、national_format、phone_number、portability、record_type。
验证(OTP)流程
支持多种验证触发方式,均以 verify_profile_id 为必填项:
- 短信验证:
POST /verifications/sms,调用client.verifications.triggerSMS - 语音呼叫验证:
POST /verifications/call,调用client.verifications.triggerCall - Flash call 验证:
POST /verifications/flashcall,调用client.verifications.triggerFlashcall - WhatsApp 验证:
POST /verifications/whatsapp,调用client.verifications.triggerWhatsappVerification
各触发接口返回验证记录对象,包含 created_at、custom_code、id、phone_number、record_type、status(pending/accepted/invalid/expired/error)、timeout_secs、type(sms/call/flashcall/whatsapp)、updated_at、verify_profile_id。
验证码校验支持两种方式:
- 按电话号码:
POST /verifications/by_phone_number/{phone_number}/actions/verify,必填 code、verify_profile_id,返回 phone_number 与 response_code(accepted/rejected)。 - 按验证 ID:
POST /verifications/{verification_id}/actions/verify,可选 code、status,返回 phone_number 与 response_code。
此外可查询验证记录:按电话号码 GET /verifications/by_phone_number/{phone_number},或按 ID GET /verifications/{verification_id}。
Verify profile 管理
支持完整 CRUD 操作:列表 GET /verify_profiles(返回自动分页迭代器)、创建 POST /verify_profiles(必填 name)、检索 GET /verify_profiles/{verify_profile_id}、更新 PATCH /verify_profiles/{verify_profile_id}、删除 DELETE /verify_profiles/{verify_profile_id}。Profile 字段包括 call、created_at、daily_spend_limit、daily_spend_limit_enabled、flashcall、id、language、name、record_type(verification_profile)、sms、updated_at、webhook_failover_url、webhook_url、whatsapp。
消息模板管理
支持检索模板 GET /verify_profiles/templates、创建模板 POST /verify_profiles/templates(必填 text,示例:Your {{app_name}} verification code is: {{code}}.)、更新模板 PATCH /verify_profiles/templates/{template_id}(必填 text)。
使用方式与错误处理
电话号码必须使用 E.164 格式(例如 +13125550001),包含 + 前缀与国家代码,不包含空格、短划线或括号。列表方法返回自动分页迭代器,可用 for await (const item of result) { ... } 遍历所有页。
所有 API 调用需处理错误:网络错误(Telnyx.APIConnectionError)、限流(Telnyx.RateLimitError,429,可读取 retry-after 头并以指数退避重试)、其他 API 错误(Telnyx.APIError)。常见错误码:401 无效 API 密钥、403 权限不足、404 资源未找到、422 校验错误(检查字段格式)、429 限流。
适用场景
电话号码数据补全、用户注册或登录时的短信/语音/Flash call/WhatsApp OTP 验证,以及需要管理验证配置与消息模板的服务端应用。
限制
确切的收费、价格、套餐、平台支持范围、性能指标及使用限制在所提供的资料中暂未提供。