Receive an SMS Message with Node.js
Note:
Before you can get started, you need the following already set up:
- Set all SMS API configuration settings.
- Node.js and a familiarity with how to create a new app.
- ngrok. You'll use ngrok to open a tunnel to your local server.
Quickly set up a node.js server to receive SMS messages with the Sinch SMS API.
Set up your Node.js application
First we'll create a Node project using npm. This creates a package.json and the core dependencies necessary to start coding.
To create the project:
- Create a folder called
receive-sms-app
- Navigate into the folder you created and run the following command.
npm init
This command adds the package.json file. You will be prompted to provide values for the fields. For this tutorial, you can simply accept the default values and press enter at each stage.
- Add the axios package with npm to generate the necessary dependencies. In this application, we are going to use Express to handle incoming requests.
npm install axios npm install express
Create your file
Create a new file named index.js in the project and paste the "Receive an SMS message" code into the file.
Fill in your parameters
Change the following parameters in the code to your values.
Parameter | Your value |
---|---|
SERVICE_PLAN_ID | The service plan ID found on your Sinch Customer Dashboard. SMS > APIs > REST configuration |
API_TOKEN | The API token found on your Sinch Customer Dashboard. SMS > APIs > REST configuration > Click Show to reveal your API token. |
Double check that the region is correct on your base URL. Learn more about regional options here.
Start your web server and set up a tunnel
We use webhooks to notify your application when someone sends a text to your Sinch number. To handle these, you will learn how to create a webserver and make it reachable on the Internet.
- Start the server by executing the following command:
node index.js
- Now you need to open a tunnel to the server you just set up. We are using ngrok for this. If you don't have ngrok installed already, install it with the following command:
npm install ngrok -g
- Open a terminal or command prompt and enter:
ngrok http 3000
You will see a screen like the following.
- On the highlighed "Forwarding" line, copy the address ending in
.ngrok.io
.
Configure your Callback URL
Next, configure a Callback URL for your Sinch account.
- On your Sinch Customer Dashboard-> Service APIs click on the service plan ID link.
- In “Callback URL” click Add Callback URL and paste in the HTTPS address referred to in the previous section.
Send an SMS message to your sinch number
To test your application, send an SMS message to your Sinch number from your mobile phone to get an automatic reply.
Additional resources
- Visit our API specification to test more endpoints.