Integrating an SMS API is one of the fastest ways to add communication capabilities to your Sri Lankan application. Whether you need OTP verification, transactional alerts, or marketing campaigns, the TXTMSG v3 REST API makes it straightforward.
Getting Started with TXTMSG SMS API
Before writing any code, you need API credentials. Sign up for a TXTMSG account and retrieve your API key from the dashboard. The API base URL is https://sms.txtmsg.lk/api/v3.
Sending Your First SMS with cURL
The quickest way to test the API is with cURL:
curl -X POST https://sms.txtmsg.lk/api/v3/sms/send \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d @- <
If successful, you will receive a response with the message SID and status:
{
"sid": "SM92f02b34...",
"status": "queued",
"date_created": "2026-07-10T10:00:00Z"
}
PHP SDK Integration
For PHP applications (Laravel, WordPress, custom frameworks):
<?php
$ch = curl_init("https://sms.txtmsg.lk/api/v3/sms/send");
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
"accept: application/json",
"Authorization: Bearer YOUR_API_KEY",
"Content-Type: application/json"
],
CURLOPT_POSTFIELDS => json_encode([
"recipient" => "+947XXXXXXXX",
"sender_id" => "YourBrand",
"type" => "plain",
"message" => "Your OTP is 8842"
])
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo "Status: $httpCode\nResponse: $response\n";
Python SDK Integration
For Python applications (Django, Flask, FastAPI):
import requests
url = "https://sms.txtmsg.lk/api/v3/sms/send"
headers = {
"Authorization": "Bearer YOUR_API_KEY",
"Content-Type": "application/json"
}
payload = {
"recipient": "+947XXXXXXXX",
"sender_id": "YourBrand",
"type": "plain",
"message": "Your OTP is 8842"
}
response = requests.post(url, json=payload, headers=headers)
print(response.status_code)
print(response.json())
Node.js SDK Integration
For Node.js applications (Express, NestJS, Next.js):
const fetch = require("node-fetch");
const url = "https://sms.txtmsg.lk/api/v3/sms/send";
const headers = {
"Authorization": "Bearer YOUR_API_KEY",
"Content-Type": "application/json"
};
const body = {
recipient: "+947XXXXXXXX",
sender_id: "YourBrand",
type: "plain",
message: "Your OTP is 8842"
};
fetch(url, { method: "POST", headers, body: JSON.stringify(body) })
.then(res => res.json())
.then(data => console.log(data));
OTP Verification Flow
Here is a complete OTP verification flow using the TXTMSG API:
- Send OTP — Generate a random 6-digit code and send it via the SMS API
- Store OTP — Save the code with an expiration timestamp (typically 5 minutes)
- Verify OTP — Compare user input against stored code and check expiration
- Invalidate OTP — Mark the code as used after successful verification
Best Practices for SMS API Integration
- Use HTTPS — Always use HTTPS endpoints, never send API keys over HTTP
- Implement Retry Logic — Handle transient failures with exponential backoff
- Monitor Delivery Status — Use webhooks to track message delivery status
- Rate Limiting — Respect API rate limits and implement client-side throttling
- Error Handling — Handle all HTTP error codes gracefully
FAQ
How long does SMS API integration take?
Basic integration can be done in under 30 minutes. OTP flows typically take 2-4 hours with proper storage and verification logic.
Does the TXTMSG API support Unicode SMS?
Yes, the API supports Unicode characters including Sinhala and Tamil scripts.
What are the rate limits?
Standard plans support up to 100 requests/second. Enterprise plans have custom limits.