Node.js

As an outcome of this guide, you will learn how to send a simple text message to your handset with the Infobip SMS API and the Infobip Node.js SDK.

To see the finished code, check the complete code example at the bottom of this guide.


Prerequisites

Difficulty level

This guide assumes basic knowledge of Node.js and very basic familiarity with APIs.

Summary of the steps

  • Install the Infobip API Node SDK.
  • Import the Infobip and AuthType dependencies.
  • Create an Infobip instance and add your base URL and the API key.
  • Add the SMS payload to the infobip.channel.sms.send method.
  • Log a response to track the status of your SMS message.

Install the Infobip API Node SDK

npm i -S @infobip-api/sdk
OR
yarn add @infobip-api/sdk
view raw node-install.sh hosted with ❤ by GitHub

Import dependencies and create an Infobip client instance

You’ll use the client instance to authenticate against the Infobip API and access all its methods.

import { Infobip, AuthType } from '@infobip-api/sdk';
const infobipClient = new Infobip({
baseUrl: process.env.INFOBIP_URL,
apiKey: process.env.INFOBIP_KEY,
authType: AuthType.ApiKey,
});

Step 1. Import the Infobip and AuthType dependencies.

Step 2. Create an Infobip instance and add your baseUrl and apiKey that you can access either from your Infobip account or from the Infobip API landing page, for those who logged in first.

Add the SMS payload

Use the infobip.channel.sms.send method to add the SMS payload.

try {
const infobipResponse = await infobipClient.channels.sms.send({
type: 'text',
messages: [{
destinations: [
{
to: '447123456789',
},
],
from: 'Infosum SDK Test',
text: 'Hello World',
}],
});
} catch (error) {
console.error(error);
}

Key points:

  • The destination address in the to field must be in the international format, e.g. 447415774332.
  • If using a free Infobip trial account, the phone number you use must be the same phone number you’ve registered on signup for that account.

Add multiple recipients and multiple messages

Add multiple recipients by adding multiple to fields to the destination array. Similarly, create multiple objects within the messages array to send multiple messages with one request.

Log a response to track the SMS status

Log the data object to view the response body.

const {data} = infobipResponse;
  console.log(JSON.stringify(data));

The response contains a messageId that you can use for troubleshooting, analytics, scheduling, etc. In the case of multiple messages, your response will also contain an auto-generated bulkId that you can use similarly.

{
  "bulkId": "2034072219640523072",
  "messages": [
    {
      "messageId": "41793026727",
      "status": {
        "description": "Message sent to next instance",
        "groupId": 1,
        "groupName": "PENDING",
        "id": 26,
        "name": "MESSAGE_ACCEPTED"
      },
      "to": "2250be2d4219-3af1-78856-aabe-1362af1edfd2"
    }
  ]
}

Complete code example

All of the above is condensed into the following code snippet.

try {
const infobipResponse = await infobipClient.channels.sms.send({
type: 'text',
messages: [{
destinations: [
{
to: '447123456789',
},
],
from: 'Infosum SDK Test',
text: 'Hello World',
}],
});
} catch (error) {
console.error(error);
}