Sending SMS in Node.js with D7 Messaging API: A Step-by-Step Guide
In this comprehensive tutorial, we'll walk you through the process of sending SMS messages from your Node.js application using the D7 Messaging API. Node.js is an open-source, cross-platform JavaScript runtime environment that executes JavaScript code outside of a web browser. We'll utilize the Direct7 Node.js package for seamless integration. Let's dive into the details!
Prerequisites
Before we begin, make sure you have a D7 account with SMS capabilities. If you don't have an account, you can sign up for a free trial. Once registered, claim your free credits through OTP verification and create a D7 application to obtain the Auth Token, a crucial component for the code implementation
Sending SMS via the REST API
To send an outgoing SMS from your D7 account, you'll need to make an HTTP POST request. The Direct7 Node.js library simplifies this process. If you haven't installed the package, do so by running: npm i direct7
Now, create a file named send_sms.js and include the following code:
const Client = require('direct7')
const client = new Client(apiToken="Your API token")
const response = await client.sms.sendMessage({
recipients: ['+91999XXXXXXX'],
content: 'Greetings from D7 API',
originator: 'SignOtp',
report_url: 'https://the_url_to_recieve_delivery_report.com',
unicode: false
});
console.log(response);
Remember to replace the placeholder values for the API token, recipients, and other parameters with your specific details.
Understanding the Code
● API Token
Replace the placeholder with your unique API token obtained from the D7 dashboard.
● Originator
Specify the sender/header by replacing the placeholder. It can be your brand name with a maximum character limit of 11 or a mobile number with a country code. If you would like to register a new SenderID, you can submit your details here. You can also set the Default Originator here, and all your messages will be prefixed with the given originator
● Recipients
Set the recipient's mobile number with the country code prefix (+) and a country code, e.g., +16175551212). You can also set the Default Country here, and all your messages will be prefixed with the selected country code.
● Content
Define the content of your SMS, which contains the content of the SMS we’re going to send. A long message over 160 characters will be split into multiple messages with 153 characters each and forwarded to telecom. When Unicode messages exceed 70 characters, they are split into multiple messages of 63 characters each. Also, billing will be based on the number of message parts sent
● Unicode
You can also send normal text and Unicode messages by setting the parameter unicode to true or false. Set as false for normal GSM 03.38 characters (English, normal characters). Set as true for non-GSM 03.38 characters (Arabic, Chinese, Hebrew, Greek-like regional languages, and Unicode characters)
● Report URL
You can set the report_url parameter to receive delivery status (DLR) for your message and specify the callback server URL where you want to receive the message status updates using the report_url parameter. When the delivery status changes, the status updates will be sent to the specified URL. For information on the format of the DLR message, please refer to the "Receiving DLR" section.
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.
Understanding Direct7's Response
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);
Now you're equipped with the knowledge to seamlessly send SMS messages using the D7 Messaging API in your Node.js application.
Start messaging here: https://d7networks.com/docs/SMS/Send-SMS/.
Happy messaging!
Leave your comments