Node.js Express Project to Send OTP SMS Messages to Mobile Number Using Fast2SMS API in Javascript

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
uh hello friends today in this tutorial i will be showing you that how to receive otp message inside your nodejs application for any sort of verification inside your application for login application or any sort of so for doing this we will be using this api called as fast to sms so basically if you go to this website here fast to sms.com so you just need to register on this website and after registration you will get this interface and you will get some free credits where you can test out the application so you will see this is a brand new account that i created inside this website and it offers its own api which is this section dev api and after that the second option is this api key so select this option and it will automatically generate your api key you can also regenerate it so this is my api key simply copy this so accordingly just sign up and you will have your own api key simply copy this and now inside your node shares project guys so this is my node.js project let me build it from scratch and let me move out of this directory let me also close this and make a new directory which is node otp project i will go into this directory node otp project and i will open this inside my visual studio code text editor so now we will initialize the empty package.json file which contains all the information about the project now we need to install this dependency which is this is a node.js dependency which offers uh a great interface to this website which is fast to sms this is a module here so you can search for this module fast dash to dash sms so this is a module node.js module which interacts with this api so if you search for this module the very first link which comes in you will see this is the module here it contains 284 weekly downloads so simply you need to install this fast dash to dash sms so it will hardly take five seconds to install this module after installation you can use this module so simply i will create the index.js file the starting point of the application now we will simply ex import this module fast to sms we will require it fast dash to sms so after requiring this now we simply need to uh include the api key guys so inside options so just create an options variable here and inside this object it contains a parameter called as authorization so here you need to include your own api key so just go to your website dashboard i have already shown you how to register it simply copy this api key and paste it here and after that guys the second parameter is numbers here you can also provide bulk numbers also multiple numbers so if you want to send out multiple messages at one time you can do it so numbers colon and then inside this array you can pass the number so numbers should be passed without mentioning the country code so i will pass my number you will see just write it and also the second parameter will be the message guys whatever message that you want to send in my case i will send otp you can send out anything so we will say this is a test message test otp code message your otp is code is some random value five six seven eight so now we need to send this to the number that we mentioned here so for sending it it is very simple we will use the module which is fast to sms and this contains a send method send message method and here we will provide the options that's it so now guys this will return a this is a async function this will return a promise so we can display the response to the user simply in the console that if the message is sent or not and if any sort of error takes place we can catch it by dot catch and we can display the error in case of error like this console log error so basically now if i launch the application if i see if i write here node index.js so you will see it will send out the otp and now you will see it will return out the request id to this and message sms send successfully so we have successfully sent the otp message let me show it this will be let me show it let me move this window a little bigger so let me show the mobile where i received this so you will see guys if if i can show you like this this is the message it's received here you will see i just want to [Music] you can see that dear user this is a test otp code message your otp code is eight so i just received it right now you will see that so if you do it you can also receive it on your mobile phone as well so just need to provide your number and after that it will just uh send out to you it is damn easy using this api and if any if i make any sort of error i provide a wrong number and let me run it once again you will see that now it will show you error that it is a invalid number so now it will show you this error so this is a very simple node module by which you can send out otp sms messages to any number using node.js using fast to sms api and also guys you can make it as a express application you can give take the input from the user so we can do it we can convert this application so simply in install express this is very easy so i just want to convert this to a express application where we can take the number as the input so simply we will say we will make the index.html and we will have the form which will make a post request send message and the method will be post and inside this we will have input type of number or we can simply say text input type of text and here we will say number that's it so here placeholder will say enter mobile that's it so this is required after that we will have the button to send message and then we will just say this is of type submit that's it so now we need to make this post request so inside index.js guys we need to first of all import express so we will say var express require it so constab express and then we need to app.get when we load the page for the very first time the get request we need to load the index.html file so you will say request.send file and name plus index.html so we need to start the server so right here we will say app.listen and we will listen on port 5000 and we will say app is listening on port 5000 so you will see that guys if i run this application let me comment out this code here so that it doesn't again send out the message so now if i run this you will see you can see app is listening on port 5000 so basically if i go to my home page you will see now you will see localhost 5000 you will see this is a mobile number and send message and we also need a field for which we can provide a message to the user so we will simply say input type text and we will say message and we will say placeholder enter your message and this also is required so refresh it and now you will see two input fields will be there first for the number and second for the message and then we have a simple button of send message so simply we need to make this force request so we will say app.get oh sorry post send message and here we can receive these parameters simply by requesting this by console logging it request dot body dot message and request dot body dot number so we have provided these as name parameters so this is very much useful it needs to be same because we are getting this using this name parameter so if you launch this application again and if i send out this any number here if i write anything here if i click send you will see it cannot read it because we need to for this we need to require body parser middleware so whenever you are working for with any form in express you need this middleware this is a built-in middleware you need not have to install install it and then you need to initialize this this is very easy bodyparser.url encoded extend it to false and then we need to write about use body parser.json so just write these two lines and then your problem will be solved and simply once again refresh your application and send out so again send out so you will see that guys now these information is printed in the console so we are successfully getting it from the html form inside of express application now we simply need to move this code which you see here so simply you need to move this code here simply inside this post request so we can do this like this so we can make this inside a separate function we will say send message so we can make this as a async function async function so we can paste this code here like this like this so function send message so now inside this we can call this method here send message and we can pass the message which will be request or body dot message and the number as well request or body dot number so here we can receive this message dot number and also we can pass the response here response so just pass this response object as well so response is there so whenever it is successful we will simply say to the user we will send out a response that sms sent successfully sms otp if any sort of error take place then we can say simply send out some error taken place that's it so now we need to replace the number here so simply you need to replace it by the number that you are receiving here so simply just replace number here and for the message simply replace this with the message so inside this field replace it by message that's it so now we have made this application dynamic so we can provide the number and the message dynamically so once again refresh the application so again you will see it is saying sms quotes and successfully here you will see that so once again if i show you so if i zoom the screen you will see once again it is received on my phone here so you will see this is the latest message which is received here i've got the owner of coding six year so in this easy way guys you can send out otps sms messages to any mobile inside nodejs i have shown you all the source code and if you want the full source code you can go to the video description link i have given this link in the video description you can check out and download all the source code please hit the like button subscribe the channel and i will be seeing you in the next video
Info
Channel: Coding Shiksha
Views: 17,709
Rating: undefined out of 5
Keywords: coding shiksha, fast2sms api, node fast2sms, node send otp, node send message, node send message to mobile, send otp to mobile
Id: TjVEo8r7qHs
Channel Id: undefined
Length: 14min 22sec (862 seconds)
Published: Tue Jan 25 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.