Whatify LogoWhatify
SDKs

PHP Laravel SDK

Official Whatify SDK for PHP and Laravel applications

The official Whatify SDK for PHP and Laravel applications.

Before using this SDK, sign up at https://whatify.dev to get your API token.

Installation

Install via Composer:

composer require whatify/whatify-php

Laravel Auto-Discovery

The package will automatically register itself in Laravel 5.5+.

For older versions, add the service provider to config/app.php:

'providers' => [
    // ...
    Whatify\WhatifyServiceProvider::class,
],

'aliases' => [
    // ...
    'Whatify' => Whatify\Facades\Whatify::class,
],

Configuration

Publish Configuration (Laravel)

php artisan vendor:publish --provider="Whatify\WhatifyServiceProvider"

This creates config/whatify.php:

<?php

return [
    'api_token' => env('WHATIFY_API_TOKEN'),
    'api_key' => env('WHATIFY_API_KEY'),
];

Environment Variables

Add to your .env file:

WHATIFY_API_TOKEN=your-api-token-here

Usage

use Whatify\Facades\Whatify;

// Connect device
$result = Whatify::connect();

// Send message
$result = Whatify::sendText('1234567890', 'Hello from Laravel!');

// Check status
$status = Whatify::status();

// Disconnect
$result = Whatify::disconnect();

Dependency Injection (Laravel)

use Whatify\WhatifyClient;

class NotificationController extends Controller
{
    public function send(WhatifyClient $whatify)
    {
        $result = $whatify->sendText('1234567890', 'Hello!');
        return response()->json($result);
    }
}

Plain PHP

<?php

require 'vendor/autoload.php';

use Whatify\WhatifyClient;

$client = new WhatifyClient([
    'api_token' => 'your-api-token'
]);

$result = $client->sendText('1234567890', 'Hello from PHP!');
print_r($result);

API Methods

Connect Device

Connect your WhatsApp device.

$result = Whatify::connect();

// Returns:
// [
//     'success' => true,
//     'qrCode' => 'data:image/png;base64,...',
//     'status' => 'qr',
//     'message' => 'Scan QR code to connect'
// ]

Disconnect Device

$result = Whatify::disconnect();

// Returns:
// [
//     'success' => true,
//     'message' => 'Device disconnected successfully'
// ]

Check Status

$result = Whatify::status();

// Returns:
// [
//     'success' => true,
//     'status' => 'connected',
//     'isConnected' => true,
//     'message' => 'Device is connected'
// ]

Send Messages

Send Text Message

// Single recipient
$result = Whatify::sendText('1234567890', 'Hello World!');

// Multiple recipients
$result = Whatify::sendText(
    ['1234567890', '0987654321'],
    'Bulk message'
);

Send Attachment

$result = Whatify::sendAttachment('1234567890', [
    'type' => 'image',
    'url' => 'https://example.com/image.jpg',
    'caption' => 'Check this out!'
]);

Send Text with Attachment

$result = Whatify::send([
    'to' => '1234567890',
    'text' => 'Here is your document',
    'attachment' => [
        'type' => 'document',
        'url' => 'https://example.com/report.pdf',
        'filename' => 'monthly-report.pdf'
    ]
]);

Attachment Types:

  • image - Image files
  • video - Video files
  • audio - Audio files
  • document - PDF, DOCX, XLSX, etc.

Response:

[
    'success' => true,
    'message' => 'Message sent successfully',
    'messageIds' => ['msg_123', 'msg_124']
]

Send OTP

Send One-Time Password (OTP) codes via WhatsApp with multi-language support.

Send OTP (English)

try {
    $result = Whatify::sendOTP('1234567890', '123456', 'English');
    print_r($result['data']);
} catch (WhatifyException $e) {
    Log::error('Failed to send OTP: ' . $e->getMessage());
}

Send OTP (Arabic)

$result = Whatify::sendOTP('1234567890', '654321', 'Arabic');

Generate and Send OTP

// Generate a random 6-digit OTP
$otpCode = str_pad(rand(0, 999999), 6, '0', STR_PAD_LEFT);

$result = Whatify::sendOTP('1234567890', $otpCode, 'English');

// Store OTP in database for verification
DB::table('otps')->insert([
    'phone' => '1234567890',
    'code' => Hash::make($otpCode),
    'expires_at' => now()->addMinutes(10)
]);

Using Array Syntax

$result = Whatify::sendOTP([
    'phone' => '1234567890',
    'code' => '123456',
    'language' => 'English'
]);

Plain PHP

<?php

$client = new WhatifyClient(['api_token' => 'your-token']);

$result = $client->sendOTP('1234567890', '123456', 'English');
print_r($result);

OTP Parameters:

  • phone (string, required) - Recipient phone number with country code
  • code (string|number, required) - OTP code (4-8 digits)
  • language (string, optional) - Message language: "English" or "Arabic" (default: "English")

Response:

