Sinch Developer Documentation

Building blocks to enable rich customer communication

Node.js Quickstart

Learn how to quickly send SMS messages with the Sinch API

In a very short amount of time and with a few lines of code, your Node.js application can send and receive SMS messages via Sinch.

This Quickstart guide will teach you how to:

  1. Sign up for a Sinch account and get an SMS enable Sinch virtual number.
  2. Ready your environment for sending and receiving messages.
  3. Send your first SMS
  4. Receive inbound messages via Webhooks

Sign up for a Sinch account and get a Sinch virtual number

If you’ve already signed up for a Sinch account and an SMS enabled phone number, you can jump to the next step.

If you need to get a virtual number from Sinch, click here to learn how.

Install Node.js

To see if Node.js is already installed on your machine, open a terminal, and run the following command:

node --version

If installed, you will see something like:
$ node --version
V14.5.1

If you don’t already have Node.js installed, simply go to Node.js and select the right installer for your system. To be sure you’ve installed Node.js correctly, run the command above.

Send SMS

If you haven't already, create a new node app with npm, accept the defaults, and add the request package.

npm init
npm install request

Create the index.js and paste the below into the file:

var request = require("request");
var options = {
  method: 'POST',
  url: 'https://us.sms.api.sinch.com/xms/v1/{service_plan_id}/batches',
  headers: {accept: 'application/json', 'content-type': 'application/json',
      "Authorization": "Bearer {your token}"},
  body: '{"to":"[{To Number}]","from":"{your free test number}","body":"This is a test message"}'
};

request(options, function (error, response, body) {
  if (error) throw new Error(error);
  console.log(body);
});

Replace the token values

Before you can execute the code that sends an SMS message, you need to replace the following values with your values:

{service_plan_id}
{your token}
{your free test number}
{To number}

To find the service plan and token, go to https://dashboard.sinch.com/sms/api/rest, log in and click “Show” to reveal your API token.

Screen shot of dashboardScreen shot of dashboard

To find the From-number, click the service plan id link and scroll to the bottom of the page and then change the {To number} to your phone number.

Click here to read more about the batches endpoint.

Configure the Callback URL for your SMS service using webhooks

Before you implement the server to handle incoming SMS, you need to configure your SMS service to handle callbacks. Head over to https://dashboard.sinch.com/sms/api/rest, click your service and you will see a section like the below.

Screen shot of dashboardScreen shot of dashboard

If you just want to look at what we post you can use http://requestbin.net/

Click create and you will see:

Screen shot of request binScreen shot of request bin

Copy the bin URL to the callback URL info and click Save.

Screen shot of callback configuredScreen shot of callback configured

That's it! You're now ready to send an SMS to your Sinch number

To see the data we send on incoming SMS, refresh your request bin page.

requestbin requestrequestbin request

Handle incoming SMS with Node.js

Create a new node app and paste this into app.js

const url = require("url");
const http = require("http");
const server = http.createServer((req, res) => {
  let data = [];
  req.on("data", (chunk) => {
    data.push(chunk);
  });
  req.on("end", () => {
    console.log(JSON.parse(data));
  });
  res.end();
});
server.listen(3000);

Before you can handle incoming traffic to your local server, you need to open up a tunnel to your local server. For that, you can use ngrok tunnel. Open a terminal/command prompt and type: ngrok http 3000

Copy the https address in your window, then run app.js in the command prompt 'node app.js'

requestbin requestrequestbin request

Go back to your dashboard and change the callback URL for your SMS service.

  1. In the terminal windows, start the app.js node app.js
  2. Send an SMS to your Sinch Number.
  3. You will now see the request come in.

requestbin requestrequestbin request

You can read more about all the different endpoints in the API reference guide

Edit on GitHub

Updated 4 days ago


Node.js Quickstart


Learn how to quickly send SMS messages with the Sinch API

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.