|
| 1 | +# smscode |
| 2 | + |
| 3 | +Official Python SDK for the SMSCode virtual-number API. |
| 4 | + |
| 5 | +Use it to rent temporary phone numbers, receive SMS OTP verification codes, and |
| 6 | +manage order lifecycle from Python services, bots, and automations. |
| 7 | + |
| 8 | +## Install |
| 9 | + |
| 10 | +```bash |
| 11 | +pip install smscode |
| 12 | +``` |
| 13 | + |
| 14 | +Requires Python 3.10+. |
| 15 | + |
| 16 | +## Quick start |
| 17 | + |
| 18 | +`SmscodeClient` uses the USD-native `/v2` API by default. Money values are typed |
| 19 | +objects with the exact IDR ledger amount preserved as `canonical_amount`. |
| 20 | + |
| 21 | +```py |
| 22 | +import os |
| 23 | + |
| 24 | +from smscode import OtpTimeoutError, OrderTerminalError, SmscodeClient |
| 25 | + |
| 26 | +client = SmscodeClient(token=os.environ["SMSCODE_TOKEN"]) |
| 27 | + |
| 28 | +body = { |
| 29 | + "catalog_product_id": int(os.environ["SMSCODE_CATALOG_PRODUCT_ID"]), |
| 30 | + "max_price": "0.50", # /v2 uses a USD decimal string, never a float |
| 31 | + "quantity": 1, |
| 32 | +} |
| 33 | + |
| 34 | +with client: |
| 35 | + created = client.orders.create(body) |
| 36 | + order = created.orders[0] |
| 37 | + order_id = int(order["id"]) |
| 38 | + |
| 39 | + try: |
| 40 | + otp = client.orders.wait_for_otp(order_id, timeout_ms=120_000) |
| 41 | + print("OTP:", otp.otp_code) |
| 42 | + # Submit otp.otp_code in your target app here. |
| 43 | + client.orders.finish(order_id) |
| 44 | + except (OtpTimeoutError, OrderTerminalError): |
| 45 | + # No OTP evidence arrived. Cancel remains available only in that case. |
| 46 | + client.orders.cancel(order_id) |
| 47 | +``` |
| 48 | + |
| 49 | +## Async client |
| 50 | + |
| 51 | +The async client has the same surface and uses `httpx.AsyncClient` internally. |
| 52 | + |
| 53 | +```py |
| 54 | +import os |
| 55 | + |
| 56 | +from smscode import AsyncSmscodeClient |
| 57 | + |
| 58 | + |
| 59 | +async def main() -> None: |
| 60 | + async with AsyncSmscodeClient(token=os.environ["SMSCODE_TOKEN"]) as client: |
| 61 | + balance = await client.balance.get() |
| 62 | + print(balance.balance.amount, balance.balance.currency) |
| 63 | +``` |
| 64 | + |
| 65 | +## Resend and wait for a new OTP |
| 66 | + |
| 67 | +`finish` does not require a new OTP after resend; the order is finishable once it |
| 68 | +has OTP evidence. If your integration needs to wait for a different post-resend |
| 69 | +code, pass the previous code as `after_code`. |
| 70 | + |
| 71 | +```py |
| 72 | +first = client.orders.wait_for_otp(order_id) |
| 73 | + |
| 74 | +client.orders.resend(order_id) |
| 75 | + |
| 76 | +second = client.orders.wait_for_otp( |
| 77 | + order_id, |
| 78 | + after_code=first.otp_code, |
| 79 | + timeout_ms=120_000, |
| 80 | +) |
| 81 | + |
| 82 | +print("new OTP:", second.otp_code) |
| 83 | +# Submit second.otp_code in your target app here, then finish. |
| 84 | +client.orders.finish(order_id) |
| 85 | +``` |
| 86 | + |
| 87 | +If the provider sends the same digits again, code-based polling cannot |
| 88 | +distinguish it from the previous OTP. |
| 89 | + |
| 90 | +## Idempotent order create |
| 91 | + |
| 92 | +Order create is money-sensitive. The SDK resolves an idempotency key before the |
| 93 | +request, sends it as `idempotency-key`, and attaches it to create errors. |
| 94 | + |
| 95 | +```py |
| 96 | +from smscode import SmscodeError |
| 97 | + |
| 98 | +try: |
| 99 | + created = client.orders.create(body) |
| 100 | +except SmscodeError as err: |
| 101 | + if err.idempotency_key is None: |
| 102 | + raise |
| 103 | + # Retry the exact same body with the same key. Never mint a fresh key for |
| 104 | + # the same attempted create. |
| 105 | + created = client.orders.create(body, idempotency_key=err.idempotency_key) |
| 106 | +``` |
| 107 | + |
| 108 | +## Webhooks |
| 109 | + |
| 110 | +Verify webhook signatures against the raw request body before parsing JSON. |
| 111 | + |
| 112 | +```py |
| 113 | +from smscode import parse_webhook_event, verify_webhook_signature |
| 114 | + |
| 115 | + |
| 116 | +def handle_webhook(raw_body: bytes, signature_header: str | None, secret: str) -> int: |
| 117 | + if not verify_webhook_signature(raw_body, signature_header or "", secret): |
| 118 | + return 401 |
| 119 | + |
| 120 | + event = parse_webhook_event(raw_body) |
| 121 | + if event["event"] == "order.otp_received": |
| 122 | + print(event["data"]["otp_code"]) |
| 123 | + return 204 |
| 124 | +``` |
| 125 | + |
| 126 | +## `/v1` namespace |
| 127 | + |
| 128 | +Use `.v1` only when you intentionally want legacy IDR-only shapes. |
| 129 | + |
| 130 | +```py |
| 131 | +with SmscodeClient(token=os.environ["SMSCODE_TOKEN"]) as client: |
| 132 | + balance_v2 = client.balance.get() |
| 133 | + balance_v1 = client.v1.balance.get() |
| 134 | +``` |
| 135 | + |
| 136 | +## Error handling |
| 137 | + |
| 138 | +Every API error is a typed `SmscodeError` subclass. Branch on the class or |
| 139 | +`err.code`, not on `err.message`. `RateLimitError` and retryable server errors |
| 140 | +carry `retry_after_seconds` when the API sends `Retry-After`. |
| 141 | + |
| 142 | +## License |
| 143 | + |
| 144 | +MIT |
0 commit comments