Node_js

NITHIN

Node.js Send Text Message : A Step-by-Step Guide

Discover the step-by-step process of sending SMS messages from your Node.js application.

In this comprehensive guide, you’ll learn how to send SMS from a Node.js application use the powerful D7 Messaging API. Whether you're building alerts, marketing tools, or user notifications, this tutorial will walk you through the process step by step.

Why Use Node.js to Send Text Messages?

Node.js is a fast, scalable, and efficient runtime environment widely used for real-time and backend services. When combined with an API, it becomes an ideal solution for sending SMS messages programmatically, quickly, and reliably.

Want to test it yourself?
👉Sign up for a free D7 account and start sending messages in minutes!

Prerequisites

Before we get started:

  • Create aD7 Networks account (you can start with a free trial).
  • Claim your free credits via OTP verification.
  • Generate your Auth Token by creating a D7 Application

Install Direct7 SDK in Node.js

To integrate D7 Messaging into your Node.js project, install the Direct7 Node.js package: npm i direct7

Send Text Message Using Node.js

Create a file named send_sms.js and paste the following code:

const Client = require('direct7')

const client = new Client(apiToken="Your API token")
const schedule_time = null ;
const tag = null ;

const response = await client.sms.sendMessage(
        'SignOtp',
        'https://the_url_to_recieve_delivery_report.com',
        schedule_time,
        tag,
        {
            recipients: ["+recipient1", "recipient2"],
            content: "Greetings from D7 API",
            unicode: false
        }
    );
console.log(response);

Remember to replace the placeholder values for the API token, recipients, and other parameters with your specific details.

Different Scenarios for Sending SMS Using Node.js

The D7 Messaging API is highly flexible and supports multiple messaging use cases beyond simple one-to-one messages. Here are some common scenarios:

  • Same Content to Multiple Recipients
    Send a broadcast message to multiple users at once using an array of recipient numbers.
  • Different Content to Multiple Destinations
    Personalize each message using dynamic values for different recipients in the same request.
  • Send Unicode Messages
    Enable support for non-Latin languages such as Arabic, Chinese, Hebrew, etc., by setting unicode: true.
  • Send Mixed Content (Text and Unicode)
    Combine English text with Unicode characters to create rich, multilingual messages.
  • Send Scheduled Messages
    Schedule SMS for a specific time using the schedule_time parameter.

📖 Explore these use cases with code samples in the official docs:
👉 See Full SMS API Guide

Understanding the Code

🔑 API Token

This is your secure access token from the D7 dashboard. Replace "Your API token" with your actual token.

👤 Originator

The sender's name or phone number is displayed on the recipient's device. Must be a registered Sender ID.

🔗 Register your Sender ID

📱 Recipients

Enter the recipient’s phone number in international format, e.g., +16175551212.

📝 Content

The actual SMS message. Messages over 160 characters are split into parts. Unicode messages over 70 characters are split as well.

🌐 Unicode

Set unicode: true for non-English characters like Arabic, Hebrew, Chinese, etc.

📡 Report URL

Use this to get real-time delivery status updates.

Once you've updated the code, run it from the command line:

node send_sms.js

In just a few moments, you should receive an SMS! Let’s take a moment to understand what’s going on behind the scenes when you send this request to D7.

Track Message Delivery Status

When Direct7 receives your SMS request via the REST API, it validates the API token and either queues the SMS or returns an HTTP error. If validated, it provides a response containing the request_id, status, and created_at time. A sample response looks like this:

{       	"request_id": "d9835609-a4e0-10ea-a26a-eeafbe700fef", "status": "accepted",
"created_at": "2001-08-05T12:52:38.393Z"}

To see the status of your message, include the following code with the above request_id.

const Client = require('direct7')
const client = new Client(apiToken="Your API token")
const response = await client.sms.getStatus({request_id:"d9835609-a4e0-10ea-a26a- eeafbe700fef"});
console.log(response);

Pro Tips for Using Node.js to Send Text Messages

  • Error Handling: Use try-catch for robust code.
  • Bulk Messaging: Send to multiple recipients in one go.
  • Scheduling: Use cron jobs for timed messages.
  • DLR Logs: Store DLRs for delivery tracking.

Use Cases for Sending SMS via Node.js

Using Node.js to send text messages with the D7 Messaging API is fast, reliable, and scalable. Whether you're building small notification systems or full-scale communication platforms, this integration provides a seamless way to deliver your messages across the globe.

📢 Take your communications to the next level.
👉 Sign up and start sending SMS today

Happy messaging with Node.js and D7!


bg-img-left bg-img-left

Sign Up and Try D7 API for Free

Start today and enhance your communication workflows.