Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

CuriousBread's avatar

Vonage SMS Verification

Hello, is there a package or a series of how to do SMS verification by sending an OTP to the client's phone number? I would like to include it as a part of the registration process, and if possible I would like to use it together with the email verification using jetstream.

0 likes
3 replies
LaryAI's avatar
Level 58

Yes, you can use the Vonage SMS API to send OTP (One-Time Password) to the client's phone number for SMS verification. Here's a step-by-step solution:

  1. Sign up for a Vonage account and obtain your API key and secret.

  2. Install the Vonage PHP SDK by running the following command in your Laravel project directory:

    composer require vonage/client
    
  3. Create a new controller to handle the SMS verification logic. For example, you can create a VerificationController using the following command:

    php artisan make:controller VerificationController
    
  4. In the VerificationController, create a method to send the OTP to the client's phone number. Here's an example implementation:

    <?php
    
    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    use Vonage\Client\Credentials\Basic;
    use Vonage\Client;
    use Vonage\SMS\Message\SMS;
    
    class VerificationController extends Controller
    {
        public function sendOTP(Request $request)
        {
            $basic  = new Basic('YOUR_VONAGE_API_KEY', 'YOUR_VONAGE_API_SECRET');
            $client = new Client($basic);
    
            $otp = mt_rand(100000, 999999); // Generate a random OTP
    
            $message = new SMS($request->phone_number, 'Your OTP is: ' . $otp);
            $response = $client->sms()->send($message);
    
            // Store the OTP in the database or session for verification
    
            return response()->json(['message' => 'OTP sent successfully']);
        }
    }
    

    Replace 'YOUR_VONAGE_API_KEY' and 'YOUR_VONAGE_API_SECRET' with your actual Vonage API key and secret.

  5. Add a route to the sendOTP method in your routes/web.php file:

    use App\Http\Controllers\VerificationController;
    
    Route::post('/send-otp', [VerificationController::class, 'sendOTP']);
    
  6. Finally, you can call the sendOTP method from your registration process or any other relevant place in your application. For example, you can use an AJAX request to send the OTP:

    $.ajax({
        url: '/send-otp',
        method: 'POST',
        data: {
            phone_number: 'CLIENT_PHONE_NUMBER'
        },
        success: function(response) {
            console.log(response.message);
        }
    });
    

    Replace 'CLIENT_PHONE_NUMBER' with the actual client's phone number.

Remember to handle the OTP verification process as well, which typically involves comparing the user-provided OTP with the stored OTP.

Note: This solution assumes you have already set up Jetstream for email verification.

robots's avatar

Hey, but this does not work with Laravel 8; does anyone have a solution?

Please or to participate in this conversation.