Skip to main content

PHP SDK

The official afriroute/afriroute package provides a clean PHP client for SMS, Voice, WhatsApp, and Email — with automatic retries and Bearer-token authentication built in. Compatible with Laravel and Symfony.

Installation​

composer require afriroute/afriroute

Requires PHP 8.0 or newer. Then include Composer's autoloader:

require 'vendor/autoload.php';

Authentication​

Create a client with your API key. It is sent as Authorization: Bearer $AFRIROUTE_API_KEY against https://api.afriroute.ai.

use AfriRoute\Afriroute;

$client = new Afriroute('$AFRIROUTE_API_KEY');

Target the sandbox or tune behavior with named arguments:

$client = new Afriroute(
apiKey: '$AFRIROUTE_API_KEY',
baseUrl: 'https://api.afriroute.ai',
timeout: 60,
maxRetries: 5,
);
tip

Load your key from the environment: new Afriroute(getenv('AFRIROUTE_API_KEY')).

Send an SMS​

$response = $client->sms->send([
'to' => '+251900000000',
'message' => 'Hello from AfriRoute',
'sender_id' => 'AfriRoute',
]);

echo $response['message_id'];

Send a WhatsApp Message​

$wa = $client->whatsapp->send([
'to' => '+251900000000',
'message' => 'Hello via WhatsApp',
]);

echo $wa['message_id'];

Make a Voice Call​

$call = $client->voice->call([
'to' => '+251900000000',
'from' => '+1234567890',
'tts_message' => 'Welcome to AfriRoute',
]);

echo $call['call_id'];

Send an Email​

$email = $client->email->send([
'to' => '[email protected]',
'subject' => 'Welcome',
'body' => '<h1>Hello</h1>',
]);

echo $email['message_id'];

Error Handling​

Failed requests throw AfriRoute\Exception\AfriRouteException, exposing getCode(), getMessage(), and getStatusCode():

use AfriRoute\Exception\AfriRouteException;

try {
$client->sms->send([
'to' => '+251900000000',
'message' => 'Hello',
'sender_id' => 'AfriRoute',
]);
} catch (AfriRouteException $e) {
switch ($e->getErrorCode()) {
case 'INVALID_PHONE_NUMBER':
echo 'Use E.164 format, e.g. +251911234567';
break;
case 'INSUFFICIENT_BALANCE':
echo 'Top up your account';
break;
case 'RATE_LIMIT_EXCEEDED':
echo 'Slow down — retried automatically';
break;
default:
echo $e->getErrorCode() . ': ' . $e->getMessage();
}
}

Transient failures (429, 5xx) are retried automatically up to maxRetries with exponential back-off.

Laravel Integration​

Bind the client as a singleton in a service provider:

$this->app->singleton(Afriroute::class, fn () =>
new Afriroute(config('services.afriroute.key'))
);

Features​

  • Simple array-based request payloads
  • PSR-compatible HTTP layer
  • Automatic retries with exponential back-off
  • Built-in rate-limit (429) handling
  • Works with Laravel and Symfony

Last Updated: May 2026 · Need help? Contact Support →