PHP code example of multiwa / multiwa-php

1. Go to this page and download the library: Download multiwa/multiwa-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/ */

    

multiwa / multiwa-php example snippets




use MultiWA\MultiWA;

// baseUrl must 
//   Docker:    http://localhost:3333/api/v1
$client = new MultiWA(
    baseUrl: 'https://your-multiwa-instance.com/api/v1',
    apiKey: 'your-api-key',
);

$result = $client->messages()->sendText(
    profileId: 'your-profile-id',
    to: '6281234567890',
    text: 'Hello from the MultiWA PHP SDK!',
);

echo $result->messageId;

$messages = $client->messages();

$messages->sendText($profileId, $to, 'Hello!');
$messages->sendText($profileId, $to, 'Yes', quotedMessageId: $whatsappMessageId); // reply with a quote
$messages->sendImage($profileId, $to, url: 'https://example.com/a.jpg', caption: 'Look');
$messages->sendVideo($profileId, $to, url: 'https://example.com/a.mp4', caption: 'Watch');
$messages->sendAudio($profileId, $to, url: 'https://example.com/a.ogg', ptt: true); // ptt: voice note
$messages->sendDocument($profileId, $to, 'report.pdf', url: 'https://example.com/report.pdf', caption: 'Q3');
$messages->sendLocation($profileId, $to, -6.2088, 106.8456, name: 'Monas', address: 'Jakarta');
$messages->sendContact($profileId, $to, 'Budi', '6281111111111', email: '[email protected]');
$messages->sendPoll($profileId, $to, 'Lunch?', ['Rice', 'Noodles'], allowMultipleAnswers: false);
$messages->sendReaction($profileId, $result->messageId, '👍'); // MultiWA's message id; '' removes it

// Stored messages, newest first (rows as the API returns them)
$rows = $messages->list($profileId, limit: 50, offset: 0, type: 'text', direction: 'incoming');

$contacts = $client->contacts();

// Creates the contact, or merges tags and metadata into the one with this phone
$contact = $contacts->create($profileId, '6281234567890', name: 'Budi', tags: ['vip'], metadata: ['source' => 'web']);
$contact = $contacts->get($contact->id);

$page = $contacts->list($profileId, search: 'bud', tags: ['vip', 'new'], limit: 50, offset: 0);
// ['contacts' => Contact[], 'total' => 1, 'limit' => 50, 'offset' => 0]

$contacts->update($contact->id, name: 'Budi S', tags: ['vip']); // tags and metadata replace the old ones
$contacts->delete($contact->id); // ['success' => true]

$broadcasts = $client->broadcasts();

$broadcast = $broadcasts->create(
    profileId: $profileId,
    name: 'October promo',
    message: 'Hello! Our promo starts today.',            // a text, or a message array (below)
    recipients: ['type' => 'tags', 'value' => ['vip']],
    settings: ['delayMin' => 5000, 'delayMax' => 15000],   // optional
);

$broadcasts->update($broadcast->id, name: 'October promo v2');   // drafts only
$broadcasts->schedule($broadcast->id, new DateTimeImmutable('2026-11-01 09:00', new DateTimeZone('Asia/Jakarta')));
$broadcasts->start($broadcast->id);   // ['success' => true, 'message' => 'Broadcast started', 'recipientCount' => 42]
$broadcasts->pause($broadcast->id);   // the Broadcast, status paused
$broadcasts->resume($broadcast->id);  // ['success' => true, 'message' => 'Broadcast resumed']
$broadcasts->cancel($broadcast->id);  // the Broadcast, status failed
$broadcasts->delete($broadcast->id);  // ['success' => true]; not while it is running

$broadcasts->get($broadcast->id);
$broadcasts->list($profileId, status: 'running'); // Broadcast[]
$broadcasts->stats($broadcast->id);
// ['id' => …, 'status' => 'completed', 'stats' => ['total' => 10, 'sent' => 9, 'delivered' => 8,
//  'read' => 5, 'failed' => 1, 'pending' => 0, 'successRate' => 80, 'progress' => 100], 'duration' => 600, …]

