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

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. Create a new node app with npm.
    Copy
    Copied
    npm init
  2. Accept the defaults for the application.
  3. Add the fetch package with npm to generate the necessary dependencies. In this application, we are going to use Express to handle incoming requests.
    Copy
    Copied
    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.

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

  1. Start the server by executing the following command:
    Copy
    Copied
    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:
    Copy
    Copied
    npm install ngrok -g
  3. Open a terminal or command prompt and enter:
    Copy
    Copied
    ngrok http 3000
  4. 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

Was this page helpful?

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}`);
});