PHP code example of tuseme / sdk

1. Go to this page and download the library: Download tuseme/sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tuseme / sdk example snippets



Tuseme\TusemeClient;

$client = new TusemeClient([
    'api_key'    => 'tk_test_your_api_key',
    'api_secret' => 'sk_test_your_api_secret',
]);

$response = $client->messages->send([
    'content'    => 'Hello from Tuseme! Your OTP is 482910.',
    'sender_id'  => 'TUSEME-LTD',
    'recipients' => [
        ['msisdn' => '+254712345678', 'name' => 'John Doe']
    ],
    'type'     => 'transactional',
    'priority' => 'HIGH',
]);

echo "Message ID: " . $response['message_id'] . "\n";
echo "Status: " . $response['status'] . "\n";

// Sandbox credentials (for testing)
$client = new TusemeClient(['api_key' => 'tk_test_...', 'api_secret' => 'sk_test_...']);

// Production credentials
$client = new TusemeClient(['api_key' => 'tk_live_...', 'api_secret' => 'sk_live_...']);

// Single recipient
$response = $client->messages->send([
    'content'    => 'Your verification code is 123456',
    'sender_id'  => 'TUSEME-LTD',
    'recipients' => [['msisdn' => '+254712345678']],
    'type'       => 'transactional',
]);

// Multiple recipients with metadata
$response = $client->messages->send([
    'content'    => 'Flash sale! 50% off today only.',
    'sender_id'  => 'TUSEME-LTD',
    'recipients' => [
        ['msisdn' => '+254712345678', 'name' => 'Alice'],
        ['msisdn' => '+254798765432', 'name' => 'Bob'],
    ],
    'type'     => 'promotional',
    'metadata' => ['campaign' => 'flash_sale_q2'],
]);

$status = $client->messages->get('msg_a1b2c3d4...');
echo "Status: " . $status['status'] . "\n";
echo "Delivered at: " . $status['delivered_at'] . "\n";

$result = $client->messages->list(['page' => 1, 'page_size' => 20, 'status' => 'delivered']);
foreach ($result['data'] as $msg) {
    echo $msg['recipient'] . ': ' . $msg['status'] . "\n";
}

use Tuseme\TusemeException;

try {
    $response = $client->messages->send([
        'content'    => 'Hello!',
        'recipients' => [['msisdn' => '+254712345678']],
    ]);
} catch (TusemeException $e) {
    if ($e->getCode() === 401) {
        echo "Invalid credentials — check your API key and secret\n";
    } elseif ($e->getCode() === 400) {
        echo "Bad request: " . $e->getMessage() . "\n";
    } elseif ($e->getCode() === 429) {
        echo "Rate limited — try again later\n";
    }
}

$client = new TusemeClient([
    'api_key'     => '...',
    'api_secret'  => '...',
    'base_url'    => 'https://api.tuseme.co.ke/api/v1',  // default
    'timeout'     => 30,   // request timeout in seconds
    'max_retries' => 3,    // automatic retries on failure
]);