Send an SMS Message with Node.js

Note:

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

Quickly send SMS messages in a Node.js application with the Sinch SMS API.

Steps:
  1. Set up your Node.js application.
  2. Send your first SMS message.

Set up your Node.js application

  1. Once your free Sinch account is setup and you have a virtual number, 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.
    Copy
    Copied
    npm install node-fetch
Note:

The node fetch package requires us to use node modules, so we need to use a .mjs file type instead of a .js.

Create your file

Create a new file named index.mjs in the project and paste the provided "Send an SMS message" code into the file.

Send 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 SINCH_NUMBER = 'YOUR_Sinch_virtual_number';
const TO_NUMBER = 'sender_number';

import fetch from 'node-fetch';

async function run() {
  const resp = 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({
        from: SINCH_NUMBER,
        to: [TO_NUMBER],
        body: 'Programmers are tools for converting caffeine into code. We just got a new shipment of mugs! Check them out: https://tinyurl.com/4a6fxce7!'
      })
    }
  );

  const data = await resp.json();
  console.log(data);
}

run();

Fill in your parameters

  1. Assign your values to the following parameters:
    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.
    SINCH_NUMBERAny number you've assigned to your Sinch account. Find the number on your Sinch Customer Dashboard by clicking the service plan ID link and scrolling to the bottom of the page.
    TO_NUMBERThe phone number to which you want to send the test SMS message.
  2. Then, save the file.

Send your first SMS message

Now you can execute the code and send your test SMS message.

Run the following command:

Copy
Copied
node index.mjs

Next steps

The code used in the index.mjs file sends a POST request to the Sinch SMS API /batches endpoint to send the SMS message. Read more about the batches endpoint.

Additional resources

Was this page helpful?

Send 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 SINCH_NUMBER = 'YOUR_Sinch_virtual_number';
const TO_NUMBER = 'sender_number';

import fetch from 'node-fetch';

async function run() {
  const resp = 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({
        from: SINCH_NUMBER,
        to: [TO_NUMBER],
        body: 'Programmers are tools for converting caffeine into code. We just got a new shipment of mugs! Check them out: https://tinyurl.com/4a6fxce7!'
      })
    }
  );

  const data = await resp.json();
  console.log(data);
}

run();