Code Examples & Tutorials
Get started quickly with our multi-language code snippets and step-by-step tutorials
Send Single SMS (PHP)
<?php
// TxtMsg.lk SMS Gateway - Send Single SMS
$apiKey = 'YOUR_API_KEY';
$senderId = 'YOUR_SENDER_ID';
$recipient = '94712345678'; // Sri Lankan number
$message = 'Hello from TxtMsg.lk!';
$url = 'https://api.txtmsg.lk/send';
$data = [
'api_key' => $apiKey,
'sender_id' => $senderId,
'recipient' => $recipient,
'message' => $message
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response, true);
if ($result['success']) {
echo 'SMS sent successfully! ID: ' . $result['message_id'];
} else {
echo 'Error: ' . $result['error'];
}
?>Send Bulk SMS (PHP)
<?php
// TxtMsg.lk SMS Gateway - Send Bulk SMS
$apiKey = 'YOUR_API_KEY';
$senderId = 'YOUR_SENDER_ID';
$recipients = [
'94712345678',
'94712345679',
'94712345680'
];
$message = 'Bulk SMS from TxtMsg.lk!';
$url = 'https://api.txtmsg.lk/send-bulk';
$data = [
'api_key' => $apiKey,
'sender_id' => $senderId,
'recipients' => $recipients,
'message' => $message
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response, true);
if ($result['success']) {
echo 'Bulk SMS sent! Batch ID: ' . $result['batch_id'];
} else {
echo 'Error: ' . $result['error'];
}
?>Send SMS with Python
import requests
import json
# TxtMsg.lk SMS Gateway - Python Example
API_KEY = 'YOUR_API_KEY'
SENDER_ID = 'YOUR_SENDER_ID'
RECIPIENT = '94712345678'
MESSAGE = 'Hello from TxtMsg.lk via Python!'
url = 'https://api.txtmsg.lk/send'
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {API_KEY}'
}
data = {
'sender_id': SENDER_ID,
'recipient': RECIPIENT,
'message': MESSAGE
}
response = requests.post(url, json=data)
if response.status_code == 200:
result = response.json()
if result['success']:
print(f"SMS sent successfully! ID: {result['message_id']}")
else:
print(f"Error: {result['error']}")
else:
print(f"HTTP Error: {response.status_code}")Send SMS with Node.js
const axios = require('axios');
// TxtMsg.lk SMS Gateway - Node.js Example
const API_KEY = 'YOUR_API_KEY';
const SENDER_ID = 'YOUR_SENDER_ID';
const RECIPIENT = '94712345678';
const MESSAGE = 'Hello from TxtMsg.lk via Node.js!';
async function sendSMS() {
try {
const response = await axios.post('https://api.txtmsg.lk/send', {
api_key: API_KEY,
sender_id: SENDER_ID,
recipient: RECIPIENT,
message: MESSAGE
});
if (response.data.success) {
console.log(`SMS sent successfully! ID: ${response.data.message_id}`);
} else {
console.log(`Error: ${response.data.error}`);
}
} catch (error) {
console.error('Error sending SMS:', error.message);
}
}
sendSMS();WordPress Integration
<?php
// WordPress Integration with TxtMsg.lk SMS Gateway
function send_sms_notification($phone, $message) {
$api_key = get_option('txtmsg_api_key');
$sender_id = get_option('txtmsg_sender_id');
if (!$api_key || !$sender_id) {
return false;
}
$url = 'https://api.txtmsg.lk/send';
$data = array(
'api_key' => $api_key,
'sender_id' => $sender_id,
'recipient' => $phone,
'message' => $message
);
$response = wp_remote_post($url, array(
'body' => $data,
'timeout' => 30
));
if (is_wp_error($response)) {
error_log('SMS sending failed: ' . $response->get_error_message());
return false;
}
$result = json_decode(wp_remote_retrieve_body($response), true);
return $result['success'] ?? false;
}
// Hook into WooCommerce order completion
add_action('woocommerce_order_status_completed', function($order_id) {
$order = wc_get_order($order_id);
$phone = $order->get_billing_phone();
$message = "Thank you for your order! Order #{$order_id} is now complete.";
send_sms_notification($phone, $message);
});
?>Laravel SMS Service
<?php
// Laravel Service Class for TxtMsg.lk SMS Gateway
namespace App\Services;
use Illuminate\Support\Facades\Http;
class TxtMsgService
{
protected $apiKey;
protected $senderId;
protected $baseUrl = 'https://api.txtmsg.lk';
public function __construct()
{
$this->apiKey = config('services.txtmsg.api_key');
$this->senderId = config('services.txtmsg.sender_id');
}
public function sendSMS($recipient, $message)
{
$response = Http::post("{$this->baseUrl}/send", [
'api_key' => $this->apiKey,
'sender_id' => $this->senderId,
'recipient' => $recipient,
'message' => $message
]);
return $response->json();
}
public function sendBulkSMS($recipients, $message)
{
$response = Http::post("{$this->baseUrl}/send-bulk", [
'api_key' => $this->apiKey,
'sender_id' => $this->senderId,
'recipients' => $recipients,
'message' => $message
]);
return $response->json();
}
public function checkBalance()
{
$response = Http::get("{$this->baseUrl}/balance", [
'api_key' => $this->apiKey
]);
return $response->json();
}
}
// Usage in Controller
$smsService = new TxtMsgService();
$result = $smsService->sendSMS('94712345678', 'Hello from Laravel!');
Step-by-Step Tutorials
Learn how to implement common SMS features with detailed walkthroughs
OTP Verification System
Build a complete OTP verification system for user authentication
SMS Notifications for E-commerce
Set up automated order confirmations and delivery updates
Bulk SMS Campaign Manager
Create and manage SMS marketing campaigns with scheduling
Two-Factor Authentication
Implement secure 2FA using SMS verification
Complete API Reference
Access the full API documentation from documentation.txtmsg.lk