Shopping cart

    Subtotal $0.00

    View cartCheckout

    How to Send SMS Using Fast2SMS and Twilio in Laravel 12

    • Home
    • API
    • How to Send SMS Using Fast2SMS and Twilio in Laravel 12
    Featured Image

    Description

    Learn how to send SMS notifications in Laravel 12 using Fast2SMS and Twilio. This guide covers API setup, configuration, and implementation for seamless SMS delivery.

    Introduction

    Sending SMS notifications is crucial for applications that require user authentication, OTP verification, or transactional messaging. Laravel 12 provides robust support for integrating SMS gateways like Fast2SMS and Twilio. In this guide, we will walk you through the steps to integrate and send SMS using both providers.

    Prerequisites

    Before getting started, ensure you have the following:

    • Laravel 12 installed on your system
    • Composer installed
    • Fast2SMS API key
    • Twilio account with SID, Auth Token, and a Twilio phone number
    • Basic knowledge of Laravel framework

    Step 1: Install Laravel 12

    If you haven’t installed Laravel 12 yet, use the following command:

    composer create-project --prefer-dist laravel/laravel smsapp
    

    Navigate to the project directory:

    cd smsapp
    

    Step 2: Set Up Fast2SMS in Laravel 12

    Get Fast2SMS API Key

    1. Sign up at Fast2SMS.
    2. Generate an API key from the dashboard.

    Configure Fast2SMS in Laravel

    Edit your .env file to store the Fast2SMS credentials:

    FAST2SMS_API_KEY=your_api_key_here
    

    Create SMS Service for Fast2SMS

    Inside the Laravel project, create a service class:

    namespace App\Services;
    
    use Illuminate\Support\Facades\Http;
    
    class Fast2SmsService {
        public function sendSms($message, $phoneNumber) {
            $response = Http::withHeaders([
                'authorization' => env('FAST2SMS_API_KEY'),
                'Content-Type' => 'application/json'
            ])->post('https://www.fast2sms.com/dev/bulkV2', [
                'route' => 'v3',
                'message' => $message,
                'language' => 'english',
                'numbers' => $phoneNumber
            ]);
            return $response->json();
        }
    }
    

    Use Fast2SMS Service in Controller

    Modify the SmsController.php to send an SMS:

    namespace App\Http\Controllers;
    
    use App\Services\Fast2SmsService;
    use Illuminate\Http\Request;
    
    class SmsController extends Controller {
        protected $smsService;
    
        public function __construct(Fast2SmsService $smsService) {
            $this->smsService = $smsService;
        }
    
        public function send(Request $request) {
            $message = "Hello, this is a test message from Techliphant";
            $phoneNumber = $request->phone;
            $response = $this->smsService->sendSms($message, $phoneNumber);
            return response()->json($response);
        }
    }
    

    Define Route for Sending SMS

    Add the following route in routes/web.php:

    use App\Http\Controllers\SmsController;
    
    Route::post('/send-sms', [SmsController::class, 'send']);
    

    Step 3: Set Up Twilio in Laravel 12

    Get Twilio Credentials

    1. Sign up at Twilio.
    2. Get your Account SID, Auth Token, and Twilio Phone Number.

    Install Twilio SDK

    Run the following command:

    composer require twilio/sdk
    

    Configure Twilio in Laravel

    Update your .env file with the Twilio credentials:

    TWILIO_SID=your_twilio_sid
    TWILIO_AUTH_TOKEN=your_twilio_auth_token
    TWILIO_PHONE_NUMBER=your_twilio_phone_number
    

    Create Twilio SMS Service

    Create a new service class:

    namespace App\Services;
    
    use Twilio\Rest\Client;
    
    class TwilioSmsService {
        public function sendSms($message, $recipientNumber) {
            $sid = env('TWILIO_SID');
            $token = env('TWILIO_AUTH_TOKEN');
            $from = env('TWILIO_PHONE_NUMBER');
    
            $twilio = new Client($sid, $token);
            return $twilio->messages->create($recipientNumber, [
                'from' => $from,
                'body' => $message
            ]);
        }
    }
    

    Use Twilio Service in Controller

    Modify SmsController.php to integrate Twilio:

    use App\Services\TwilioSmsService;
    
    class SmsController extends Controller {
        protected $twilioService;
    
        public function __construct(TwilioSmsService $twilioService) {
            $this->twilioService = $twilioService;
        }
    
        public function sendTwilioSms(Request $request) {
            $message = "Hello from Twilio!";
            $phoneNumber = $request->phone;
            $response = $this->twilioService->sendSms($message, $phoneNumber);
            return response()->json($response);
        }
    }
    

    Define Route for Sending SMS with Twilio

    Add the following route:

    Route::post('/send-twilio-sms', [SmsController::class, 'sendTwilioSms']);
    

    Step 4: Testing SMS Sending

    Use Postman or cURL to test sending an SMS.

    For Fast2SMS:

    curl -X POST http://127.0.0.1:8000/send-sms -d "phone=9876543210"
    

    For Twilio:

    curl -X POST http://127.0.0.1:8000/send-twilio-sms -d "phone=+11234567890"
    
    Mermaid Diagram showing how these works

    Conclusion

    Integrating SMS functionality in Laravel 12 is straightforward using Fast2SMS and Twilio. This tutorial covered the setup, configuration, and implementation for both services. Depending on your requirements, you can choose the provider that best suits your needs.

    Need Expert Help?

    For professional assistance with Laravel development and SMS integration, contact Techliphant.

    Leave A Comment

    Your email address will not be published. Required fields are marked *