SMS API
Send SMS and OTP codes to Sri Lankan mobiles, track delivery, and manage sender IDs, contacts and giveaways from your own software. Every endpoint takes and returns JSON.
- Base URL
https://bestsms.lk/api/v1- Format
- JSON in, JSON out, UTF-8
- Time zone
- Asia/Colombo for every date
Every response has "ok". When something goes wrong you get an HTTP 4xx or 5xx status with an error object you can show or log.
{
"ok": true,
...
}{
"ok": false,
"error": {
"code": "sender_not_approved",
"message": "That sender ID is not approved on your account."
}
}Authentication
Create a key in Dashboard › API & webhooks. Keys start with bsk_live_ and are shown once, so store yours somewhere safe. You can lock a key to your server's IP addresses and revoke it at any time.
Send the key in one of these ways:
Authorization: Bearer <API_KEY>(recommended)X-API-Key: <API_KEY>?api_key=<API_KEY>, only for the simple GET /send link
Never put a key in a website's front-end code or a mobile app. Call the API from your server.
curl https://bestsms.lk/api/v1/balance \ -H "Authorization: Bearer bsk_live_xxxxxxxxxxxxxxxx"
Phone numbers and limits
Any Sri Lankan mobile number works in any of these forms. Numbers are cleaned to 947XXXXXXXX and duplicates in one request are sent once.
0771234567771234567+9477123456794771234567
Each key can make 120 requests a minute. Over that you get 429 rate_limited. One send request can have up to 100,000 numbers, so batch big lists into one call instead of calling once per number.
How SMS are counted
One credit sends one SMS part to one number. Long messages are split into parts and each part costs a credit. If a message has any Sinhala, Tamil, emoji or other character outside the standard SMS alphabet, the whole message is sent as Unicode.
| Text type | One SMS | Each part of a longer SMS |
|---|---|---|
| Standard (GSM) English text | 160 characters | 153 characters |
| Unicode: Sinhala, Tamil, emoji | 70 characters | 67 characters |
The characters ^ { } \ [ ] ~ | € count as two in standard text.
Account
Check your balance
GET/balance
Returns your remaining credits and the price per SMS on your account.
curl "https://bestsms.lk/api/v1/balance" \ -H "Authorization: Bearer $BESTSMS_KEY"
$ch = curl_init('https://bestsms.lk/api/v1/balance');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('BESTSMS_KEY'),
'Content-Type: application/json',
],
]);
$res = json_decode(curl_exec($ch), true);
const res = await fetch('https://bestsms.lk/api/v1/balance', {
method: 'GET',
headers: {
Authorization: `Bearer ${process.env.BESTSMS_KEY}`,
'Content-Type': 'application/json',
},
});
const data = await res.json();
import os, requests
r = requests.get(
"https://bestsms.lk/api/v1/balance",
headers={"Authorization": f"Bearer {os.environ['BESTSMS_KEY']}"},
)
print(r.json())
{
"ok": true,
"credits": 9480,
"price_per_sms": 1.3500000000000001
}SMS
Send SMS
POST/sms/send
Send one message to one or many numbers. Credits for every number are held when the request is accepted, and credits for numbers that fail are refunded when the campaign finishes. Up to 100 recipients are sent before the response returns; larger lists are queued and sent in the background.
| Field | Type | Details |
|---|---|---|
torequired | string | array | Numbers separated by commas, or an array. For personal messages pass an array of objects: {"phone", "name", "field1", "field2"} and use {name}, {field1}, {field2} in the message. |
messagerequired | string | The text. Up to 1,530 characters (10 SMS parts). |
sender_idrequired | string | An approved sender ID on your account. |
schedule_at | string | Send later, in Sri Lanka time. Format "2026-12-01 09:30". |
name | string | Campaign name shown in your dashboard. |
client_ref | string | Your own reference, returned with each message. |
curl -X POST "https://bestsms.lk/api/v1/sms/send" \
-H "Authorization: Bearer $BESTSMS_KEY" \
-H "Content-Type: application/json" \
-d '{
"to": "0771234567,0762289014",
"sender_id": "MYSHOP",
"message": "Your order #2041 is ready for pickup.",
"client_ref": "order-2041"
}'
$ch = curl_init('https://bestsms.lk/api/v1/sms/send');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('BESTSMS_KEY'),
'Content-Type: application/json',
],
CURLOPT_POSTFIELDS => json_encode([
'to' => '0771234567,0762289014',
'sender_id' => 'MYSHOP',
'message' => 'Your order #2041 is ready for pickup.',
'client_ref' => 'order-2041',
]),
]);
$res = json_decode(curl_exec($ch), true);
const res = await fetch('https://bestsms.lk/api/v1/sms/send', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.BESTSMS_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
"to": "0771234567,0762289014",
"sender_id": "MYSHOP",
"message": "Your order #2041 is ready for pickup.",
"client_ref": "order-2041"
}),
});
const data = await res.json();
import os, requests
r = requests.post(
"https://bestsms.lk/api/v1/sms/send",
headers={"Authorization": f"Bearer {os.environ['BESTSMS_KEY']}"},
json={
"to": "0771234567,0762289014",
"sender_id": "MYSHOP",
"message": "Your order #2041 is ready for pickup.",
"client_ref": "order-2041",
},
)
print(r.json())
{
"ok": true,
"campaign_id": 18342,
"status": "done",
"recipients": 2,
"credits_reserved": 2,
"invalid": 0,
"opted_out": 0,
"messages": [
{
"id": 904211,
"phone": "94771234567",
"status": "sent"
},
{
"id": 904212,
"phone": "94762289014",
"status": "sent"
}
]
}Send with a simple link
GET/send
For POS systems and devices that can only open a URL. Same response as POST /sms/send. Put the key in the api_key query parameter and URL-encode the message. Prefer POST /sms/send wherever you can, because links can end up in logs.
| Field | Type | Details |
|---|---|---|
api_keyrequired | string | Your API key. |
torequired | string | Numbers separated by commas. |
messagerequired | string | URL-encoded text. |
sender_idrequired | string | An approved sender ID. |
https://bestsms.lk/api/v1/send?api_key=bsk_live_xxxx&to=0771234567&sender_id=MYSHOP&message=Your%20bill%20is%20Rs%202%2C450.%20Thank%20you!
{
"ok": true,
"campaign_id": 18343,
"status": "done",
"recipients": 1,
"credits_reserved": 1,
"invalid": 0,
"opted_out": 0,
"messages": [
{
"id": 904213,
"phone": "94771234567",
"status": "sent"
}
]
}Get a message status
GET/sms/{id}
Status is one of queued, sent, delivered, failed or cancelled. Encoding is gsm (160 characters per SMS) or unicode (70).
| Field | Type | Details |
|---|---|---|
idrequired | integer | Message id from the send response. |
curl "https://bestsms.lk/api/v1/sms/904211" \ -H "Authorization: Bearer $BESTSMS_KEY"
$ch = curl_init('https://bestsms.lk/api/v1/sms/904211');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('BESTSMS_KEY'),
'Content-Type: application/json',
],
]);
$res = json_decode(curl_exec($ch), true);
const res = await fetch('https://bestsms.lk/api/v1/sms/904211', {
method: 'GET',
headers: {
Authorization: `Bearer ${process.env.BESTSMS_KEY}`,
'Content-Type': 'application/json',
},
});
const data = await res.json();
import os, requests
r = requests.get(
"https://bestsms.lk/api/v1/sms/904211",
headers={"Authorization": f"Bearer {os.environ['BESTSMS_KEY']}"},
)
print(r.json())
{
"ok": true,
"message": {
"id": 904211,
"campaign_id": 18342,
"phone": "94771234567",
"status": "delivered",
"segments": 1,
"encoding": "gsm",
"gateway_ref": "a91f0c2e",
"error": null,
"client_ref": "order-2041",
"created_at": "2026-12-01 09:30:02",
"sent_at": "2026-12-01 09:30:03",
"delivered_at": "2026-12-01 09:30:05"
}
}Campaigns
List campaigns
GET/campaigns
Newest first, 50 per page.
| Field | Type | Details |
|---|---|---|
page | integer | Page number, starting at 1. |
curl "https://bestsms.lk/api/v1/campaigns?page=1" \ -H "Authorization: Bearer $BESTSMS_KEY"
$ch = curl_init('https://bestsms.lk/api/v1/campaigns?page=1');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('BESTSMS_KEY'),
'Content-Type: application/json',
],
]);
$res = json_decode(curl_exec($ch), true);
const res = await fetch('https://bestsms.lk/api/v1/campaigns?page=1', {
method: 'GET',
headers: {
Authorization: `Bearer ${process.env.BESTSMS_KEY}`,
'Content-Type': 'application/json',
},
});
const data = await res.json();
import os, requests
r = requests.get(
"https://bestsms.lk/api/v1/campaigns?page=1",
headers={"Authorization": f"Bearer {os.environ['BESTSMS_KEY']}"},
)
print(r.json())
{
"ok": true,
"campaigns": [
{
"id": 18342,
"name": "Year-end sale",
"sender": "MYSHOP",
"status": "done",
"total": 2480,
"sent": 2443,
"delivered": 2381,
"failed": 37,
"credits_used": 2443,
"scheduled_at": null,
"created_at": "2026-12-01 09:30:00"
}
],
"page": 1,
"pages": 4
}Get one campaign
GET/campaigns/{id}
Returns the same fields as the list, for one campaign.
| Field | Type | Details |
|---|---|---|
idrequired | integer | Campaign id. |
curl "https://bestsms.lk/api/v1/campaigns/18342" \ -H "Authorization: Bearer $BESTSMS_KEY"
$ch = curl_init('https://bestsms.lk/api/v1/campaigns/18342');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('BESTSMS_KEY'),
'Content-Type: application/json',
],
]);
$res = json_decode(curl_exec($ch), true);
const res = await fetch('https://bestsms.lk/api/v1/campaigns/18342', {
method: 'GET',
headers: {
Authorization: `Bearer ${process.env.BESTSMS_KEY}`,
'Content-Type': 'application/json',
},
});
const data = await res.json();
import os, requests
r = requests.get(
"https://bestsms.lk/api/v1/campaigns/18342",
headers={"Authorization": f"Bearer {os.environ['BESTSMS_KEY']}"},
)
print(r.json())
{
"ok": true,
"campaign": {
"id": 18342,
"name": "Year-end sale",
"sender": "MYSHOP",
"status": "sending",
"total": 2480,
"sent": 1200,
"delivered": 980,
"failed": 4,
"credits_used": 1200,
"scheduled_at": null,
"created_at": "2026-12-01 09:30:00"
}
}Cancel a campaign
POST/campaigns/{id}/cancel
Stops a scheduled or sending campaign. Messages not yet sent are cancelled and their credits are refunded right away.
| Field | Type | Details |
|---|---|---|
idrequired | integer | Campaign id. |
curl -X POST "https://bestsms.lk/api/v1/campaigns/18350/cancel" \ -H "Authorization: Bearer $BESTSMS_KEY"
$ch = curl_init('https://bestsms.lk/api/v1/campaigns/18350/cancel');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('BESTSMS_KEY'),
'Content-Type: application/json',
],
]);
$res = json_decode(curl_exec($ch), true);
const res = await fetch('https://bestsms.lk/api/v1/campaigns/18350/cancel', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.BESTSMS_KEY}`,
'Content-Type': 'application/json',
},
});
const data = await res.json();
import os, requests
r = requests.post(
"https://bestsms.lk/api/v1/campaigns/18350/cancel",
headers={"Authorization": f"Bearer {os.environ['BESTSMS_KEY']}"},
)
print(r.json())
{
"ok": true,
"refunded": 2480
}Sender IDs
List sender IDs
GET/sender-ids
All sender IDs on your account and their status: pending, approved, rejected or revoked. Only approved ones can send.
curl "https://bestsms.lk/api/v1/sender-ids" \ -H "Authorization: Bearer $BESTSMS_KEY"
$ch = curl_init('https://bestsms.lk/api/v1/sender-ids');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('BESTSMS_KEY'),
'Content-Type: application/json',
],
]);
$res = json_decode(curl_exec($ch), true);
const res = await fetch('https://bestsms.lk/api/v1/sender-ids', {
method: 'GET',
headers: {
Authorization: `Bearer ${process.env.BESTSMS_KEY}`,
'Content-Type': 'application/json',
},
});
const data = await res.json();
import os, requests
r = requests.get(
"https://bestsms.lk/api/v1/sender-ids",
headers={"Authorization": f"Bearer {os.environ['BESTSMS_KEY']}"},
)
print(r.json())
{
"ok": true,
"sender_ids": [
{
"sender_id": "MYSHOP",
"status": "approved",
"created_at": "2026-11-28 10:12:00"
},
{
"sender_id": "MYSHOP SALE",
"status": "pending",
"created_at": "2026-12-01 08:01:00"
}
]
}Request a sender ID
POST/sender-ids
Asks our team to approve a new sender ID. It must be 3 to 11 characters: letters, digits, spaces, dots, & or -, with at least one letter.
| Field | Type | Details |
|---|---|---|
sender_idrequired | string | The name customers will see. |
business_namerequired | string | Your registered business or brand name. |
purposerequired | string | What you will send with it. |
sample_message | string | An example message. |
curl -X POST "https://bestsms.lk/api/v1/sender-ids" \
-H "Authorization: Bearer $BESTSMS_KEY" \
-H "Content-Type: application/json" \
-d '{
"sender_id": "MYSHOP",
"business_name": "My Shop (Pvt) Ltd",
"purpose": "Order updates and offers for our customers",
"sample_message": "Your order #2041 is ready for pickup."
}'
$ch = curl_init('https://bestsms.lk/api/v1/sender-ids');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('BESTSMS_KEY'),
'Content-Type: application/json',
],
CURLOPT_POSTFIELDS => json_encode([
'sender_id' => 'MYSHOP',
'business_name' => 'My Shop (Pvt) Ltd',
'purpose' => 'Order updates and offers for our customers',
'sample_message' => 'Your order #2041 is ready for pickup.',
]),
]);
$res = json_decode(curl_exec($ch), true);
const res = await fetch('https://bestsms.lk/api/v1/sender-ids', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.BESTSMS_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
"sender_id": "MYSHOP",
"business_name": "My Shop (Pvt) Ltd",
"purpose": "Order updates and offers for our customers",
"sample_message": "Your order #2041 is ready for pickup."
}),
});
const data = await res.json();
import os, requests
r = requests.post(
"https://bestsms.lk/api/v1/sender-ids",
headers={"Authorization": f"Bearer {os.environ['BESTSMS_KEY']}"},
json={
"sender_id": "MYSHOP",
"business_name": "My Shop (Pvt) Ltd",
"purpose": "Order updates and offers for our customers",
"sample_message": "Your order #2041 is ready for pickup.",
},
)
print(r.json())
{
"ok": true,
"id": 57,
"status": "pending"
}OTP
Send an OTP code
POST/otp/send
Generates a random code, texts it and stores only a hash of it. Sending a new code to the same number replaces the old one.
| Field | Type | Details |
|---|---|---|
torequired | string | One mobile number. |
sender_idrequired | string | An approved sender ID. |
length | integer | Digits, 4 to 8. Default 6. |
expiry | integer | Seconds the code stays valid, 60 to 1800. Default 300. |
message | string | Must contain {code}. Default "Your verification code is {code}". |
curl -X POST "https://bestsms.lk/api/v1/otp/send" \
-H "Authorization: Bearer $BESTSMS_KEY" \
-H "Content-Type: application/json" \
-d '{
"to": "0771234567",
"sender_id": "MYSHOP",
"length": 6,
"expiry": 300,
"message": "Your MyShop login code is {code}. It expires in 5 minutes."
}'
$ch = curl_init('https://bestsms.lk/api/v1/otp/send');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('BESTSMS_KEY'),
'Content-Type: application/json',
],
CURLOPT_POSTFIELDS => json_encode([
'to' => '0771234567',
'sender_id' => 'MYSHOP',
'length' => 6,
'expiry' => 300,
'message' => 'Your MyShop login code is {code}. It expires in 5 minutes.',
]),
]);
$res = json_decode(curl_exec($ch), true);
const res = await fetch('https://bestsms.lk/api/v1/otp/send', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.BESTSMS_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
"to": "0771234567",
"sender_id": "MYSHOP",
"length": 6,
"expiry": 300,
"message": "Your MyShop login code is {code}. It expires in 5 minutes."
}),
});
const data = await res.json();
import os, requests
r = requests.post(
"https://bestsms.lk/api/v1/otp/send",
headers={"Authorization": f"Bearer {os.environ['BESTSMS_KEY']}"},
json={
"to": "0771234567",
"sender_id": "MYSHOP",
"length": 6,
"expiry": 300,
"message": "Your MyShop login code is {code}. It expires in 5 minutes.",
},
)
print(r.json())
{
"ok": true,
"otp_id": 3311,
"expires_at": "2026-12-01 09:35:00"
}Verify an OTP code
POST/otp/verify
Checks a code. A code works once. After 5 wrong tries it is locked and you get otp_too_many_attempts.
| Field | Type | Details |
|---|---|---|
torequired | string | The number the code was sent to (or pass otp_id instead). |
otp_id | integer | From the send response. |
coderequired | string | What the person typed. |
curl -X POST "https://bestsms.lk/api/v1/otp/verify" \
-H "Authorization: Bearer $BESTSMS_KEY" \
-H "Content-Type: application/json" \
-d '{
"to": "0771234567",
"code": "482916"
}'
$ch = curl_init('https://bestsms.lk/api/v1/otp/verify');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('BESTSMS_KEY'),
'Content-Type: application/json',
],
CURLOPT_POSTFIELDS => json_encode([
'to' => '0771234567',
'code' => '482916',
]),
]);
$res = json_decode(curl_exec($ch), true);
const res = await fetch('https://bestsms.lk/api/v1/otp/verify', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.BESTSMS_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
"to": "0771234567",
"code": "482916"
}),
});
const data = await res.json();
import os, requests
r = requests.post(
"https://bestsms.lk/api/v1/otp/verify",
headers={"Authorization": f"Bearer {os.environ['BESTSMS_KEY']}"},
json={
"to": "0771234567",
"code": "482916",
},
)
print(r.json())
{
"ok": true,
"verified": true
}Contacts
List contact groups
GET/groups
Your contact groups with the number of contacts in each.
curl "https://bestsms.lk/api/v1/groups" \ -H "Authorization: Bearer $BESTSMS_KEY"
$ch = curl_init('https://bestsms.lk/api/v1/groups');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('BESTSMS_KEY'),
'Content-Type: application/json',
],
]);
$res = json_decode(curl_exec($ch), true);
const res = await fetch('https://bestsms.lk/api/v1/groups', {
method: 'GET',
headers: {
Authorization: `Bearer ${process.env.BESTSMS_KEY}`,
'Content-Type': 'application/json',
},
});
const data = await res.json();
import os, requests
r = requests.get(
"https://bestsms.lk/api/v1/groups",
headers={"Authorization": f"Bearer {os.environ['BESTSMS_KEY']}"},
)
print(r.json())
{
"ok": true,
"groups": [
{
"id": 4,
"name": "Loyalty members",
"contacts": 2480
},
{
"id": 7,
"name": "Wholesale",
"contacts": 112
}
]
}Add a contact
POST/contacts
Adds a number to a group. Pass group (a name) instead of group_id and the group is created if it does not exist. Adding a number that is already in the group updates its name and fields.
| Field | Type | Details |
|---|---|---|
group_id | integer | Existing group id. |
group | string | Group name, created if missing. |
phonerequired | string | Mobile number. |
name | string | Used by {name}. |
field1 | string | Used by {field1}. |
field2 | string | Used by {field2}. |
curl -X POST "https://bestsms.lk/api/v1/contacts" \
-H "Authorization: Bearer $BESTSMS_KEY" \
-H "Content-Type: application/json" \
-d '{
"group": "Loyalty members",
"phone": "0771234567",
"name": "Nimal",
"field1": "1,250 points"
}'
$ch = curl_init('https://bestsms.lk/api/v1/contacts');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('BESTSMS_KEY'),
'Content-Type: application/json',
],
CURLOPT_POSTFIELDS => json_encode([
'group' => 'Loyalty members',
'phone' => '0771234567',
'name' => 'Nimal',
'field1' => '1,250 points',
]),
]);
$res = json_decode(curl_exec($ch), true);
const res = await fetch('https://bestsms.lk/api/v1/contacts', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.BESTSMS_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
"group": "Loyalty members",
"phone": "0771234567",
"name": "Nimal",
"field1": "1,250 points"
}),
});
const data = await res.json();
import os, requests
r = requests.post(
"https://bestsms.lk/api/v1/contacts",
headers={"Authorization": f"Bearer {os.environ['BESTSMS_KEY']}"},
json={
"group": "Loyalty members",
"phone": "0771234567",
"name": "Nimal",
"field1": "1,250 points",
},
)
print(r.json())
{
"ok": true,
"contact_id": 88412
}Add an opt-out
POST/optouts
Numbers on your opt-out list are skipped in every campaign. OTP messages still go through.
| Field | Type | Details |
|---|---|---|
phonerequired | string | Mobile number. |
reason | string | Why, for your records. |
curl -X POST "https://bestsms.lk/api/v1/optouts" \
-H "Authorization: Bearer $BESTSMS_KEY" \
-H "Content-Type: application/json" \
-d '{
"phone": "0771234567",
"reason": "Replied STOP"
}'
$ch = curl_init('https://bestsms.lk/api/v1/optouts');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('BESTSMS_KEY'),
'Content-Type: application/json',
],
CURLOPT_POSTFIELDS => json_encode([
'phone' => '0771234567',
'reason' => 'Replied STOP',
]),
]);
$res = json_decode(curl_exec($ch), true);
const res = await fetch('https://bestsms.lk/api/v1/optouts', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.BESTSMS_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
"phone": "0771234567",
"reason": "Replied STOP"
}),
});
const data = await res.json();
import os, requests
r = requests.post(
"https://bestsms.lk/api/v1/optouts",
headers={"Authorization": f"Bearer {os.environ['BESTSMS_KEY']}"},
json={
"phone": "0771234567",
"reason": "Replied STOP",
},
)
print(r.json())
{
"ok": true
}Giveaways
List giveaways
GET/giveaways
Your giveaways with their public link and entry count. Type is draw, quiz, instant or referral.
curl "https://bestsms.lk/api/v1/giveaways" \ -H "Authorization: Bearer $BESTSMS_KEY"
$ch = curl_init('https://bestsms.lk/api/v1/giveaways');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('BESTSMS_KEY'),
'Content-Type: application/json',
],
]);
$res = json_decode(curl_exec($ch), true);
const res = await fetch('https://bestsms.lk/api/v1/giveaways', {
method: 'GET',
headers: {
Authorization: `Bearer ${process.env.BESTSMS_KEY}`,
'Content-Type': 'application/json',
},
});
const data = await res.json();
import os, requests
r = requests.get(
"https://bestsms.lk/api/v1/giveaways",
headers={"Authorization": f"Bearer {os.environ['BESTSMS_KEY']}"},
)
print(r.json())
{
"ok": true,
"giveaways": [
{
"slug": "xmas-hamper",
"title": "Win a Christmas hamper",
"type": "draw",
"status": "live",
"entries": 3214,
"link": "https://bestsms.lk/g/xmas-hamper",
"ends_at": "2026-12-25 23:59:00"
}
]
}List giveaway entries
GET/giveaways/{slug}/entries
Entries for one giveaway, oldest first, 100 per page.
| Field | Type | Details |
|---|---|---|
slugrequired | string | The giveaway slug. |
page | integer | Page number. |
curl "https://bestsms.lk/api/v1/giveaways/xmas-hamper/entries?page=1" \ -H "Authorization: Bearer $BESTSMS_KEY"
$ch = curl_init('https://bestsms.lk/api/v1/giveaways/xmas-hamper/entries?page=1');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('BESTSMS_KEY'),
'Content-Type: application/json',
],
]);
$res = json_decode(curl_exec($ch), true);
const res = await fetch('https://bestsms.lk/api/v1/giveaways/xmas-hamper/entries?page=1', {
method: 'GET',
headers: {
Authorization: `Bearer ${process.env.BESTSMS_KEY}`,
'Content-Type': 'application/json',
},
});
const data = await res.json();
import os, requests
r = requests.get(
"https://bestsms.lk/api/v1/giveaways/xmas-hamper/entries?page=1",
headers={"Authorization": f"Bearer {os.environ['BESTSMS_KEY']}"},
)
print(r.json())
{
"ok": true,
"entries": [
{
"name": "Nimal Perera",
"phone": "94771234567",
"email": null,
"verified": true,
"referrals": 3,
"bonus_entries": 6,
"created_at": "2026-12-02 18:22:10",
"answers": {
"city": "Kandy"
}
}
],
"page": 1,
"pages": 33
}Reference
Errors
Check ok first. When it is false, error.code is stable and safe to branch on; error.message is written for people.
| Code | HTTP | Meaning |
|---|---|---|
unauthorized | 401 | The API key is missing, wrong or revoked. |
forbidden_ip | 403 | The key has an IP allow-list and this request came from another IP. |
rate_limited | 429 | More than 120 requests in a minute on this key. Wait and retry. |
invalid_request | 422 | A field is missing or not valid. The message says which one. |
sender_not_approved | 422 | The sender ID is not approved on your account. |
insufficient_credits | 402 | Not enough credits for this send. Nothing was sent. |
not_found | 404 | The message, campaign or giveaway does not exist on your account. |
otp_invalid | 422 | The code is wrong. |
otp_expired | 422 | The code is older than its expiry time. Send a new one. |
otp_too_many_attempts | 429 | Five wrong tries. Send a new code. |
server_error | 500 | Something failed on our side. Retry, and contact us if it keeps happening. |
{
"ok": false,
"error": {
"code": "insufficient_credits",
"message": "Not enough credits. This needs 2,480 credits and you have 1,200."
}
}Webhooks
Add a webhook URL in Dashboard › API & webhooks and copy the signing secret. We POST a JSON body to your URL when something happens. Reply with any 2xx status within 6 seconds.
| Event | When |
|---|---|
campaign.finished | Every number in a campaign has been sent or has failed. Includes totals and credits used. |
message.delivered | The network confirms a message reached the phone. |
balance.low | Your credits drop below the alert level you set. |
giveaway.entry | Someone enters one of your giveaways. |
Every call has these headers:
X-BestSMS-Event: the event nameX-BestSMS-Signature:sha256=followed by the hex HMAC-SHA256 of the raw request body, keyed with your secret
Compute the HMAC over the raw body before parsing it, and compare with a constant-time function.
POST /your/webhook
X-BestSMS-Event: campaign.finished
X-BestSMS-Signature: sha256=5d1c0f...
{
"event": "campaign.finished",
"time": "2026-12-01T09:34:12+05:30",
"data": {
"campaign_id": 18342,
"status": "done",
"total": 2480,
"sent": 2443,
"failed": 37,
"credits_used": 2443
}
}
<?php
$secret = getenv('BESTSMS_WEBHOOK_SECRET');
$raw = file_get_contents('php://input');
$sig = $_SERVER['HTTP_X_BESTSMS_SIGNATURE'] ?? '';
$expected = 'sha256=' . hash_hmac('sha256', $raw, $secret);
if (!hash_equals($expected, $sig)) {
http_response_code(401);
exit;
}
$event = json_decode($raw, true);
if ($event['event'] === 'campaign.finished') {
// update your records
}
http_response_code(200);
import crypto from 'node:crypto';
import express from 'express';
const app = express();
app.post('/bestsms', express.raw({ type: 'application/json' }), (req, res) => {
const expected = 'sha256=' + crypto
.createHmac('sha256', process.env.BESTSMS_WEBHOOK_SECRET)
.update(req.body)
.digest('hex');
const got = req.get('X-BestSMS-Signature') || '';
const ok = got.length === expected.length &&
crypto.timingSafeEqual(Buffer.from(got), Buffer.from(expected));
if (!ok) return res.sendStatus(401);
const event = JSON.parse(req.body);
// event.event, event.data
res.sendStatus(200);
});
Ready to try it?
Create an account, request a sender ID and make a key. Your dashboard shows every API call's messages under Delivery reports.