PHP code example of whatsdev / whatsdev-php

1. Go to this page and download the library: Download whatsdev/whatsdev-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

whatsdev / whatsdev-php example snippets


use WhatsDev\Client;

$client = new Client('YOUR_TEST_API_KEY');
$session = $client->sessions()->create();
while ($client->sessions()->get($session['data']['id'])['data']['status'] !== 'connected') {
    sleep(1);
}
$result = $client->messages()->sendText($session['data']['id'], '967700000000', 'Hello from WhatsDev!');

echo $result->data['data']['id'];

use WhatsDev\Exceptions\ApiException;
use WhatsDev\Exceptions\QuotaExceededException;

try {
    $client->messages()->sendText($sessionId, '967700000000', 'Hello!');
} catch (QuotaExceededException $e) {
    // this plan's daily or monthly send quota is used up
    echo "Out of quota: {$e->errorCode}\n";
} catch (ApiException $e) {
    // any other typed API error — $e->errorCode, $e->status, $e->details and $e->requestId are all available
    echo "Request {$e->requestId} failed ({$e->status}): {$e->getMessage()}\n";
}

foreach ($client->contacts()->list(['status' => 'active']) as $contact) {
    echo $contact['id'], "\n";
}

$file = $client->messages()->media($messageId);

file_put_contents('/tmp/inbound-media', $file->contents);
echo $file->contentType; // e.g. image/jpeg, or null if the server named none

use WhatsDev\Webhooks\SignatureVerifier;

$rawBody = file_get_contents('php://input'); // must be the exact raw bytes, not a re-encoded payload
$signature = $_SERVER['HTTP_X_SIGNATURE'] ?? null;

SignatureVerifier::assert($rawBody, $signature, $webhookSecret); // throws InvalidSignatureException on failure

$payload = json_decode($rawBody, true);

$groups = $client->request('GET', 'v1/sessions/7/groups', ['query' => ['limit' => 10]]);

$group = $client->request('POST', 'v1/sessions/7/groups', [
    'body' => ['name' => 'Ops team', 'participants' => ['967700000000']],
]);

$group = $client->request('POST', 'v1/sessions/7/groups', [
    'body' => ['name' => 'Ops team', 'participants' => ['967700000000']],
    'idempotencyKey' => 'ops-team-2026-08-14',
]);

use WhatsDev\Laravel\WhatsDev;

WhatsDev::messages()->sendText($sessionId, '967700000000', 'Hello!');
bash
composer 
bash
php artisan vendor:publish --tag=whatsdev-config