threegeeks / geekads-php
PHP SDK for the GeekAds WhatsApp API: template and text messages, one-time codes, contacts, webhooks and the partner API.
Requires
- php: ^8.2
- ext-json: *
- guzzlehttp/guzzle: ^7.8
- psr/http-client: ^1.0
Requires (Dev)
- phpunit/phpunit: ^11.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Send WhatsApp template messages, one-time codes and replies from PHP through GeekAds.
composer require threegeeks/geekads-php
Requires PHP 8.2+. Using Laravel? Install threegeeks/geekads-laravel instead. It includes this package.
Quick start
Create an API key in GeekAds under API keys.
use GeekAds\GeekAds; $geekads = new GeekAds(getenv('GEEKADS_API_KEY')); // An approved template; {{1}} becomes "A-1042" $message = $geekads->messages()->template('919876543210', 'order_update', 'en_US', bodyParams: ['A-1042']); echo $message['status']; // "sent"; delivery and read updates arrive by webhook
Phone numbers include the country code: 919876543210 (a leading + and spaces are fine).
Messages
// Template with an image header and a URL button $geekads->messages()->template('919876543210', 'diwali_offer', 'en', bodyParams: ['Asha', '20%'], components: [ ['type' => 'header', 'parameters' => [['type' => 'image', 'image' => ['link' => 'https://cdn.example.com/offer.jpg']]]], ['type' => 'button', 'sub_type' => 'url', 'index' => '0', 'parameters' => [['type' => 'text', 'text' => 'offers/diwali']]], ]); // Free-form text, only within 24 hours of the customer's last message $geekads->messages()->text('919876543210', 'Your table is ready!', buttons: [['id' => 'coming', 'title' => 'On my way']]); $geekads->messages()->find($message['id']);
Safe retries: every template send carries an idempotency key, so the SDK's automatic retries (rate limits, server errors) never send a message twice. Pass your own key, such as idempotencyKey: "order-{$id}-shipped", to stay safe across your own retries too.
One-time codes
You need an approved authentication template with a "Copy code" button.
$otp = $geekads->otp()->send('919876543210', 'login_otp'); // GeekAds generates and sends the code // … the user types the code … if ($geekads->otp()->verify($otp['otp_id'], $typedCode)) { // verified }
verify()returnsfalsefor a wrong or expired code.- After 5 wrong tries it throws
RateLimitException; ask the user to request a new code.
Ad conversions
Tell GeekAds what a chat turned into. If the customer came from a Click-to-WhatsApp ad, GeekAds reports it to Meta, so the ad finds more buyers like them.
$geekads->conversions()->record('919876543210', 'Purchase', 18999.00, idempotencyKey: "booking-{$booking->id}"); $geekads->conversions()->record('919876543210', 'LeadSubmitted');
Contacts, templates, webhooks
$geekads->contacts()->upsert('919876543210', ['name' => 'Asha', 'groups' => ['VIP'], 'custom_fields' => ['city' => 'Port Blair']]); $geekads->templates()->list('approved'); $endpoint = $geekads->webhooks()->create('https://example.com/geekads/webhook', ['message.received', 'message.status']); // store $endpoint['secret']; it is shown only once
Receiving webhooks
use GeekAds\Webhook; use GeekAds\Exceptions\InvalidSignatureException; try { $event = Webhook::verify(file_get_contents('php://input'), $_SERVER['HTTP_X_GEEKADS_SIGNATURE'] ?? '', $secret); } catch (InvalidSignatureException) { http_response_code(400); exit; } if ($event['event'] === 'message.received') { $from = $event['data']['from']; $text = $event['data']['body']; $fromAd = $event['data']['referral']; // set when the customer came from a Click-to-WhatsApp ad }
Errors
Every API error throws a GeekAds\Exceptions\ApiException (or a subclass) with a stable errorCode:
try { $geekads->messages()->template($to, 'order_update', 'en'); } catch (\GeekAds\Exceptions\ValidationException $e) { $e->fields(); // ['to' => ['…']] } catch (\GeekAds\Exceptions\ApiException $e) { $e->errorCode; // invalid_template, template_not_approved, opted_out, no_sender, meta_rejected, … $e->status; // HTTP status $e->details['meta_code'] ?? null; // WhatsApp's own error code for meta_rejected }
| Exception | When |
|---|---|
AuthenticationException |
Missing, wrong or revoked key (401) |
ValidationException |
Bad input (422) |
RateLimitException |
Still rate-limited after the retries |
ConnectionException |
GeekAds could not be reached. Retry with the same idempotency key |
Partner API (3Geeks apps)
use GeekAds\Partner\PartnerClient; $partner = new PartnerClient(getenv('GEEKADS_PARTNER_KEY'), getenv('GEEKADS_PARTNER_SECRET')); $result = $partner->provision('agency-17', 'Sea Breeze Travels', 'ravi@seabreeze.in', 'Ravi Kumar'); // $result['api_key'] is returned only when the workspace is created; store it with the agency $link = $partner->connectLink('agency-17', 'https://yourapp.example/settings/whatsapp'); // redirect the agency owner to $link['url'] to connect their own WhatsApp number
Testing your code
Pass any PSR-18 client, for example Guzzle with a MockHandler, as the third constructor argument. Laravel apps can use GeekAds::fake() from the Laravel package.
License
MIT