Free Numbers Pricing Reviews Feedback Terms & Policy API Docs Blog About
Log in Get Started
6 views 6 min read
Back to Blog
API

81. OTP Verification API in Python: Order a Number and Read the Code

81. OTP Verification API in Python: Order a Number and Read the Code

Building an OTP verification API in Python is one of the most practical ways to automate account testing, signup flows, and quality assurance. Instead of ordering a number in a browser and refreshing an inbox, your script can request a virtual number, trigger the code, and read the message as structured data. This guide walks through the full workflow with SMS Feel, from API key to parsed code.

Direct answer: Use the SMS Feel API to order a virtual number, submit that number to the service you are testing, then poll the SMS Feel inbox endpoint until the verification message arrives. In Python, store the API key in an environment variable, use the requests library, and extract the code with a regular expression that matches four to eight digits.

What an OTP verification API does

The API acts as a bridge between your code and a real mobile number. You send an order request, the platform allocates a number from the selected country, and any SMS sent to that number is stored for retrieval. A good OTP API exposes three core actions: order a number, list incoming messages, and release the number when the job is done. Some platforms also offer webhooks, which push the message to your server instead of making your script poll.

For Python developers, this pattern fits naturally into scripts and CI pipelines. You can wrap the three calls in a small client class, add retries, and reuse the same logic across dozens of test cases.

Why Python is a good fit for SMS OTP workflows

Python is readable, has excellent HTTP libraries, and runs almost anywhere. A verification script rarely needs a heavy framework; the requests library plus the standard library is enough. If you already use pytest or unittest, you can turn an OTP check into an assertion and run it every time you deploy.

Python also makes text parsing straightforward. Verification messages vary, but most contain a four to eight digit code. A regular expression that matches a standalone digit sequence catches the common cases. For messages with words like code or OTP, you can narrow the pattern and reduce false matches.

Prerequisites before you order a number

  • A SMS Feel account created through the SMS Feel registration page.
  • An API key with permission to order numbers and read messages.
  • Python 3.8 or newer and the requests package installed.
  • A target service that accepts a virtual number for verification.
  • A clear policy for lawful testing and data handling.

Keep the API key out of source control. Store it in an environment variable such as SMSFEEL_API_KEY and read it with os.environ. If the key is exposed, rotate it immediately.

Step-by-step: order a number and read the code

  1. Authenticate your request. Send the API key in the header defined in the SMS Feel API documentation. Do not place secrets in the URL.
  2. Order a number. Call the order endpoint with the country and service you need. The response returns a number identifier and the phone number itself.
  3. Submit the number to the target service. Use the returned number in the signup or login form that triggers the OTP.
  4. Poll the inbox. Call the message endpoint with the order identifier every few seconds. Stop after a sensible timeout, for example 120 seconds.
  5. Parse the code. Search the message text for a digit sequence. Prefer the sequence nearest to words like code, OTP, or verification.
  6. Validate and release. Check that the code has the expected length, then release the number so it can be reused by the platform.

A simple polling loop should sleep between requests. Aggressive polling wastes quota and can trigger rate limits. Start with a five second interval and back off if the API returns a rate-limit status.

Handling errors and timeouts

Network errors, expired numbers, and delayed SMS are normal in verification work. Your script should distinguish between a temporary failure and a permanent one. A connection timeout is worth retrying with exponential backoff. An invalid API key or a rejected order is not.

ErrorLikely causeAction
401 UnauthorizedMissing or wrong API keyCheck the header and rotate the key if needed
429 Too Many RequestsPolling too quicklyIncrease the sleep interval and add backoff
No message after timeoutService rejected the number or SMS delayedRelease and retry with a different number
Invalid numberWrong country or service mappingCheck the order parameters

Log the order identifier, the target service, and the response status, but never log the full API key or the complete verification message. If a code does not arrive, release the number and try a different country or service rather than reusing a failed order.

Security and compliance rules

Use the API only for accounts you own or are authorised to test. Do not use virtual numbers to create fake accounts, bypass platform limits, or hide identity for fraud. Many services prohibit disposable numbers, and violating their terms can lead to a permanent ban. SMS Feel is intended for privacy, testing, and development, so keep the use case within those boundaries.

Store verification codes only as long as needed. If you are testing a production system, mask the number and the code in logs. Review the platform terms of the target service before you automate verification.

Frequently Asked Questions

Can I use an OTP verification API in Python for free?

SMS Feel may offer trial credit or a limited free tier, but virtual numbers usually consume balance. Check the current pricing and trial terms on the SMS Feel site before you build a large test suite.

How long does it take to receive an OTP?

Most codes arrive within a few seconds to a minute. Delivery depends on the target service and the mobile network. Build a timeout of at least two minutes and allow one retry with a fresh number.

Why does my script receive an old message?

Some numbers receive multiple messages, including marketing SMS. Filter by sender, timestamp, or keywords such as OTP and verification. Always read the newest message for the current order.

Can I avoid polling and use webhooks instead?

Yes, if the API supports webhooks. A webhook pushes each message to your server, which reduces latency and API calls. You still need a fallback poll in case a delivery is delayed.

Is a virtual number allowed on every platform?

No. Banks, payment apps, and some marketplaces reject virtual or VoIP numbers. Read the target platform rules first. Using a number that the platform prohibits can get the account restricted.

Conclusion

An OTP verification API in Python removes manual copying and makes verification testing repeatable. Order a number, submit it, poll for the message, parse the code, and release the number. Start with the SMS Feel account setup, follow the developer documentation, and keep every test lawful and authorised.

Related reading: explore more API and automation guides on the SMS Feel blog.

Tags

#otp verification api python #python otp api #virtual number api #sms verification api #otp automation #python requests
Contact us