G K G Guru

JavaScript SMS API Integration

Complete JavaScript code examples for integrating with the SecureSMSC SMS API using fetch.

Fast Integration

Get started in minutes with simple REST API calls

Global Reach

Send SMS to 200+ countries worldwide

Secure

Enterprise-grade SSL/TLS encryption

Reliable

99.9% uptime with delivery tracking

Quick Start: Get your API key from the dashboard to begin integration.

Installation

Install the required dependencies:

Terminal
# No installation needed - fetch is built-in

Send Single SMS

JavaScript
const API_KEY = 'YOUR_API_KEY';
const API_URL = 'https://whitelabled.securesmsc.com/api/v2/sms/send';

async function sendSMS(recipient, message) {
    const response = await fetch(API_URL, {
        method: 'POST',
        headers: {
            'Authorization': `Bearer ${API_KEY}`,
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            sender_id: 'SENDER',
            recipient: recipient,
            message: message,
            route: 'transactional'
        })
    });
    
    const data = await response.json();
    
    if (response.ok) {
        console.log('Success:', data.message);
        return data;
    } else {
        throw new Error(data.error || 'Failed to send SMS');
    }
}

// Usage
sendSMS('1234567890', 'Hello from JavaScript!')
    .then(result => console.log(result))
    .catch(error => console.error(error));

Send Bulk SMS

JavaScript
async function sendBulkSMS(recipients, message) {
    const response = await fetch(
        'https://whitelabled.securesmsc.com/api/v2/sms/send-bulk',
        {
            method: 'POST',
            headers: {
                'Authorization': `Bearer ${API_KEY}`,
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                sender_id: 'SENDER',
                recipients: recipients,
                message: message,
                route: 'promotional'
            })
        }
    );
    return response.json();
}

// Usage
sendBulkSMS(['1234567890', '0987654321'], 'Bulk message!');

Check SMS Status

JavaScript
async function checkStatus(messageId) {
    const response = await fetch(
        `https://whitelabled.securesmsc.com/api/v2/sms/status/${messageId}`,
        {
            headers: { 'Authorization': `Bearer ${API_KEY}` }
        }
    );
    const data = await response.json();
    console.log('Status:', data.data.status);
    return data;
}

Check Balance

JavaScript
async function getBalance() {
    const response = await fetch(
        'https://whitelabled.securesmsc.com/api/v2/balance',
        {
            headers: { 'Authorization': `Bearer ${API_KEY}` }
        }
    );
    const data = await response.json();
    console.log('Balance:', data.data.balance, 'credits');
    return data;
}

Error Handling

JavaScript - Browser/React
class SMSClient {
    constructor(apiKey) {
        this.apiKey = apiKey;
        this.baseUrl = 'https://whitelabled.securesmsc.com/api/v2';
    }

    async send(recipient, message, retries = 3) {
        for (let i = 0; i < retries; i++) {
            try {
                const response = await fetch(`${this.baseUrl}/sms/send`, {
                    method: 'POST',
                    headers: {
                        'Authorization': `Bearer ${this.apiKey}`,
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        sender_id: 'SENDER',
                        recipient,
                        message,
                        route: 'transactional'
                    })
                });

                if (response.ok) {
                    return await response.json();
                }
                throw new Error(`HTTP ${response.status}`);
            } catch (error) {
                if (i === retries - 1) throw error;
                await new Promise(r => setTimeout(r, Math.pow(2, i) * 1000));
            }
        }
    }
}

// Usage
const sms = new SMSClient('YOUR_API_KEY');
sms.send('1234567890', 'Hello!')
    .then(result => console.log(result))
    .catch(error => console.error(error));