短信
短信发送、OTP 与验证,以及投递状态。
概览
基础路径:
/v1/smsPython 命名空间:
infra.smstext
# Python
from infrai import infra
# TypeScript
import { InfraiClient } from "@infrai/sdk";
const client = new InfraiClient({ apiKey: process.env.INFRAI_API_KEY! });
// → client.sms方法
sms.send
POST /v1/sms/send
发送短信;支持模板与 BYOK 供应商。
Python
python
infra.sms.send(to=..., body=..., from_=None, template_id=None, vendor=None, idempotency_key=None)TypeScript
typescript
client.sms.send(opts: SmsSendOptions): Promise<SmsRecord>参数
| 名称 | 类型 | 必填 | 说明 |
|---|---|---|---|
to | string | 必填 | E.164 格式的收件手机号。 |
body | string | 可选 | 短信文本。 |
from | string | 可选 | 发件标识或号码。 |
template_id | string | 可选 | 用于渲染的模板 id(替代正文)。 |
vendor | string | 可选 | 固定使用某个供应商,而非自动路由。 |
idempotency_key | string | 可选 | 可选去重 key;相同重试将返回同一结果。 |
返回
SmsRecord { sms_id, to, state, vendor, segments, created_at }示例
一次性前置(每个范例都假定已完成):
bash
pip install infrai
# one-time auth (no secret needed): anonymous account + trial, writes ~/.infrai/credentials
python -c "from infrai import infra; infra.activate()"
# returning user instead: export INFRAI_API_KEY=ifr_pk_proj_...python
from infrai import infra
msg = infra.sms.send(to="+15555550123", body="Your order has shipped.")
print("sms_id:", msg.get("sms_id"), "| state:", msg.get("state"))sms.otp
POST /v1/sms/otp
向手机号发送一次性验证码。
Python
python
infra.sms.otp(to=...)TypeScript
typescript
// see ROUTES["sms.otp"]参数
| 名称 | 类型 | 必填 | 说明 |
|---|---|---|---|
to | string | 必填 | E.164 格式的收件手机号。 |
返回
{ request_id, expires_at }示例
一次性前置(每个范例都假定已完成):
bash
pip install infrai
# one-time auth (no secret needed): anonymous account + trial, writes ~/.infrai/credentials
python -c "from infrai import infra; infra.activate()"
# returning user instead: export INFRAI_API_KEY=ifr_pk_proj_...python
from infrai import infra
# Send a one-time passcode, then verify the code the user typed.
otp = infra.sms.otp(to="+15555550123")
print("sent:", otp.get("status") or otp.get("state"))
verified = infra.sms.verify(to="+15555550123", code="000000")
print("valid:", verified.get("verified") or verified.get("valid"))sms.verify
POST /v1/sms/verify
校验一次性验证码。
Python
python
infra.sms.verify(to=..., code=...)TypeScript
typescript
// see ROUTES["sms.verify"]参数
| 名称 | 类型 | 必填 | 说明 |
|---|---|---|---|
to | string | 必填 | E.164 格式的收件手机号。 |
code | string | 必填 | 要校验的一次性验证码。 |
返回
{ valid: boolean }示例
一次性前置(每个范例都假定已完成):
bash
pip install infrai
# one-time auth (no secret needed): anonymous account + trial, writes ~/.infrai/credentials
python -c "from infrai import infra; infra.activate()"
# returning user instead: export INFRAI_API_KEY=ifr_pk_proj_...python
from infrai import infra
res = infra.sms.verify(to="+15555550123", code="123456")
print("valid:", res.get("valid") or res.get("verified"))sms.status
GET /v1/sms/{id}
获取短信的投递状态。
Python
python
infra.sms.status(id=sms_id)TypeScript
typescript
client.sms.status(smsId: string): Promise<SmsRecord>参数
| 名称 | 类型 | 必填 | 说明 |
|---|---|---|---|
id | string | 必填 | 短信记录 id。 |
返回
SmsRecord示例
一次性前置(每个范例都假定已完成):
bash
pip install infrai
# one-time auth (no secret needed): anonymous account + trial, writes ~/.infrai/credentials
python -c "from infrai import infra; infra.activate()"
# returning user instead: export INFRAI_API_KEY=ifr_pk_proj_...python
from infrai import infra
rec = infra.sms.status(id="sms_...")
print("state:", rec.get("state"), "| segments:", rec.get("segments"))