Skip to content Skip to sidebar Skip to footer

How Can I Send Verfication Codes To Mobile Numbers In Node.js? (like Nodemailer, But For Sms)

I have sent confirmation e-mails to users by using nodemailer for my project. It is working fine. Now I am looking to send verification codes to mobile numbers from node.js, but I

Solution 1:

Most carriers provide a SMS gateway to which you can send an email and have it arrive as SMS. If you want a free way of sending SMS that works with your current nodemailer implementation, this is probably your best option. Otherwise, you might want to search for paid SMS services that you can integrate with.

Here is a list of SMS gateways: http://en.wikipedia.org/wiki/List_of_SMS_gateways

From the linked Wikipedia page: For instance, to send to a number typically expressed in the USA as 987-555-0100, one would email 9875550100@SMS-gateway.

Solution 2:

NodeJS package https://www.npmjs.com/package/springedge will be easy to send sms. You can install as

npm install springedge

Code example of sending sms:

// send sms var springedge = require('springedge');

var params = {
  'apikey': '', // API Key 'sender': 'SEDEMO', // Sender Name 'to': [
    '919019xxxxxxxx'//Moblie Number 
  ],
  'message': 'test+message'
};

springedge.messages.send(params, 5000, function (err, response) {
  if (err) {
    returnconsole.log(err);
  }
  console.log(response);
});

Solution 3:

To ensure you can reach a users mobile no matter their location and network it's likely you're going to have to look at a paid service such as Nexmo (who I work for) or Twilio.

With these services you can either build your own verification (2FA - Two Factor Authentication) workflow:

  1. User enters their phone number in a form and submits to your app
  2. Your app receives the phone number
  3. You send the user an SMS with an auth code
  4. User receives the auth code
  5. User enters auth code into a form and submits to your app
  6. Your app receives the auth code and checks that auth code against the phone number (maybe using the current session phone number)
  7. If the auth code is the one you are expected then the user is validated

Or you can use their 2FA authentication products (Nexmo - Verify or Twilio - Authy that should help simplify this workflow.

Using Nexmo verify the code would be:

Send Verification Request

varNexmo = require('nexmo');
var nexmo = newNexmo({apiKey: API_KEY, apiSecret: API_SECRET});

var verifyRequestId = null; // use in the check process

nexmo.verify.request({number: TO_NUMBER, brand: APP_NAME}, function(err, result) {
  if(err) { console.error(err); }
  else {
    verifyRequestId = result. request_id;
  }
});

Check Authentication Code

nexmo.verify.control({request_id: verifyRequestId, cmd: 'cancel'}, function(err, result) {
  if(err) { console.error(err); }
  else {
    console.log(result);
  }
});

Solution 4:

You can use MSG 91 service. It provides OTP verification service as well.

Solution 5:

For a testing you should use this API https://www.fast2sms.com however you can use it as a business too. Before running the below code make sure you have generated valid API Authorization Key and for that you should register for free.

Now place receiver's mobile number for bulk message to and you can use it as a verification and OTP purpose too.

Install npm module in your project, where you want to implement an SMS system.

npm install unirest

Code for GET method:

var unirest = require("unirest");

var req = unirest("GET", "https://www.fast2sms.com/dev/bulk");

req.query({
  "authorization": "YOUR_API_KEY",
  "sender_id": "FSTSMS",
  "message": "This is a test message",
  "language": "english",
  "route": "p",
  "numbers": "9999999999,8888888888,7777777777",
});

req.headers({
  "cache-control": "no-cache"
});

req.end(function (res) {
  if (res.error) thrownewError(res.error);

  console.log(res.body);
});

Code for POST method:

var unirest = require("unirest");

var req = unirest("POST", "https://www.fast2sms.com/dev/bulk");

req.headers({
  "authorization": "YOUR_API_KEY"
});

req.form({
  "sender_id": "FSTSMS",
  "message": "This is a test message",
  "language": "english",
  "route": "p",
  "numbers": "9999999999,8888888888,7777777777",
});

req.end(function (res) {
  if (res.error) thrownewError(res.error);

  console.log(res.body);
});

Post a Comment for "How Can I Send Verfication Codes To Mobile Numbers In Node.js? (like Nodemailer, But For Sms)"