SendAfrica logoSendAfricaDocs

PHP SDK

composer require sendafrica/sendafrica — resource-based architecture on cURL with typed models, retries, and notifications support.


Packagesendafrica/sendafrica on Packagist · v1.0.0 · stable
Installcomposer require sendafrica/sendafrica
RequiresPHP 7.4+, ext-curl, ext-json
ArchitectureResource-based (sms, credits, payments, auth, webhooks, notifications)

#Installation & setup

auth.php
php
<?php
require 'vendor/autoload.php';

use SendAfrica\SendAfrica;

// Uses the SENDAFRICA_API_KEY environment variable
$client = new SendAfrica();

// Or pass the key directly
$client = new SendAfrica("SA-xxxxx");

// Tune base URL, timeout, retries, debugging, webhook secret
$client = new SendAfrica(
    "SA-xxxxx",
    "https://api.sendafrica.online/v1",
    10,            // timeout (seconds)
    3,             // max retries on 429/5xx
    "production",
    false,         // debug logging
    "whsec_..."    // webhook secret
);

#Sending SMS

send.php
php
<?php
$result = $client->sms->send("0712345678", "Your OTP is 123456");
echo $result->messageId . "\n";    // "SA-xxxx-xxxx-xxxx"
echo $result->status . "\n";       // "sent"
echo $result->creditsUsed . "\n";  // 1

// Preview encoding/credits before sending
$analysis = $client->sms->analyze("Hello from SendAfrica!");
echo $analysis->parts . " parts\n";

#Bulk sending

bulk.php
php
<?php
$result = $client->sms->sendMany([
    ["to" => "+255711111111", "message" => "Hello John"],
    ["to" => "+255722222222", "message" => "Hello Mary"],
]);

echo $result->getSentCount() . " sent\n";
echo $result->getFailedCount() . " failed\n";

foreach ($result->results as $r) {
    echo $r->to . ": " . $r->status . "\n";
}

#Credits, payments & notifications

account.php
php
<?php
$balance = $client->credits->balance();
echo $balance->accountId . ": " . $balance->balance . " credits\n";

$transactions = $client->credits->history(1, 50);
foreach ($transactions as $tx) {
    echo $tx->id . " " . $tx->type . " " . $tx->amount . "\n";
}

// Tiered rate card
$rate = $client->payments->rate();
foreach ($rate->tiers as $tier) {
    echo "Up to " . $tier->maxAmountTzs . " TZS: "
         . $tier->rateTzsPerCredit . " TZS/credit\n";
}

// In-app notifications
$unread = $client->notifications->unreadCount();
$client->notifications->markAllRead();

#Webhooks

webhooks.php
php
<?php
$rawBody = file_get_contents("php://input");
$signature = $_SERVER["HTTP_X_SENDAFRICA_SIGNATURE"] ?? "";

$event = $client->webhooks->parse($rawBody, $signature, "whsec_...");

if ($event->type === "sms.delivered") {
    echo "Message " . $event->messageId . " delivered\n";
}

#Error handling

errors.php
php
<?php
use SendAfrica\Exceptions\SendAfricaException;
use SendAfrica\Exceptions\InsufficientCreditsException;
use SendAfrica\Exceptions\RateLimitException;

try {
    $client->sms->send("0712345678", "Hello");
} catch (InsufficientCreditsException $e) {
    echo "Not enough credits: " . $e->getMessage();
} catch (RateLimitException $e) {
    echo "Rate limited, retry later";
} catch (SendAfricaException $e) {
    echo "API error: " . $e->getMessage();
}