Skip to content
Last updated

Receive an SMS Message with Node.js

Note:

Before you can get started, you need the following already set up:

Quickly set up a node.js server to receive SMS messages with the Sinch SMS API.

Steps:
  1. Set up your Node.js application.
  2. Configure your Callback URL.
  3. Test the application.

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:

  1. Create a folder called receive-sms-app

  2. 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.

  3. 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.

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 REGION ='YOUR_region'
const SINCH_URL= 'https://'+REGION+'.sms.api.sinch.com/xms/v1/' + SERVICE_PLAN_ID + '/batches'
const headers={'Content-Type': 'application/json', 'Authorization': 'Bearer ' + API_TOKEN}
const axios = require('axios')
const express = require('express');
const app = express();
const port = 3000;
app.use(express.json());

app.post('/', async (req, res) => {
  var requestBody = req.body;
  console.log(requestBody);
  const payload = {
    from: requestBody.to,
    to: [requestBody.from],
    body: 'You sent: ' + requestBody.body,
  };

axios.post(SINCH_URL, payload, { headers })
    .then(response =>
        console.log(response.data)
    ).catch(error =>
    console.error('There was an error!', error.response)
);
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.

ParameterYour value
SERVICE_PLAN_IDThe service plan ID found on your Sinch Build Dashboard. SMS > APIs > REST configuration
API_TOKENThe API token found on your Sinch Build 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.

  1. Start the server by executing the following command:

    node index.js
  2. 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
  3. Open a terminal or command prompt and enter:

    ngrok http 3000

    You will see a screen like the following. ngrok screenshot

  4. 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.

  1. On your Sinch Build Dashboard-> Service APIs click on the service plan ID link.
  2. 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