You can send an SMS from Node.js with a single fetch() call to the SMSGatewayHub REST API — no SDK or npm install needed. This guide covers single SMS, bulk SMS, OTP, and how to handle the JSON response and errors correctly.
What You Need Before You Start
Node.js 18 or later (for built-in fetch) and three things from your SMSGatewayHub account:
| Requirement | Where to get it |
|---|
| APIKey | Dashboard → API Settings, after free signup at smsgatewayhub.com/Register |
| DLT-approved Sender ID | Registered on your telecom operator's DLT portal |
| EntityId & dlttemplateid | Generated when your DLT entity/template is approved |
⚠ DLT registration is mandatory for Indian mobile numbers — see the DLT Registration guide (smsgatewayhub.com/dlt-registration) if you haven't done this yet.
Quick Start: Send a Single SMS
This is the complete code — no dependencies, works on any Node.js 18+ project:
// send-sms.js
const APIKEY = "yourapicode";
const SENDER_ID = "TESTIN";
const ENTITY_ID = "your-entity-id";
const TEMPLATE_ID = "your-dlt-template-id";
async function sendSms(number, text) {
const url = new URL("https://www.smsgatewayhub.com/api/mt/SendSMS");
url.searchParams.set("APIKey", APIKEY);
url.searchParams.set("senderid", SENDER_ID);
url.searchParams.set("channel", "2"); // 2 = Transactional
url.searchParams.set("DCS", "0"); // 0 = English, 8 = Unicode
url.searchParams.set("flashsms", "0");
url.searchParams.set("number", number); // e.g. 91989xxxxxxx
url.searchParams.set("text", text);
url.searchParams.set("route", "1");
url.searchParams.set("EntityId", ENTITY_ID);
url.searchParams.set("dlttemplateid", TEMPLATE_ID);
const res = await fetch(url.toString());
const data = await res.json();
if (data.ErrorCode === "000") {
console.log("SMS sent. Job ID:", data.JobId);
} else {
console.error("SMS failed:", data.ErrorMessage);
}
return data;
}
sendSms("91989xxxxxxx", "Your order has been shipped!");
Run it with node send-sms.js. The URLSearchParams-based URL object automatically URL-encodes your message text, so you don't need to call encodeURIComponent() manually.
Sending Bulk SMS in Node.js
Same endpoint, same code — just pass comma-separated numbers:
const numbers = ["91989xxxxxxx", "91999xxxxxxx", "91888xxxxxxx"].join(",");
await sendSms(numbers, "Flash sale starts now — 30% off, today only!");
For large recipient lists (thousands of numbers), batch them in chunks of a few hundred per request rather than one giant comma-separated string, to keep the URL length manageable.
Sending OTP SMS in Node.js
Set channel to "OTP" — everything else stays the same. OTP messages get delivery priority and bypass DND filters:
function generateOtp() {
return Math.floor(100000 + Math.random() * 900000).toString(); // 6-digit
}
async function sendOtp(number) {
const otp = generateOtp();
const url = new URL("https://www.smsgatewayhub.com/api/mt/SendSMS");
url.searchParams.set("APIKey", APIKEY);
url.searchParams.set("senderid", SENDER_ID);
url.searchParams.set("channel", "OTP");
url.searchParams.set("DCS", "0");
url.searchParams.set("flashsms", "0");
url.searchParams.set("number", number);
url.searchParams.set("text", `Your OTP is ${otp}. Valid for 5 minutes. Do not share this code.`);
url.searchParams.set("route", "1");
url.searchParams.set("EntityId", ENTITY_ID);
url.searchParams.set("dlttemplateid", TEMPLATE_ID);
const res = await fetch(url.toString());
const data = await res.json();
return { ...data, otp }; // store otp (hashed) server-side to verify later
}
🔒 Never send the OTP back to the client in the API response for real use — this example returns it only so you can store a hashed copy server-side to verify against later.
Handling the Response
Every successful call returns a JSON body like this:
{"ErrorCode":"000","ErrorMessage":"Success","JobId":"20047",
"MessageData":[{"Number":"91989xxxxxxx","MessageId":"BqTiw66A2UGLFV3DnwHFLQ"}]}
Store the JobId if you want to check delivery status later via the DLR Status API (smsgatewayhub.com/free-sms-gateway-developer-api#dlr).
Common Errors & Fixes
| ErrorCode | Meaning | Fix |
|---|
| 0013 | Mobile numbers not valid | Include country code, no spaces/symbols: 91989xxxxxxx |
| 0015 | Sender ID not valid | Confirm the Sender ID is DLT-approved, exactly 6 characters |
| 0021 | Insufficient credits | Top up your account balance |
| 0024 | Template mismatch | Message text must match your DLT template exactly |
Always wrap the fetch call in try/catch to handle network failures separately from API-level errors:
try {
const result = await sendSms(number, text);
if (result.ErrorCode !== "000") {
// handle API-level error (invalid number, insufficient credits, etc.)
}
} catch (networkErr) {
// handle network/timeout failure
}
FAQ
Do I need an npm package to send SMS from Node.js?
No. Node.js 18+ includes a built-in fetch() function, so you can call the SMSGatewayHub REST API directly with zero dependencies.
How do I send bulk SMS from Node.js in one request?
Pass multiple mobile numbers separated by commas in the number parameter of the same SendSMS endpoint — no code changes needed beyond the parameter value.
How do I handle SMS API errors in Node.js?
Check the ErrorCode field in the JSON response and branch your logic on it, wrapped in a try/catch around the fetch call for network-level failures.
Full API reference: this guide covers the essentials — for every parameter, JSON/XML POST formats, Voice API and the live API tester, see the complete SMS API documentation at smsgatewayhub.com/free-sms-gateway-developer-api.
Get your free API key and start sending
Free signup, no setup fee — start sending SMS from Node.js in minutes.
🔑 Get Your Free API Key