Send SMS from PHP & Laravel: Kenya SMS API Tutorial
A step-by-step PHP and Laravel tutorial for sending SMS in Kenya. From Composer installation to notification channels and queued jobs — build reliable SMS messaging into your Laravel application.
Why Laravel for SMS in Kenya?
Laravel is the most popular PHP framework among Kenyan developers, and for good reason. Its elegant syntax, powerful queue system, and notification channels make it the perfect choice for building applications that need reliable SMS delivery. Whether you are building an e-commerce platform for Jumia-style shopping, a school management system for CBC curriculum tracking, or a SACCO portal for member communications, Laravel and KenyaSMS are a powerful combination.
Installation via Composer
Install the KenyaSMS PHP SDK using Composer:
composer require kenyasms/laravel
After installation, publish the configuration file:
php artisan vendor:publish --provider="KenyaSMS\Laravel\KenyaSMSServiceProvider"
This creates a config/kenyasms.php file where you set your API credentials.
Configuration
Add your KenyaSMS credentials to your .env file:
KENYASMS_API_KEY=your_api_key_here
KENYASMS_SENDER_ID=YOURSHOP
KENYASMS_BASE_URL=https://app.kenyasms.com/api/v1
The configuration file references these environment variables, keeping your credentials secure and environment-specific — different keys for staging and production.
Sending a Single SMS
The simplest way to send an SMS in your Laravel application:
use KenyaSMS\Laravel\Facades\KenyaSMS;
KenyaSMS::send(
to: '254712345678',
message: 'Your order #4521 has been confirmed. Delivery in 2-3 hours.'
);
This sends a single SMS and returns a response object containing the message_id, status, and credits_used.
Sending Bulk SMS
For bulk sending — say, a promotional campaign to all your customers in Nairobi — use the bulk method:
use KenyaSMS\Laravel\Facades\KenyaSMS;
$recipients = Customer::where('city', 'Nairobi')->pluck('phone')->toArray();
KenyaSMS::bulk(
recipients: $recipients,
message: 'Flash sale! 50% off all items this weekend at our Westlands store. Shop now!'
);
Using Laravel Notification Channels
The real power of the KenyaSMS Laravel package is its integration with Laravel's notification system. Create an SMS notification:
php artisan make:notification OrderShipped
Then implement the toKenyaSms method in your notification class:
use KenyaSMS\Laravel\KenyaSMSMessage;
public function via($notifiable): array
{
return ['kenyasms'];
}
public function toKenyaSms($notifiable): KenyaSMSMessage
{
return (new KenyaSMSMessage)
->content("Hi {$notifiable->name}, your order #{$this->order->id} has been shipped!")
->from('DUKAMART');
}
Your User model (or any notifiable model) needs a routeNotificationForKenyasms method:
public function routeNotificationForKenyasms(): string
{
return $this->phone; // Should be in 2547XXXXXXXX format
}
Now send the notification like any other Laravel notification:
$user->notify(new OrderShipped($order));
Queueing SMS Jobs for Performance
For high-volume sending, you do not want SMS API calls blocking your web requests. Queue them instead. Simply implement ShouldQueue on your notification:
use Illuminate\Contracts\Queue\ShouldQueue;
class OrderShipped extends Notification implements ShouldQueue
{
use Queueable;
// ... rest of notification
}
For even more control, create a dedicated job:
php artisan make:job SendBulkCampaign
Inside the job, chunk your recipients and send in batches:
public function handle(): void
{
$this->recipients->chunk(1000)->each(function ($chunk) {
KenyaSMS::bulk(
recipients: $chunk->pluck('phone')->toArray(),
message: $this->message
);
});
}
With Laravel's queue workers (which run under Supervisor in KenyaSMS's Docker setup), your bulk campaigns process in the background while your users continue browsing your site without delays.
Error Handling and Retries
Network hiccups happen. Wrap your SMS calls in try-catch blocks and leverage Laravel's retry mechanisms:
try {
$response = KenyaSMS::send(to: $phone, message: $text);
} catch (InsufficientCreditsException $e) {
Log::critical('SMS credits depleted', ['balance' => $e->getBalance()]);
// Alert admin to top up
} catch (KenyaSMSException $e) {
Log::error('SMS sending failed', ['error' => $e->getMessage()]);
// Will be retried by the queue worker
throw $e;
}
For queued jobs, set retry properties:
public int $tries = 3;
public int $backoff = 30; // seconds between retries
Real-World Use Cases in Kenya
Here is how Kenyan developers are using Laravel with KenyaSMS today:
- E-commerce (Nairobi) — order confirmations, shipping updates, delivery OTPs. A typical Kilimall-style marketplace sends 50,000+ transactional SMS per day.
- Schools (Kiambu, Machakos) — exam results via SMS to parents, fee reminders, school closure alerts. Many CBC-compliant school systems integrate SMS for parent communication.
- SACCOs (Kisumu, Nakuru) — loan approval notifications, repayment reminders, dividend announcements. SASRA compliance requires timely member communication.
- Healthcare (Mombasa) — appointment reminders, lab result notifications, prescription refill alerts.
Get Started
Install the package, configure your API key, and send your first SMS in under 5 minutes. Create your KenyaSMS account to get your API key. With direct routes to Safaricom, Airtel, and Telkom and a 99.5% delivery rate, your messages will reach your users every time.
Ready to Start Sending SMS?
Join thousands of Kenyan businesses using KenyaSMS. Get 10 free credits on signup.