$profiles = $client->profiles();

$profile = $profiles->create($workspaceId, 'Sales', engine: 'whatsapp-web-js'); // engine is optional
$profile = $profiles->get($profile->id);           // $profile->displayName, ->phoneNumber, ->status, …
$all = $profiles->list($workspaceId);              // Profile[] with messageCount; workspaceId is optional
$profiles->update($profile->id, name: 'Sales ID'); // also engine, webhookUrl, webhookSecret
$profiles->status($profile->id);
// ['id' => …, 'name' => 'Sales', 'status' => 'connected', 'phone' => '628…', 'lastConnectedAt' => …, 'engineConnected' => true]
$profiles->disconnect($profile->id);               // ['status' => 'disconnected']
$profiles->delete($profile->id);                   // ['success' => true]; also deletes its messages and contacts

$profiles->connect($profileId); // ['status' => 'connecting', 'message' => 'Scan QR code to connect']

use MultiWA\Clients\WebhooksClient;

$webhooks = $client->webhooks();

$webhook = $webhooks->create($profileId, 'https://example.com/hook', ['message.received', 'message.sent']);
$secret = $webhook->secret; // generated by the server; keep it to verify deliveries

$webhooks->get($webhook->id);
$webhooks->list($profileId);                                      // Webhook[]
$webhooks->update($webhook->id, events: ['message.received'], enabled: false);
$webhooks->test($webhook->id);   // sends a signed `test` event: ['success' => true, 'message' => …]
$webhooks->delete($webhook->id); // ['success' => true]

// In your webhook endpoint: verify the signature over the RAW body
$ok = WebhooksClient::verifySignature(
    file_get_contents('php://input'),
    $_SERVER['HTTP_X_MULTIWA_SIGNATURE'] ?? null,
    $secret,
);

$calls = $client->calls();

$page = $calls->list($profileId, outcome: 'answered');    // CallPage: ->calls, ->nextCursor
$calls->update($callId, followUp: 'done');                // unassign() removes the assignee
$calls->reply($callId, 'We will call you back');

// Owners and admins
$file = fopen('notice.mp3', 'rb');                        // recording needs the announcement first
$calls->uploadAnnouncement($profileId, $file);            // the SDK leaves your stream open
fclose($file);
$calls->updateSettings($profileId, answerInDashboard: true, recording: true);
$audio = $calls->getRecording($callId);                   // AudioFile: ->content, ->contentType
$calls->deleteRecording($callId);

$result = $client->request('POST', 'messages/otp', [
    'json' => ['profileId' => $profileId, 'to' => $to, 'text' => 'Your code is 563842', 'code' => '563842'],
]);

use MultiWA\Exceptions\ApiException;
use MultiWA\Exceptions\MultiWAException;
use MultiWA\Exceptions\RateLimitException;
use MultiWA\Exceptions\ValidationException;

try {
    $result = $client->messages()->sendText($profileId, $to, 'Hello!');
    if (!$result->success) {
        echo "WhatsApp refused it: {$result->error}";
    }
} catch (RateLimitException $e) {
    echo "{$e->getErrorCode()}, retry after {$e->getRetryAt()?->format(DATE_ATOM)}";
} catch (ValidationException $e) {
    echo implode("\n", $e->getErrors());
} catch (ApiException $e) {
    echo "HTTP {$e->getStatusCode()} {$e->getErrorCode()}: {$e->getMessage()}";
} catch (MultiWAException $e) {
    echo "MultiWA unreachable: {$e->getMessage()}";
}

use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Psr7\Response;

$client = new MultiWA('https://wa.example.com/api/v1', 'test-key', handler: new MockHandler([
    new Response(201, [], '{"success":true,"messageId":"m1","conversationId":"c1","status":"sent"}'),
]));
bash
composer 
json
{
  "repositories": [{ "type": "path", "url": "../MultiWA/packages/sdk-php" }],
  "