入门 API自动化PythonNode

🔄使用 API Key 实现自动取号与轮询

用一段 Python / Node 脚本实现「取号 → 定时轮询 → 拿到验证码」的完整自动化,并处理超时与失败重试。

✍️ SimSmsBox 📅 2026年5月25日

手动 curl 适合调试,真实业务需要自动化。本教程给出可直接套用的轮询脚本。

思路

  1. 创建订单,拿到 orderId 与取码地址凭证 apiBindingKey
  2. 取码地址 /api/sms/record?key=<apiBindingKey>&format=txt 每 2~3 秒拉一次(该地址在订单有效期内一直有效、无需鉴权头);
  3. 返回 YES|<验证码> 即拿到码;
  4. 超过最长等待时间则取消订单并重试。

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 与模板化返回

← 返回教程列表