This SDK is currently available for preview purposes only. It should not be used in production environments.

Send an SMS Message with Node.js SDK

Note:

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

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

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

Set up your Node.js application

First we'll create a Node project using npm. This creates a package.json and the core dependencies necessary to start coding.

To create the project, do the following steps:

  1. Create a folder called send-sms-app
  2. Navigate into the folder you created and run the following command.
    Copy
    Copied
    npm init
    This command adds the package.json file. You will be prompted to provide values for the fields. For this tutorial, you can simply accept the default values and press enter at each stage.

You can install the Sinch Node.js SDK using either NPM or Yarn:

NPMYarn
Copy
Copied
npm install @sinch/sdk-core
Copy
Copied
yarn add @sinch/sdk-core
Note:
The @sinch/sdk-core 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 "index.mjs" code into the file.

index.mjs

This code sends an SMS to the specified phone number using the Sinch Node.js SDK.

import { SinchClient } from '@sinch/sdk-core';

const sinchClient = new SinchClient({
    projectId: "YOUR_project_id",
    keyId: "YOUR_access_key",
    keySecret: "YOUR_access_secret"
});

let response = await sinchClient.sms.batches.send({
    sendSMSRequestBody: {
        to: [
            "YOUR_to_number"
        ],
        from: "YOUR_sinch_number",
        body: "This is a test message using the Sinch Node.js SDK."
    }
});

console.log(JSON.stringify(response));

Modify your application

The code provided includes placeholder parameters. You'll need to update the parameters detailed in the following subsections with your values.

Initialize the client

Before initializing a client using this SDK, you'll need three pieces of information:

  • Your Project ID
  • An access key ID
  • An access key Secret
These values can be found on the Access Keys page of the Customer Dashboard. You can also create new access key IDs and Secrets, if required.
Note:
If you have trouble accessing the above link, ensure that you have gained access to the Conversation API by accepting the corresponding terms and conditions.

To start using the SDK, you need to initialize the main client class with your credentials from your Sinch dashboard.

Copy
Copied
import {SinchClient} from '@sinch/sdk-core';

const sinchClient = new SinchClient({
    projectId: "YOUR_project_id",
    keyId: "YOUR_access_key",
    keySecret: "YOUR_access_secret"
});
Note:

For testing purposes on your local environment it's fine to use hardcoded values, but before deploying to production we strongly recommend using environment variables to store the credentials, as in the following example:

.env File

Copy
Copied
PROJECTID="YOUR_project_id"
ACCESSKEY="YOUR_access_key"
ACCESSSECRET="YOUR_access_secret" 

app.js File

Copy
Copied
import {SinchClient} from '@sinch/sdk-core';

const sinchClient = new SinchClient({
    projectId: process.env.PROJECTID,
    keyId: process.env.ACCESSKEY,
    keySecret: process.env.ACCESSSECRET
});
Note:

If you are using the Node.js SDK for multiple products that use different sets of authentication credentials, you can include all of the relevant credentials in the same configuration object, as in the following example:

Copy
Copied
import { SinchClient } from '@sinch/sdk-core';

const sinchClient = new SinchClient({
    projectId: "YOUR_project_id",
    keyId: "YOUR_access_key",
    keySecret: "YOUR_access_secret",
    applicationKey: "YOUR_application_key",
    applicationSecret: "YOUR_application_secret"
});

Fill in remaining parameters

Replace the remaining placeholder values for these parameters with your values:

ParameterYour value
YOUR_Sinch_numberAny 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.
YOUR_to_numberThe phone number to which you want to send the test SMS message.

Ensure you save your 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

You should receive a text to the phone number you entered and you'll see a response in your terminal or command prompt. You did it!

Next steps

The code you used in the index.mjs 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.

Additional resources

We'd love to hear from you!
Rate this content:
Still have a question?
 
Ask the community.

index.mjs

This code sends an SMS to the specified phone number using the Sinch Node.js SDK.

import { SinchClient } from '@sinch/sdk-core';

const sinchClient = new SinchClient({
    projectId: "YOUR_project_id",
    keyId: "YOUR_access_key",
    keySecret: "YOUR_access_secret"
});

let response = await sinchClient.sms.batches.send({
    sendSMSRequestBody: {
        to: [
            "YOUR_to_number"
        ],
        from: "YOUR_sinch_number",
        body: "This is a test message using the Sinch Node.js SDK."
    }
});

console.log(JSON.stringify(response));