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

Node.js Tutorial: Order a Virtual Number and Read the SMS Code

Node.js Tutorial: Order a Virtual Number and Read the SMS Code

This nodejs sms verification api tutorial shows how to order a virtual number, wait for the SMS and read the code using JavaScript. It is written for developers who want a working flow they can drop into an existing app or bot.

Direct answer: Use Node.js with axios or fetch, send an order request to the OtpGet API, store the order ID, poll the status endpoint on a timer, extract the code when it arrives, and cancel the order if it times out.

Prerequisites

  • Node.js installed on your machine.
  • An OtpGet account and an API key.
  • A funded balance.
  • Basic familiarity with async/await.

Step 1: Set Up the Project

  1. Create a folder and run npm init -y.
  2. Install axios: npm install axios.
  3. Create a .env file with your API key.
  4. Add a script file, for example order.js.

Read the API documentation to confirm the endpoint names and parameters your account supports.

Step 2: Order the Number

The order request sends your API key, the country and the service. The response returns an order ID and a phone number. Save both in variables or a database row so you can use them in the polling step.

Always check the response for error codes before proceeding. If you see NO_NUMBERS, tell the user to pick another country instead of continuing.

Step 3: Display the Number to the User

Send the number to your frontend or chat interface immediately. The user needs it to request the OTP from the target service. Show a countdown so they know how long they have before the order expires.

Step 4: Poll for the SMS Code

Use setInterval or a recursive async function with a delay. Request the status endpoint with the order ID, check for a code field, and stop when found.

Elapsed timeDelay
First minute5 seconds
After one minute10-15 seconds

Clear the interval on success and on timeout to avoid memory leaks.

Step 5: Handle Errors and Timeouts

Map API error codes to clear messages. If the order times out, call the cancel endpoint and refund the user. If you see NO_BALANCE, stop all ordering and alert yourself, because your reseller balance is empty.

Our guide on OtpGet API error codes NO_NUMBERS and NO_BALANCE explains the full mapping.

Step 6: Extract the Code Safely

Some SMS messages contain extra text. Use a simple regex to pull the digits, for example a pattern that matches a 4-8 digit sequence near the word code. Test with sample messages and never trust the raw text blindly.

Step 7: Production Tips

  • Keep the API key in environment variables.
  • Log every request and response with timestamps.
  • Use a queue for high volume so you do not block the event loop.
  • Monitor success rates per country.

Frequently Asked Questions

Can I use fetch instead of axios?

Yes. Modern Node.js has native fetch, and the logic is the same.

Do I need a database?

For a single test, no. For production, store orders so you can match codes to users.

How do I handle multiple users at once?

Use a job queue or async tasks per order, each with its own timer.

What if the SMS arrives after my timeout?

Cancel at timeout and refund. Late codes are rare but you should not keep polling after cancellation.

Is this API suitable for beginners?

Yes. The flow is simple: order, poll, read, cancel. The docs cover each step.

Conclusion

With this nodejs sms verification api flow you can build a reliable virtual number feature in a few hours. Keep the code modular, respect polling limits and handle errors clearly. Create your account on OtpGet and try the flow in a test script first.

Related reading: more tutorials on the OtpGet blog.

Tags

#nodejs sms verification api #nodejs virtual number #otp polling #read sms code #api integration #virtual number tutorial
Contact us