SendKuy API

Laravel

Notifications, a facade and signed webhooks for Laravel 12 and 13.

sendkuy/laravel builds on the PHP SDK. It configures the client from .env, adds a sendkuy notification channel, and turns signed webhooks into Laravel events.

Install

composer require sendkuy/laravel
.env
SENDKUY_API_KEY=sk_live_YOUR_API_KEY

The service provider and the SendKuy facade register themselves.

Send from anywhere

use SendKuy\Laravel\Facades\SendKuy;

$messages = SendKuy::messages()->send([
    'channel' => 'whatsapp',
    'to' => ['+6281234567890'],
    'body' => 'Your order is on its way.',
]);

Type-hint SendKuy\SendKuy to have the same client injected. Every endpoint has a method; see the list of methods.

Notifications

use Illuminate\Notifications\Notification;
use SendKuy\Laravel\Notifications\SendKuyMessage;

class OrderShipped extends Notification
{
    public function via(object $notifiable): array
    {
        return ['sendkuy'];
    }

    public function toSendKuy(object $notifiable): SendKuyMessage
    {
        return SendKuyMessage::whatsapp('Order #1042 is on its way.')
            ->media('https://example.com/receipts/1042.pdf', 'document');
    }
}

The channel asks the notifiable where to send:

public function routeNotificationForSendKuy(Notification $notification): string
{
    return $this->phone;
}

Return an array to reach several recipients in one message call. Or send without a model: Notification::route('sendkuy', '+6281234567890')->notify(new OrderShipped);. A notifiable without a recipient throws MissingRecipientException before any call is made.

BuilderSets
SendKuyMessage::whatsapp($body), ::sms($body), ::email($subject, $body)the channel and the text
->template($id, $parameters)a WhatsApp template
->media($url, $type, $filename)one WhatsApp attachment
->attach($url, $filename)an email attachment, up to ten
->gateway($id)the exact sender
->scheduleAt($dateTime)send later

A retried queued notification is never sent twice

When a notification implements ShouldQueue and its job may run more than once (tries above 1, or --tries on the worker), Laravel runs it again after a worker dies, a timeout or an exception. The channel derives the idempotency key from Laravel's notification id, which stays the same across every retry of that job, so SendKuy answers a retry with the first result instead of sending again.

Keep toSendKuy() deterministic for this to hold: build the message from the notification's own data, not from now() or other values that change between runs. A retry that produces a different payload under the same key is refused with 409 idempotency_key_reused, so the job fails instead of sending twice. To schedule, store the time on the notification when you create it and pass that to scheduleAt().

A refusal is not remembered. When SendKuy refuses the send, for example with insufficient_credits or no_sender_available, nothing went out, so a retry of the job under the same key runs the send again once you top up or connect a sender. Retrying also helps with 429 and server errors: a retry under the same key either runs again or returns the message that already went out (see what is remembered).

The channel returns the created messages; a NotificationSent listener reads them from $event->response.

Use SendKuy as your Laravel mailer

Send every Laravel email, from mailables to password resets, through SendKuy. Add a mailer to config/mail.php:

config/mail.php
'mailers' => [
    'sendkuy' => [
        'transport' => 'sendkuy',
        'gateway_id' => env('SENDKUY_MAIL_GATEWAY_ID'),
    ],
],

and make it the default:

.env
MAIL_MAILER=sendkuy
MAIL_FROM_ADDRESS=hello@your-domain.com
MAIL_FROM_NAME="Your App"
  • The sender address belongs to the gateway. MAIL_FROM_ADDRESS must match the address and domain of your email gateway; MAIL_FROM_NAME becomes the name recipients see. With gateway_id, every email goes through that gateway and is never moved to another sender.
  • What is sent. Every To recipient, the subject, and the HTML part (or the escaped text part). The first Reply-To becomes reply_to; without one, replies go to the from address. List-Unsubscribe and List-Unsubscribe-Post are passed on as headers; other headers are left out. Every email is sent with transactional: true, exactly as rendered.
  • What is refused. Cc, Bcc, envelope-only recipients and attached or embedded files throw a TransportException before anything is sent. Failed messages and API errors throw one too, with the error code and message id.
  • Sending twice. A notification gets an idempotency key from its id and recipients, so a retried queued notification is never sent twice. Any other email uses its X-SendKuy-Idempotency-Key header, or else its Message-ID, which only covers the SDK retrying a request: give a queued mailable its own key. A retry whose key already sent the email counts as sent. A failover or roundrobin mailer that wraps sendkuy gets no automatic notification key.
  • Rate limit. One email is one request, under your plan's per-minute limit. Give queued mail jobs $tries and $backoff.

Webhooks

The package registers POST /sendkuy/webhook outside the web middleware group, so CSRF does not apply. Point a webhook at it and store the secret:

.env
SENDKUY_WEBHOOK_SECRET=whsec_...

A delivery with a valid signature becomes a WebhookReceived event and answers 200; anything else answers 403. Without SENDKUY_WEBHOOK_SECRET the route answers 500 and logs an error, so the problem shows up in your delivery history.

use Illuminate\Contracts\Queue\ShouldQueue;
use SendKuy\Laravel\Events\WebhookReceived;

class HandleSendKuyWebhook implements ShouldQueue
{
    public function handle(WebhookReceived $event): void
    {
        if ($event->type === 'message.received') {
            // $event->data is the conversation message; $event->id is X-SendKuy-Event-Id
        }
    }
}

Queue the listener so the route answers within the 10-second timeout. $event->id is the event id from X-SendKuy-Event-Id, which stays the same across every retry of one event: store it and skip ids you have seen. $event->payload holds the whole verified body. New event types reach the listener without a package update.

Configuration

php artisan vendor:publish --tag=sendkuy-config
KeyEnvDefault
api_keySENDKUY_API_KEYnone
base_urlSENDKUY_BASE_URLhttps://app.sendkuy.com/api/v1; change it only to reach a proxy or a mock server in tests
timeoutSENDKUY_TIMEOUT30 seconds
max_retriesSENDKUY_MAX_RETRIES2
webhook.secretSENDKUY_WEBHOOK_SECRETnone
webhook.pathSENDKUY_WEBHOOK_PATHsendkuy/webhook; empty turns the route off
webhook.toleranceSENDKUY_WEBHOOK_TOLERANCE300 seconds

Testing

Notification::fake() works as usual. To fake the client itself, bind your own instance with a PSR-18 client:

$this->app->instance(\SendKuy\SendKuy::class, new \SendKuy\SendKuy('sk_test_123', [
    'http_client' => $fakeClient,
]));