Here’s a clean and working example of how to send bulk SMS using Text.lk’s GET API with JavaScript. This version includes:
- Proper loop and function structure
- API integration using
fetch
- URL encoding to handle special characters in the message
✅ Bulk SMS Sender with Text.lk API (JavaScript)
// Function to send SMS to all numbers
function sendBulkSms(phoneNumbers, message) {
for (const phoneNumber of phoneNumbers) {
sendSms(phoneNumber, message);
}
}
// Function that sends a single SMS using Text.lk GET API
function sendSms(phone, message) {
const encodedMessage = encodeURIComponent(message);
const url = `https://app.text.lk/api/http/sms/send?recipient=${phone}&sender_id=${senderId}&message=${encodedMessage}&api_token=${apiToken}`;
fetch(url)
.then(response => response.json())
.then(data => {
console.log(`Message sent to ${phone}:`, data);
})
.catch(error => {
console.error(`Failed to send SMS to ${phone}:`, error);
});
}
// List of recipient phone numbers
const phoneNumbers = [94710000000, 94740000000, 94760000000];
// Message to be sent
const messageContent = 'This is a bulk message';
// Your Text.lk API token and sender ID
const apiToken = 'YOUR_API_TOKEN'; // Replace with your actual token
const senderId = 'TextLKDemo';
// Start the bulk SMS process
sendBulkSms(phoneNumbers, messageContent);
JavaScript
This script uses fetch to call the Text.lk API for each phone number.
Additionally, Below are two versions of the sendSms
function: one using fetch()
and the other using axios
. You can choose based on what you’re using in your environment (e.g., browsers usually prefer fetch
, Node.js apps often use axios
).
✅ 1. Version Using fetch() (Vanilla JavaScript)
function sendSms(phone, message) {
const encodedMessage = encodeURIComponent(message);
const url = `https://app.text.lk/api/http/sms/send?recipient=${phone}&sender_id=${senderId}&message=${encodedMessage}&api_token=${apiToken}`;
fetch(url)
.then(response => response.json())
.then(data => {
console.log(`Message sent to ${phone}:`, data);
})
.catch(error => {
console.error(`Failed to send SMS to ${phone}:`, error);
});
}
JavaScript✅ 2. Version Using axios (Node.js or Frontend with axios installed)
Make sure you install axios first:
npm install axios
BashThen use this version:
const axios = require('axios'); // Only needed in Node.js
function sendSms(phone, message) {
const encodedMessage = encodeURIComponent(message);
const url = `https://app.text.lk/api/http/sms/send?recipient=${phone}&sender_id=${senderId}&message=${encodedMessage}&api_token=${apiToken}`;
axios.get(url)
.then(response => {
console.log(`Message sent to ${phone}:`, response.data);
})
.catch(error => {
console.error(`Failed to send SMS to ${phone}:`, error);
});
}
BashBoth versions achieve the same goal. Use the one that matches your environment best.
Feel free to contact us if you need additional support.