# Send SMS & Handle Webhooks Using Laravel
URL: /docs/recipes/send-sms-laravel

> Integrate SMS Pro into a Laravel 10/11 application with a custom Notification Channel, Http client wrapper, and signed webhook route.

Difficulty: Beginner | Duration: 6 min | Language: PHP

## Step 1: 1. Configure Environment & Services Config (`config/services.php`)

Add your API credentials to `.env` and register the `smspro` service entry in `config/services.php`.

```php
<?php

return [
    // ... other services

    'smspro' => [
        'base_url' => env('SMSPRO_BASE_URL', 'https://api.smspro.com'),
        'api_key' => env('SMSPRO_API_KEY'),
        'default_sender_id' => env('SMSPRO_DEFAULT_SENDER_ID', 'SMSPRO'),
        'webhook_secret' => env('SMSPRO_WEBHOOK_SECRET'),
    ],
];
```

## Step 2: 2. Build the SmsProService Client (`app/Services/SmsProService.php`)

Create a reusable service class using Laravel&apos;s `Http` facade. Every mutating `POST` request attaches a unique `Str::uuid()` idempotency header and throws on HTTP `4xx/5xx` client or billing errors.

```php
<?php

namespace App\Services;

use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
use RuntimeException;

class SmsProService
{
    protected string $baseUrl;
    protected string $apiKey;
    protected string $defaultSenderId;

    public function __construct()
    {
        $this->baseUrl = config('services.smspro.base_url');
        $this->apiKey = config('services.smspro.api_key');
        $this->defaultSenderId = config('services.smspro.default_sender_id');
    }

    /**
     * Send a single transactional SMS message with automatic idempotency.
     */
    public function send(string $to, string $message, ?string $senderId = null): array
    {
        $response = Http::withHeaders([
            'X-Api-Key' => $this->apiKey,
            'Content-Type' => 'application/json',
            'Idempotency-Key' => (string) Str::uuid(),
        ])->post("{$this->baseUrl}/api/v1/public/sms/send", [
            'to' => $to,
            'senderId' => $senderId ?: $this->defaultSenderId,
            'message' => $message,
        ]);

        if ($response->failed()) {
            throw new RuntimeException("SMS Pro Dispatch Failed [{$response->status()}]: {$response->body()}");
        }

        return $response->json('data');
    }
}
```

## Step 3: 3. Create a Custom Notification Channel (`app/Channels/SmsProChannel.php`)

Implement `SmsProChannel` so any standard Laravel Notification (`User::notify()`) can dispatch SMS seamlessly through SMS Pro.

```php
<?php

namespace App\Channels;

use App\Services\SmsProService;
use Illuminate\Notifications\Notification;

class SmsProChannel
{
    public function __construct(protected SmsProService $smsPro) {}

    /**
     * Send the given notification via SMS Pro.
     */
    public function send(mixed $notifiable, Notification $notification): void
    {
        if (!method_exists($notification, 'toSmsPro')) {
            return;
        }

        $message = $notification->toSmsPro($notifiable);
        $to = $notifiable->routeNotificationFor('sms', $notification) 
            ?: $notifiable->phone_number;

        if (!$to) {
            return;
        }

        $this->smsPro->send($to, $message);
    }
}
```

## Step 4: 4. Build the Signed Webhook Controller (`app/Http/Controllers/SmsProWebhookController.php`)

Create an invokable controller in `app/Http/Controllers/SmsProWebhookController.php` that verifies `X-SmsPro-Signature` using `hash_equals()` against the raw request content (`$request->getContent()`).

```php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Log;

class SmsProWebhookController extends Controller
{
    public function __invoke(Request $request): Response
    {
        $signature = $request->header('X-SmsPro-Signature');
        $secret = config('services.smspro.webhook_secret');

        if (!$signature || !$secret) {
            return response('Missing signature or secret', 401);
        }

        // Compute HMAC-SHA256 over raw payload
        $computed = hash_hmac('sha256', $request->getContent(), $secret);

        // Constant-time string comparison prevents timing attacks
        if (!hash_equals($computed, $signature)) {
            Log::warning('SMS Pro Webhook Signature Verification Failed');
            return response('Invalid signature', 401);
        }

        $event = $request->json('event');
        $data = $request->json('data');

        Log::info("Verified SMS Pro Webhook [{$event}]:", $data);

        // TODO: Update your internal database model status

        return response('ACK', 200);
    }
}
```