How to send OTP codes with SMS in Node.js

Sending OTP (One-Time Password) is one of the most common tasks when building modern apps — signup verification, login security, password reset, etc.

In Node.js it’s extremely easy to implement — you just need an SMS provider (like Twilio) and library…


This content originally appeared on DEV Community and was authored by Jahongir Sobirov

Sending OTP (One-Time Password) is one of the most common tasks when building modern apps — signup verification, login security, password reset, etc.

In Node.js it’s extremely easy to implement — you just need an SMS provider (like Twilio) and library like auth-verify for generating, saving and checking user codes.

So we should install essential library (we'll only use auth-verify for whole process and we don't need any other libaries)

npm install auth-verify

And then we'll create index.js like this:

const AuthVerify = require('auth-verify');
const auth = new AuthVerify();

auth.otp.setSender({
   via: 'sms',
   provider: 'twilio',
   apiKey: "YOUR_ACCOUNT_SID",
   apiSecret "YOUR_AUTH_TOKEN",
   sender: "SENDER_NAME",
   mock: false // If you want to just test you can change this value to true
});

You can get accountsid and authtoken from here. And you should use your accountsid and authtoken for apiKey and apiSecret in auth-verify.
apiKeyaccountsid
apiSecretauthtoken
And then we'll generate and save OTP code for sending with SMS

auth.otp.generate(5).set("RECIEVER_NUMBER", (err)=>{
   if(err) console.log(err);
   auth.otp.message({
      to: "RECIEVER_NUMBER",
      text: `Your OTP code is ${auth.otp.code}`
   });
});

So we made OTP code and saved it to in-memory. Let's check and verify OTP code.

auth.otp.verify({check: "RECIEVER_NUMBER", code: "12345"}, (err, valid)=>{
   if(err) console.log(err);
   if (valid) console.log("Correct code!")'
   else console.log("Incorrect code!");
});

So we finally did it!


This content originally appeared on DEV Community and was authored by Jahongir Sobirov


Print Share Comment Cite Upload Translate Updates
APA

Jahongir Sobirov | Sciencx (2025-11-05T18:18:23+00:00) How to send OTP codes with SMS in Node.js. Retrieved from https://www.scien.cx/2025/11/05/how-to-send-otp-codes-with-sms-in-node-js/

MLA
" » How to send OTP codes with SMS in Node.js." Jahongir Sobirov | Sciencx - Wednesday November 5, 2025, https://www.scien.cx/2025/11/05/how-to-send-otp-codes-with-sms-in-node-js/
HARVARD
Jahongir Sobirov | Sciencx Wednesday November 5, 2025 » How to send OTP codes with SMS in Node.js., viewed ,<https://www.scien.cx/2025/11/05/how-to-send-otp-codes-with-sms-in-node-js/>
VANCOUVER
Jahongir Sobirov | Sciencx - » How to send OTP codes with SMS in Node.js. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/11/05/how-to-send-otp-codes-with-sms-in-node-js/
CHICAGO
" » How to send OTP codes with SMS in Node.js." Jahongir Sobirov | Sciencx - Accessed . https://www.scien.cx/2025/11/05/how-to-send-otp-codes-with-sms-in-node-js/
IEEE
" » How to send OTP codes with SMS in Node.js." Jahongir Sobirov | Sciencx [Online]. Available: https://www.scien.cx/2025/11/05/how-to-send-otp-codes-with-sms-in-node-js/. [Accessed: ]
rf:citation
» How to send OTP codes with SMS in Node.js | Jahongir Sobirov | Sciencx | https://www.scien.cx/2025/11/05/how-to-send-otp-codes-with-sms-in-node-js/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.