[
    'success' => true,
    'message' => 'OTP sent successfully',
    'data' => [
        'messageId' => 'msg_xxx',
        'whatsappMessageId' => 'wamid_xxx',
        'phone' => '1234567890',
        'code' => '123456',
        'language' => 'English',
        'sentAt' => '2024-01-01T12:00:00.000Z'
    ]
]

Expamle OTP Message:

  • English: 123456 is your verification code. For your security, do not share this code
  • Arabic: 123456 هذا هو رمز التحقق الخاص بك. لحمايتك، يرجى عدم مشاركة هذا الرمز.

Error Handling

use Whatify\Facades\Whatify;
use Whatify\Exceptions\WhatifyException;

try {
    $result = Whatify::sendText('1234567890', 'Hello!');
} catch (WhatifyException $e) {
    Log::error('Whatify error: ' . $e->getMessage());
    return response()->json([
        'error' => $e->getMessage()
    ], $e->getStatusCode());
}

Exception Types

use Whatify\Exceptions\WhatifyException;
use Whatify\Exceptions\WhatifyConnectionException;
use Whatify\Exceptions\WhatifyValidationException;

try {
    $result = Whatify::send($data);
} catch (WhatifyValidationException $e) {
    // Invalid input data
    return back()->withErrors($e->getMessage());
} catch (WhatifyConnectionException $e) {
    // Network/connection error
    Log::error('Connection failed: ' . $e->getMessage());
} catch (WhatifyException $e) {
    // General API error
    Log::error('API error: ' . $e->getMessage());
}

Laravel Examples

Send Notification from Controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Whatify\Facades\Whatify;

class WhatsAppController extends Controller
{
    public function sendMessage(Request $request)
    {
        $validated = $request->validate([
            'phone' => 'required|string',
            'message' => 'required|string'
        ]);

        try {
            $result = Whatify::sendText(
                $validated['phone'],
                $validated['message']
            );

            return response()->json($result);
        } catch (\Exception $e) {
            return response()->json([
                'error' => $e->getMessage()
            ], 500);
        }
    }
}

Queue Job for Bulk Messages

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Whatify\Facades\Whatify;

class SendBulkWhatsAppMessages implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $recipients;
    protected $message;

    public function __construct(array $recipients, string $message)
    {
        $this->recipients = $recipients;
        $this->message = $message;
    }

    public function handle()
    {
        foreach ($this->recipients as $recipient) {
            Whatify::sendText($recipient, $this->message);
            sleep(1); // Rate limiting
        }
    }
}

Dispatch the job:

SendBulkWhatsAppMessages::dispatch(
    ['1234567890', '0987654321'],
    'Important announcement!'
);

Laravel Notification Channel

<?php

namespace App\Notifications;

use Illuminate\Notifications\Notification;
use Whatify\Facades\Whatify;

class OrderShipped extends Notification
{
    protected $order;

    public function __construct($order)
    {
        $this->order = $order;
    }

    public function via($notifiable)
    {
        return ['whatsapp'];
    }

    public function toWhatsApp($notifiable)
    {
        $message = "Your order #{$this->order->id} has been shipped!";

        Whatify::sendText($notifiable->phone, $message);
    }
}

Service Class Pattern

<?php

namespace App\Services;

use Whatify\WhatifyClient;

class WhatsAppService
{
    protected $client;

    public function __construct(WhatifyClient $client)
    {
        $this->client = $client;
    }

    public function sendOrderConfirmation($order)
    {
        $message = "Order #{$order->id} confirmed! Total: ${$order->total}";

        return $this->client->sendText(
            $order->customer->phone,
            $message
        );
    }

    public function sendOTP($phone, $code)
    {
        $message = "Your verification code is: {$code}";

        return $this->client->sendText($phone, $message);
    }
}

Plain PHP Examples

Basic Usage

<?php

require 'vendor/autoload.php';

use Whatify\WhatifyClient;

$client = new WhatifyClient([
    'api_token' => 'your-api-token'
]);

// Check connection
$status = $client->status();
if (!$status['isConnected']) {
    $connect = $client->connect();
    echo "Scan QR: " . $connect['qrCode'];
    exit;
}

// Send message
$result = $client->sendText('1234567890', 'Hello from PHP!');

if ($result['success']) {
    echo "Message sent successfully!";
} else {
    echo "Failed to send message";
}

Send with Image

<?php

$client = new WhatifyClient(['api_token' => 'your-token']);

$result = $client->send([
    'to' => '1234567890',
    'text' => 'Check out our new product!',
    'attachment' => [
        'type' => 'image',
        'url' => 'https://example.com/product.jpg',
        'caption' => 'New Product Launch'
    ]
]);

Rate Limits

  • 100 requests per minute per API token
  • 1000 requests per hour per API token

Handle rate limits in Laravel:

use Illuminate\Support\Facades\RateLimiter;

RateLimiter::attempt(
    'send-whatsapp:' . $user->id,
    $perMinute = 10,
    function() use ($phone, $message) {
        Whatify::sendText($phone, $message);
    }
);

Support