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

OTP Verification API in Node.js: Order a Number and Read the Code

OTP Verification API in Node.js: Order a Number and Read the Code

Node.js is a natural fit for verification flows because the work is mostly waiting on network calls. An OTP verification API in Node.js integration is a small module: order a number, poll for the message, extract the code and release the number. The interesting parts are the polling budget, the error handling and the cleanup that runs even when something throws. This guide builds all three with patterns you can copy directly.

Direct answer: To use the OTP verification API in Node.js, send a POST to order a number, poll the message endpoint with the returned identifier until a code arrives, extract it with a service-specific pattern, and send a release request in a finally block. Use a shared HTTP client with timeouts on every call.

Setting Up the Node.js Client

  1. Use a built-in fetch or a small HTTP client, not a large framework.
  2. Read the API key and base URL from environment variables.
  3. Create a reusable client with a connect timeout and a response timeout.
  4. Wrap the calls in a module with one exported function per endpoint.
  5. Return plain objects and throw typed errors rather than returning nulls.

A single client module keeps the key in one place and makes the flow easy to test with a mock.

Ordering a Number

The order call takes the country, the service you are verifying and optionally a number type. Send it as JSON and check the status before parsing the body.

  • 200: number ready; read the identifier and the number.
  • 202: accepted and provisioning; poll the status endpoint.
  • 402: insufficient balance; stop and top up.
  • 429: rate limited; back off before retrying.

Persist the identifier before you do anything else. A process that orders a number and then crashes without storing the identifier has spent money for nothing.

Polling for the Code

Polling is a loop with three exit conditions: a message arrived, the budget expired or the number is gone. Write it explicitly rather than with a fixed sleep.

  1. Set a maximum number of attempts and a delay between them.
  2. Fetch the message endpoint with the identifier.
  3. If the response is empty, wait and try again.
  4. If a message is present, break and parse it.
  5. If attempts run out, throw an error that includes the identifier.

Two to three seconds between attempts with a ninety-second budget is a sensible default for a user waiting on a screen.

Parsing and Handling the Code

A generic four-to-eight digit regex will match dates and order numbers. Write a pattern per service, anchored to the verification phrase, and validate the length against the message. Hash the code if you need to compare later, and never log the plain value.

Mark the code consumed on success so a replay inside the expiry window fails.

Releasing the Number

Send the release request in a finally block so it runs whether the flow succeeded or threw. In Node.js a try/finally around the await chain is enough. Abandoned numbers leak inventory and skew your usage reporting.

Releasing is also a testing convenience. A released number cannot receive a stray message from a later run, so assertions stay deterministic.

Error Handling in Async Code

SituationWhat to logWhat to do
Empty inbox after budgetIdentifier, attempts, timestampsThrow a not-ready error and offer a reorder
402 responseIdentifier, endpointTop up and retry once
404 responseIdentifier, timestampOrder a new number
Network errorEndpoint, error typeRetry with backoff within budget

Testing and Running It

Write three tests first: a missing key throws, an empty inbox returns a not-ready error, and a message with a six-digit code produces the expected value. Then run one real flow in staging with an account from the SMS Feel registration page and confirm the parsed output against the SMS Feel developer documentation.

Check the wallet balance before a long run. A top-up mid-test turns a five-minute job into an afternoon.

Frequently Asked Questions

Which HTTP client should I use in Node.js?

Built-in fetch is sufficient for most flows. If you need connection pooling or retries, a small dedicated client works well.

How do I avoid duplicate orders after a timeout?

Send an idempotency key per order intent and reuse it across retries. A repeated request then returns the original result instead of ordering again.

Can I run this in a serverless function?

Yes, but keep the polling budget short and move long waits to a queue. Serverless timeouts are unforgiving of a ninety-second loop.

Why does the message endpoint return nothing for a while?

Either the sending service is slow or the number range is filtered. Check the order status, then try a different country.

Is the same module usable for temporary email?

The shape is similar, but the endpoints and fields differ. Read the documentation for the email flow before reusing the code.

Conclusion

An OTP verification API in Node.js module is four calls and a careful loop: order, poll, parse, release. Put the release in a finally block, keep the parsing specific and log identifiers rather than codes. To run it against real numbers, create a free SMS Feel account and start with the API documentation.

Related reading: more Node.js integration guides on the SMS Feel blog.

Tags

#otp verification api node.js #nodejs otp api #order number #poll for code #api integration #api #docs
Contact us