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
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.
- Create a new node app with npm.
npm init
- Accept the defaults for the application.
- Add the fetch package with npm to generate the necessary dependencies. In this application, we are going to use Express to handle incoming requests.
npm install cross-fetch 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.
Receive an SMS message
// Find your Service Plan ID and API Token at dashboard.sinch.com/sms/api/rest
// Find your Sinch numbers at dashboard.sinch.com/numbers/your-numbers/numbers
const SERVICE_PLAN_ID = 'YOUR_servicePlanId';
const API_TOKEN = 'YOUR_API_token';
const express = require('express');
const fetch = require('cross-fetch');
const app = express();
const port = 3000;
app.use(express.json());
app.post('/', async (req, res) => {
var requestBody = req.body;
console.log(requestBody);
const sendSMS = {
from: requestBody.to,
to: [requestBody.from],
body: 'You sent: ' + requestBody.body,
};
let result = await fetch(
'https://us.sms.api.sinch.com/xms/v1/' + SERVICE_PLAN_ID + '/batches',
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + API_TOKEN,
},
body: JSON.stringify(sendSMS),
}
);
console.log(await result.json());
res.send('Ok');
});
app.listen(port, () => {
console.log(`Listening at http://localhost:${port}`);
});
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. |
Start your web server and set up a tunnel
- 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
- Copy the HTTP address that ends with
.ngrok.io
.
Configure your Callback URL
Next, configure a Callback URL for your Sinch account.
Login to your Sinch Customer Dashboard. Click on the service plan ID link and edit the Callback URL field with the ngrok.io domain URL from the previous step.
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.