1. Go to this page and download the library: Download itxshakil/laravel-fast2sms 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/ */
itxshakil / laravel-fast2sms example snippets
use Shakil\Fast2sms\Facades\Fast2sms;
// Send a quick SMS
$response = Fast2sms::quick(
numbers: '9876543210',
message: 'Your OTP is 123456',
);
if ($response->isSuccess()) {
echo 'SMS sent! Request ID: ' . $response->requestId;
}
use Shakil\Fast2sms\Notifications\Messages\SmsMessage;
$msg = new SmsMessage('Your OTP is 123456');
$msg->charCount(); // 22
$msg->isUnicode(); // false
$msg->creditCount(); // 1
$msg->exceedsOneSms(); // false
use Shakil\Fast2sms\Facades\Fast2sms;
Fast2sms::quick(numbers: '9876543210', message: 'Hello from Fast2SMS!');
use Illuminate\Notifications\Notification;
use Shakil\Fast2sms\Enums\SmsRoute;
use Shakil\Fast2sms\Notifications\Messages\SmsMessage;
class OrderShipped extends Notification
{
public function via(object $notifiable): array
{
return ['fast2sms'];
}
public function toSms(object $notifiable): SmsMessage
{
return SmsMessage::create("Your order #{$this->order->id} has shipped!")
->withRoute(SmsRoute::QUICK);
}
}
// app/Models/User.php
public function routeNotificationForFast2sms(): string
{
return $this->phone_number;
}
use Illuminate\Notifications\Notification;
use Shakil\Fast2sms\Notifications\Messages\WhatsAppMessage;
class OrderShippedWhatsApp extends Notification
{
public function via(object $notifiable): array
{
return ['whatsapp'];
}
public function toWhatsApp(object $notifiable): WhatsAppMessage
{
return WhatsAppMessage::text("Your order #{$this->order->id} has shipped!");
}
}
// app/Models/User.php
public function routeNotificationForWhatsapp(): string
{
return $this->phone_number;
}
// Assert nothing was sent (SMS or WhatsApp)
Fast2sms::assertNothingSent();
// Assert total sends across both channels (counts typed SMS + WhatsApp records)
Fast2sms::assertSentCount(3);
// Assert exact total sends via raw sentMessages log entries
Fast2sms::assertSentTimes(3);
// Generic low-level assertion with optional closure (closure receives raw array payload)
Fast2sms::assertSent(fn (array $message) => $message['numbers'] === ['9876543210']);
// Assert no message matching criteria was sent
Fast2sms::assertNotSent(fn (array $message) => $message['numbers'] === ['9876543210']);
protected function tearDown(): void
{
Fast2sms::stopFaking();
parent::tearDown();
}
use Shakil\Fast2sms\Rules\Fast2smsPhone;
$request->validate([
'phone' => ['
use Shakil\Fast2sms\Exceptions\ApiException;
use Shakil\Fast2sms\Exceptions\AuthenticationException;
use Shakil\Fast2sms\Exceptions\Fast2smsException;
use Shakil\Fast2sms\Exceptions\NetworkException;
use Shakil\Fast2sms\Exceptions\RateLimitException;
use Shakil\Fast2sms\Exceptions\ValidationException;
try {
Fast2sms::quick(numbers: '9876543210', message: 'Hello!');
} catch (AuthenticationException $e) {
// Invalid API key — check FAST2SMS_API_KEY
} catch (RateLimitException $e) {
// Too many requests — back off and retry
} catch (ValidationException $e) {
// Invalid input — $e->getMessage() describes the problem
} catch (ApiException $e) {
// API returned an error — check $e->getMessage()
} catch (NetworkException $e) {
// Network timeout or connection failure
} catch (Fast2smsException $e) {
// Catch-all for any other package exception
}