手动 curl 适合调试,真实业务需要自动化。本教程给出可直接套用的轮询脚本。
思路
- 创建订单,拿到
orderId与取码地址凭证apiBindingKey; - 用取码地址
/api/sms/record?key=<apiBindingKey>&format=txt每 2~3 秒拉一次(该地址在订单有效期内一直有效、无需鉴权头); - 返回
YES|<验证码>即拿到码; - 超过最长等待时间则取消订单并重试。
Python 示例
import time, requests
BASE = "https://api.simsmsbox.com"
HEADERS = {"X-API-Key": "psk_xxxxxxxx"}
def get_code(service="telegram", country="US", timeout=180):
r = requests.post(f"{BASE}/api/sms/orders/purchase",
headers=HEADERS,
json={"service": service, "country": country, "cardKind": "physical", "rentDays": 30})
order = r.json()
oid = order["orderId"]
key = order["apiBindingKey"] # 取码地址凭证,订单有效期内一直有效
deadline = time.time() + timeout
while time.time() < deadline:
# 取码地址:无需 X-API-Key,key 即授权;format=txt 返回 YES|<码> 或 NO|
resp = requests.get(f"{BASE}/api/sms/record",
params={"key": key, "format": "txt"}).text.strip()
if resp.startswith("YES|"):
return resp.split("|", 1)[1]
time.sleep(3)
# 超时则取消(未收码可退款)
requests.post(f"{BASE}/api/sms/orders/{oid}/cancel", headers=HEADERS)
raise TimeoutError("验证码超时未到达")
print(get_code())
Node.js 示例
const BASE = "https://api.simsmsbox.com";
const HEADERS = { "X-API-Key": "psk_xxxxxxxx", "Content-Type": "application/json" };
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
async function getCode(service = "telegram", country = "US", timeout = 180000) {
const res = await fetch(`${BASE}/api/sms/orders/purchase`, {
method: "POST", headers: HEADERS,
body: JSON.stringify({ service, country, cardKind: "physical", rentDays: 30 }),
});
const { orderId, apiBindingKey } = await res.json();
const deadline = Date.now() + timeout;
while (Date.now() < deadline) {
// 取码地址:无需鉴权头,key 即授权
const txt = await (await fetch(`${BASE}/api/sms/record?key=${apiBindingKey}&format=txt`)).text();
if (txt.startsWith("YES|")) return txt.slice(4).trim();
await sleep(3000);
}
await fetch(`${BASE}/api/sms/orders/${orderId}/cancel`, { method: "POST", headers: HEADERS });
throw new Error("验证码超时未到达");
}
最佳实践
| 项目 | 建议 |
|---|---|
| 取码方式 | 首选取码地址 /api/sms/record(一直有效、无需鉴权头);GET /orders/{id} 作兜底 |
| 轮询间隔 | 2~3 秒,过密会浪费请求 |
| 最长等待 | 60~180 秒,按应用调整 |
| 失败重试 | 取消旧单后再重新取号 |
| 并发 | 用钱包余额与配额控制并发量 |
取码地址在订单有效期内一直有效、无需鉴权头,用它轮询最省事;订单查询接口可作兜底。进一步看自定义取码 URL 与模板化返回。