Beginner APIAutomationPythonNode

🔄Automated ordering and polling with an API Key

A short Python / Node script that automates the full "order a number → poll on a timer → get the code" flow, with timeout and failure-retry handling.

✍️ SimSmsBox 📅 May 25, 2026

Manual curl is fine for debugging, but real workloads need automation. This tutorial gives polling scripts you can use directly.

The idea

  1. Create an order and get the orderId;
  2. Query the status every 2–3 seconds;
  3. Return the code once received;
  4. Cancel the order and retry if the max wait is exceeded.

Python example

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"]
    deadline = time.time() + timeout
    while time.time() < deadline:
        s = requests.get(f"{BASE}/api/sms/orders/{oid}", headers=HEADERS).json()
        if s.get("latestCode"):
            return s["latestCode"]
        time.sleep(3)
    # cancel on timeout (refundable if no code)
    requests.post(f"{BASE}/api/sms/orders/{oid}/cancel", headers=HEADERS)
    raise TimeoutError("code did not arrive before timeout")

print(get_code())

Node.js example

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 } = await res.json();
  const deadline = Date.now() + timeout;
  while (Date.now() < deadline) {
    const s = await (await fetch(`${BASE}/api/sms/orders/${orderId}`, { headers: HEADERS })).json();
    if (s.latestCode) return s.latestCode;
    await sleep(3000);
  }
  await fetch(`${BASE}/api/sms/orders/${orderId}/cancel`, { method: "POST", headers: HEADERS });
  throw new Error("code did not arrive before timeout");
}

Best practices

ItemRecommendation
Polling interval2–3 seconds; too frequent wastes requests
Max wait60–180 seconds, tuned per app
Failure retryCancel the old order, then re-order
ConcurrencyControl concurrency with wallet balance and quota

Codes arrive asynchronously, so patient polling + a reasonable timeout is the key to reliably getting the code.

← Back to Tutorials