Code Examples

Ready-to-use code snippets for common tasks.

Send SMS

PHP

<?php
$url = 'https://api.opensms.co.ke/v3/sms/send';
$data = [
    'phone' => '+254712345678',
    'message' => 'Hello World!'
];

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Authorization: Bearer YOUR_API_KEY',
    'Content-Type: application/json'
]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

$response = curl_exec($ch);
$result = json_decode($response, true);
print_r($result);
?>

Python

import requests

url = 'https://api.opensms.co.ke/v3/sms/send'
headers = {
    'Authorization': 'Bearer YOUR_API_KEY',
    'Content-Type': 'application/json'
}
data = {
    'phone': '+254712345678',
    'message': 'Hello World!'
}

response = requests.post(url, json=data, headers=headers)
result = response.json()
print(result)

Node.js

const axios = require('axios');

const url = 'https://api.opensms.co.ke/v3/sms/send';
const headers = {
    'Authorization': 'Bearer YOUR_API_KEY',
    'Content-Type': 'application/json'
};
const data = {
    phone: '+254712345678',
    message: 'Hello World!'
};

axios.post(url, data, { headers })
    .then(response => console.log(response.data))
    .catch(error => console.error(error));

Java

import java.net.HttpURLConnection;
import java.net.URL;
import java.io.OutputStream;

public class SendSMS {
    public static void main(String[] args) throws Exception {
        String url = "https://api.opensms.co.ke/v3/sms/send";
        String json = "{\"phone\": \"+254712345678\", \"message\": \"Hello World!\"}";
        
        HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Authorization", "Bearer YOUR_API_KEY");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setDoOutput(true);
        
        try(OutputStream os = conn.getOutputStream()) {
            byte[] input = json.getBytes("utf-8");
            os.write(input, 0, input.length);
        }
        
        System.out.println("Response Code: " + conn.getResponseCode());
    }
}

Batch Send SMS

PHP

<?php
$url = 'https://api.opensms.co.ke/v3/sms/batch';
$data = [
    'messages' => [
        ['phone' => '+254712345678', 'message' => 'Hello 1'],
        ['phone' => '+254712345679', 'message' => 'Hello 2'],
        ['phone' => '+254712345680', 'message' => 'Hello 3']
    ]
];

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Authorization: Bearer YOUR_API_KEY',
    'Content-Type: application/json'
]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

$response = curl_exec($ch);
$result = json_decode($response, true);
print_r($result);
?>

Python

import requests

url = 'https://api.opensms.co.ke/v3/sms/batch'
headers = {
    'Authorization': 'Bearer YOUR_API_KEY',
    'Content-Type': 'application/json'
}
data = {
    'messages': [
        {'phone': '+254712345678', 'message': 'Hello 1'},
        {'phone': '+254712345679', 'message': 'Hello 2'},
        {'phone': '+254712345680', 'message': 'Hello 3'}
    ]
}

response = requests.post(url, json=data, headers=headers)
result = response.json()
print(result)

Send OTP

PHP

<?php
$url = 'https://api.opensms.co.ke/v3/otp/send';
$data = [
    'phone' => '+254712345678',
    'length' => 6,
    'expiry' => 300
];

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Authorization: Bearer YOUR_API_KEY',
    'Content-Type: application/json'
]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

$response = curl_exec($ch);
$result = json_decode($response, true);
print_r($result);
?>

Get Account Balance

PHP

<?php
$url = 'https://api.opensms.co.ke/v3/account';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Authorization: Bearer YOUR_API_KEY'
]);

$response = curl_exec($ch);
$result = json_decode($response, true);
echo "Balance: " . $result['balance'];
?>

Python

import requests

url = 'https://api.opensms.co.ke/v3/account'
headers = {
    'Authorization': 'Bearer YOUR_API_KEY'
}

response = requests.get(url, headers=headers)
result = response.json()
print(f"Balance: {result['balance']}")

Next Steps