Developer guides
Laravel OTP Verification over WhatsApp and SMS
Laravel OTP verification with the Hypersender SDK: send a code over WhatsApp or SMS, check what the user types, and rate-limit both routes.


Laravel OTP verification with Hypersender takes two calls: requestCode sends a one-time code from your own WhatsApp number or SMS phone, and validateCode checks what the user typed. Hypersender creates the code, sends it, and tracks expiry and single use, so you don't need an otps table.
This guide builds a phone verification flow for logged-in users: a form that sends the code, a form that checks it, and rate limits on both.
What you need
- Laravel 10, 11 or 12, on PHP 8.2 or newer
- A Hypersender account with a WhatsApp instance connected by QR code, or an SMS instance on an Android phone
- The instance's API key and instance ID, from the dashboard
Install the SDK and add the OTP keys
composer require hypersender/hypersender-laravel
php artisan vendor:publish --tag=hypersender-configThe OTP client has its own keys in .env:
HYPERSENDER_OTP_API_KEY=your_api_key
HYPERSENDER_OTP_INSTANCE_ID=your_instance_idThe instance decides the channel. A WhatsApp instance sends the code as a WhatsApp message, and an SMS instance sends a plain-text SMS from the phone.
Add a place to store the verified number:
php artisan make:migration add_phone_to_users_tableSchema::table('users', function (Blueprint $table) {
$table->string('phone')->nullable();
$table->timestamp('phone_verified_at')->nullable();
});Send the code
namespace App\Http\Controllers;
use Hypersender\Hypersender;
use Illuminate\Http\Request;
class PhoneVerificationController extends Controller
{
public function send(Request $request)
{
$data = $request->validate([
'phone' => ['required', 'regex:/^[1-9]\d{7,14}$/'], // country code + number, digits only
]);
$response = Hypersender::otp()->requestCode(
chatId: $data['phone'].'@c.us',
length: 6,
useNumber: true,
useLetter: false,
allCapital: false,
name: config('app.name'),
expires: 300,
);
if ($response->failed()) {
return back()->withErrors(['phone' => 'We could not send a code to that number.']);
}
$request->session()->put('otp_chat_id', $response->json('data.chat_id'));
return redirect()->route('phone.verify');
}
}The user gets a message with your app name and the code. expires is in seconds, from 60 to 1800, and length can be 4 to 10. Set useLetter to true for codes like K7P2QX.
The response includes the generated code in data.code. Keep it on the server. Don't return it to the browser and don't log it.
Check the code
Add the second method to the same controller:
public function verify(Request $request)
{
$data = $request->validate(['code' => ['required', 'string', 'max:10']]);
$chatId = $request->session()->get('otp_chat_id');
if (! $chatId) {
return redirect()->route('phone.edit');
}
$response = Hypersender::otp()->validateCode(
chatId: $chatId,
code: $data['code'],
);
if (! $response->successful() || $response->json('data.status') !== 'validated') {
return back()->withErrors(['code' => 'That code is wrong or has expired.']);
}
$request->session()->forget('otp_chat_id');
$request->user()->forceFill([
'phone' => strstr($chatId, '@', true),
'phone_verified_at' => now(),
])->save();
return redirect()->intended('/dashboard');
}A wrong, expired or already used code comes back as HTTP 422 with success: false, so one check covers all three. A code works once: after a successful check, the same code fails.
Routes and rate limits
use App\Http\Controllers\PhoneVerificationController;
Route::middleware('auth')->group(function () {
Route::view('/phone', 'phone.edit')->name('phone.edit');
Route::view('/phone/verify', 'phone.verify')->name('phone.verify');
Route::post('/phone', [PhoneVerificationController::class, 'send'])
->middleware('throttle:3,10');
Route::post('/phone/verify', [PhoneVerificationController::class, 'verify'])
->middleware('throttle:5,1');
});throttle:3,10 allows three codes per user every 10 minutes, so nobody can use your form to flood a number. throttle:5,1 allows five guesses a minute, which makes guessing a 6-digit code before it expires impractical. Hypersender has its own limits on top: up to 10 code requests and 20 checks a minute.
The two Blade views are plain forms: one posts phone, the other posts code.
Send the code by SMS instead
Point HYPERSENDER_OTP_INSTANCE_ID at an SMS instance and pass the number in international format, with no @c.us:
$response = Hypersender::otp()->requestCode(
chatId: '+'.$data['phone'],
length: 6,
useNumber: true,
useLetter: false,
allCapital: false,
name: config('app.name'),
expires: 300,
);The SMS reads like YourApp code: 482913, and the wording rotates between a few short versions so carriers are less likely to filter it. Validate with the same + number.
In verify, store ltrim($chatId, '+') instead of the strstr call. The phone sends through its SIM, so the cost is your mobile plan. The SMS OTP guide covers sending your own message text.
Skip the code with a WhatsApp link
On a WhatsApp instance, generateLink returns a WhatsApp link with a prefilled message. The user opens it and sends that message from their own WhatsApp, which proves they own the number, so there's no code to type:
$response = Hypersender::otp()->generateLink(
chatId: $user->phone.'@c.us',
expires: 600,
name: config('app.name'),
message: [
'prompt' => 'Send me my sign-in link',
'success' => 'Here is your link: {link}',
'failed' => 'Sign-in failed, please try again.',
'expired' => 'This link has expired. Ask for a new one.',
],
callback: [
'success' => route('otp.link.success'),
'failed' => route('otp.link.failed'),
],
);
$link = $response->throw()->json('data.whatsapp_link');SMS instances can't create links. The OTP link guide has the full flow and what to check before you sign a user in from a callback.
Common problems
- The code never arrives: the instance isn't connected. Open the dashboard and scan the QR code again, or check that the SMS phone is online.
- The request is rejected: the chat ID format doesn't match the instance. WhatsApp needs
201001234567@c.us, SMS needs+201001234567. - Every check fails: validate with the exact
chat_idthe request returned. That's why the controller keeps it in the session. - Too many requests: you hit the per-minute limit. Wait a minute and let the user retry.
Pricing
WhatsApp OTP is part of the WhatsApp plan, from $9.99 per number a month, with no fee per code. Starter includes 3,000 OTP requests per number a month. See the WhatsApp OTP use case and the plans.
The package reference is in the Laravel OTP docs, and Send WhatsApp messages in Laravel covers the rest of the SDK. Create a Hypersender account to get your API key and instance ID.





