Sending SMS Messages

Learn how to quickly send SMS messages in a Node.js application with the Sinch API.

In this guide you will learn how to:

Learn how to:
  1. Sign up for your free Sinch account
  2. Buy a number
  3. Set up your Node.js application
  4. Send your first SMS message

What you need to know before you start

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

  • Node.js and a familiarity with how to create a new app.

Sign up for your free account

Already have an account?

If you already have a free account, skip to the next section

You can sign up for a free Sinch account here.

  • When you sign up, you'll have to verify your email address and phone number. This lets Sinch verify your identity and you'll also be able to use your phone to send and receive test messages.
  • After you've verified your number, you'll be asked some questions in order to get you started in a way that's relevent to you.
  • After you finish setting up your account, you'll have access to your dashboard where you can view all of your projects and applications. You can also access your account ID, set up API tokens, get phone numbers, and more.
  • Once your account is created, you need to add a new REST API in order to send SMS Messages. Click Add New REST API and your new service plan and ID are token will be created. Take note of them, because you'll use them later.

Buy a number

Already have an number?

If you already have sinch number, skip to the next section

You can get numbers that are set up for SMS, Voice, or both. You can buy a number from any country where Sinch has numbers in inventory. Countries with numbers available for purchase are marked with Direct Access. All other countries will require you to submit a request to Sinch for manual handling.

You can search for and purchase virtual numbers by logging in to your dashboard.

  1. Go to Numbers | Get virtual numbers, fill in the required information, and click SEARCH. If you don't want to specify anything about the number, choose your country and click SEARCH. Available numbers to buy are displayed in the list.

  2. When you see a number you want to buy, click GET next to the number to buy it.

  3. You can configure your number with an SMS, Voice or Campaign ID (only for 10DLC numbers) when you activate the number.

    When you get a virtual number, if supporting documentation is needed, you will see a message in an orange box at the bottom of your form. You won't be able to get a virtual number until supporting documentation is submitted. Click here to learn more about supporting documentation.

Set up your Node.js application

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.

npm install 'node-fetch'

Create your file

Create a new file named index.js 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 servicePlanId = '';
const apiToken = '';
const sinchNumber = '';
const toNumber = '';

const fetch = require('node-fetch');

const sendSMS = {
  from: sinchNumber,
  to: [toNumber],
  body: 'Test message from Sinch.',
};

fetch('https://us.sms.api.sinch.com/xms/v1/' + servicePlanId + '/batches', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    Authorization: 'Bearer ' + apiToken,
  },
  body: JSON.stringify(sendSMS),
})
  .then((res) => res.json())
  .then((json) => console.log(json));

Fill in your parameters

Assign your values to the following parameters:

ParameterYour value
servicePlanIdThe service plan ID found on your Sinch dashboard.
apiTokenThe API token found on your Sinch dashboard. Click Show to reveal your API token.
sinchNumberAny number you've assigned to your Sinch account. Find the number on your Sinch dashboard by clicking the service plan ID link and scrolling to the bottom of the page.
toNumberThe phone number to which you want to send the test SMS message.

Save the file.

Send your first SMS message

Now you can execute the code and send your test SMS message. Run the following command:

node index.js

Next steps

The code you used in the index.js file sends a POST request to the Sinch API /batches endpoint to send the SMS message. Click here to read more about the batches endpoint.

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 servicePlanId = '';
const apiToken = '';
const sinchNumber = '';
const toNumber = '';

const fetch = require('node-fetch');

const sendSMS = {
  from: sinchNumber,
  to: [toNumber],
  body: 'Test message from Sinch.',
};

fetch('https://us.sms.api.sinch.com/xms/v1/' + servicePlanId + '/batches', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    Authorization: 'Bearer ' + apiToken,
  },
  body: JSON.stringify(sendSMS),
})
  .then((res) => res.json())
  .then((json) => console.log(json));