Automate SMS verification with the SNSTokens API
The SNSTokens API gives your scripts and test suites the same numbers, prices and refund rules as the website. This guide covers a full flow and the details that keep it reliable.
The SNSTokens API is a REST API at https://snstokens.com/api/v1 that lets your code buy virtual numbers, read SMS verification codes, cancel orders and start rentals with the same prices and refund rules as the website.
What the API is for
Automating SMS verification saves time whenever you need codes regularly. QA teams use it to test sign-up flows in different countries, developers use it to check how their own app's SMS looks in each market, and agencies use it to set up work accounts without copying numbers by hand.
The API covers both products. One-time numbers receive codes from 1 app for 20 minutes, and rentals keep a number for 3, 14 or 30 days with SMS from any app. Everything is paid from the same account credit you use on the website, and the full endpoint reference lives on the API page.
Before you start, add credit to your account. The minimum top-up is $40, and payment runs through PayGate checkout with 23 cryptocurrency options. Your script can check the balance at any time, so you can alert yourself before it runs low.
API keys and authentication
Create an API key in the console of your SNSTokens account. Every request sends it in a header: Authorization: Bearer <API key>. Requests without a valid key return the error code bad_key.
The API key is separate from your account key. Your account key is your login on the website, while the API key is meant for code. Keeping them separate means you can give a server or a CI job access to the API without sharing the login to the whole account.
Use HTTPS for every call and send the key only in the header, never in a URL or query string, where it can end up in logs.
The endpoints you will use
The base URL is https://snstokens.com/api/v1. All responses are JSON. These are the endpoints for a typical flow:
GET /balancereturns your current credit.GET /servicesreturns the apps and websites you can order, each with its slug.GET /prices?service=<slug>&country=<code>returns the price for an app and country.POST /numberswithserviceandcountrybuys a one-time number and returns its id and phone number.GET /numbers/{id}returns the order status and any codes received.POST /numbers/{id}/cancelcancels a one-time number before the first code arrives.POST /rentalswithcountryanddaysstarts a rental.GET /rentals/{id}returns the rental status and its messages.
Cache the result of GET /services instead of calling it before every order. The list of 1,000 apps and websites changes rarely, and caching keeps your request budget for the calls that matter.
A full one-time verification flow
A typical automated verification follows the same order as a manual order on the website, written as code.
- Call
GET /pricesfor the app and country to confirm the price, andGET /balanceto confirm you have enough credit. - Call
POST /numberswith the service slug and country code, and store the returned id and phone number. - Enter the phone number in the app you are verifying, through your test tool or automation.
- Poll
GET /numbers/{id}until the status is Code received, then read the code from the response. - Type the code into the app and mark the run as finished.
If the balance is too low, POST /numbers returns low_balance. A wrong slug returns bad_service, and an unsupported country returns bad_country. Checking prices first avoids most of these cases. You can also compare prices by hand in the price list.
Polling without wasting requests
The API returns the order state when you ask, so your code polls GET /numbers/{id} to see when the SMS arrives. The order statuses match the website: Waiting for SMS, Code received, Refunded and Cancelled.
Poll at a steady interval, for example every 5 seconds per order. Most codes arrive within the first couple of minutes, so a faster interval adds requests without getting the code sooner. If you run many orders at once, spread the polls out so the total stays within your limit.
- Stop polling as soon as the status is Code received, Refunded or Cancelled.
- Keep polling the same order if your flow expects a second code from the same app, since further codes in the 20 minutes are included at no extra cost.
- Log every status change with the order id, which makes later debugging much easier.
Timeouts, cancel and refunds
Each one-time number is open for 20 minutes. If no code arrives within that time, the full price returns to your credit automatically and the status becomes Refunded. Your code does not need to request the refund; it only needs to notice the status.
You can also stop earlier. Many teams set their own timeout shorter than 20 minutes, for example a few minutes, and then call POST /numbers/{id}/cancel. A cancel before the first code returns the full price to credit straight away and sets the status to Cancelled. Your script can then order a new number, perhaps in a different country.
Once a code has arrived, the number is paid for and cannot be cancelled. A cancel call at that point returns code_received. Treat this as a success signal rather than a failure: read the code and carry on. Rentals are a fixed-period fee with no refund once started and no early cancel, so your code should only start a rental when it truly needs the number for days. The refund policy has the full rules.
Rate limit and error handling
The limit is 60 requests per minute per API key. Above that, the API returns rate_limited. When you see it, pause before the next call and slow your polling interval; retrying instantly only extends the wait.
Every error is JSON with an error code, so your code can branch on the code rather than on message text:
bad_key: the key is missing or not valid. Stop and check your configuration.low_balance: not enough credit for this order. Top up before retrying.not_found: the order or rental id does not exist on this key's account.code_received: the order already has a code, so it cannot be cancelled.bad_serviceorbad_country: the slug or country code is not valid for this request.rate_limited: too many requests in the last minute. Wait and retry more slowly.network_error: a temporary issue reaching the service. Retry with a short delay that grows each time.
Make retries safe. Retrying a GET is always fine, but before retrying a POST /numbers after a network_error, call GET /balance to see whether the price was already taken, so you do not buy a second number by accident.
Keeping keys safe
Treat an API key like a password for your credit. Store it in an environment variable or a secrets manager, not in source code, and make sure it never appears in logs, error reports or screenshots.
- Use a separate key for each server or project where the console allows it, so a change to 1 key leaves the others untouched.
- If a key may have been exposed, create a new key in the console at once and stop using the old key.
- Keep the account key, your login, out of automation entirely and store it offline.
- Watch the balance with
GET /balanceand alert on unexpected drops.
Use the API for testing, privacy and work accounts in line with each app's own rules and the SNSTokens acceptable use policy. For questions about manual orders, the guide on how to receive an SMS verification code online explains the same flow on the website.
Frequently asked questions
Does the API use the same prices as the website?
How often should I poll for a code?
Do I need to request a refund through the API?
POST /numbers/{id}/cancel before the first code for an immediate full refund.What does the error code `code_received` mean?
GET /numbers/{id} and continue your flow.Can I start a number rental through the API?
POST /rentals with the country and the number of days (3, 14 or 30), then read messages with GET /rentals/{id}. Rentals are available in 12 countries.