Manual curl is fine for debugging, but real workloads need automation. This tutorial gives polling scripts you can use directly.
The idea
- Create an order and get the
orderId; - Query the status every 2–3 seconds;
- Return the code once
received; - 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
| Item | Recommendation |
|---|---|
| Polling interval | 2–3 seconds; too frequent wastes requests |
| Max wait | 60–180 seconds, tuned per app |
| Failure retry | Cancel the old order, then re-order |
| Concurrency | Control concurrency with wallet balance and quota |
Codes arrive asynchronously, so patient polling + a reasonable timeout is the key to reliably getting the code.