Guides

Send a WhatsApp message from PHP

HelloHop lets you send a WhatsApp text from PHP with one cURL POST to your linked channel. Send the recipient and text as JSON with the channel token and an Idempotency-Key. A 200 means WhatsApp accepted it, a 202 means queued, and a timeout means unknown. No Composer package is needed.

Facts (as of October 2026, API v1)

  • The send is POST /v1/sessions/{channel}/messages with type text, to and text, authenticated with the channel token.
  • The unified POST /v1/sessions/{channel}/messages route accepts an Idempotency-Key of up to 255 characters. Repeating the same key and body within 24 hours returns the first result without another send, with the response header Idempotent-Replayed: true.
  • A 200 with status server_ack means WhatsApp accepted the message. It is not a delivery receipt; delivered and read arrive as message.status webhooks.
  • A 504 send_unconfirmed, a timeout, a 409 idempotency_uncertain or a 503 with ambiguous true means the message may have gone out. Treat it as possibly delivered and look at the chat before you send it again.
  • The free sandbox channel allows 180 messages sent a day and 10 new chats a month, counted in Bangkok time. That is enough to build and test the whole flow before you pay.
  • Prices: from $20 USD per number per month, billed annually, with a free sandbox and no per-message fees.

Steps

  1. Get a channel token. Link a number in the console and create its channel token. Set CHANNEL_ID and CHANNEL_TOKEN in the environment of the PHP process.
  2. Send once. Call send_whatsapp with a key that names the business event, for example an order id, and read the outcome it returns.
  3. Act on the outcome. Mark sent, queued and unknown. Retry only a retry outcome, with the same key and body. Never resend an unknown.
  4. Call it from a hook. In WordPress, call the same function from a WooCommerce order hook.

Send text with cURL in PHP

Save the sample as send.php and run it with CHANNEL_ID and CHANNEL_TOKEN set. It needs the PHP curl extension only. The function returns an outcome of sent, queued, retry, unknown or failed. A timeout or a lost connection comes back as unknown. For a retry it returns retry_after, the seconds to wait, and does not sleep itself.

PHP
<?php
// Plain cURL, no Composer. Needs the PHP curl extension.
// cURL options: https://www.php.net/manual/en/function.curl-setopt.php and https://www.php.net/manual/en/curl.constants.php
const API_BASE = 'https://api.hellohop.app/v1';

// One send. Repeating the same key with the same body returns the first result.
function send_whatsapp(string $key, array $message): array {
    $retryAfter = null;
    $ch = curl_init(API_BASE . '/sessions/' . getenv('CHANNEL_ID') . '/messages');
    curl_setopt_array($ch, [
        CURLOPT_POST => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_TIMEOUT => 15,
        CURLOPT_HTTPHEADER => [
            'Authorization: Bearer ' . getenv('CHANNEL_TOKEN'),
            'Content-Type: application/json',
            'Idempotency-Key: ' . $key,
        ],
        CURLOPT_POSTFIELDS => json_encode($message),
        CURLOPT_HEADERFUNCTION => function ($ch, string $line) use (&$retryAfter) {
            if (stripos($line, 'Retry-After:') === 0) $retryAfter = trim(substr($line, 12));
            return strlen($line);
        },
    ]);
    $body = curl_exec($ch);
    if ($body === false) {
        return ['outcome' => 'unknown', 'status' => 0]; // timeout or lost connection: the message may have gone out
    }
    $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $data = json_decode($body, true);
    $data = is_array($data) ? $data : [];
    $outcome = classify_send($status, $data['code'] ?? null, $data['ambiguous'] ?? null);
    $wait = ctype_digit((string) $retryAfter) ? (int) $retryAfter : 1; // a date value falls back to 1 second
    return ['outcome' => $outcome, 'status' => $status, 'code' => $data['code'] ?? null, 'id' => $data['id'] ?? null, 'retry_after' => $outcome === 'retry' ? $wait : null];
}

// sent (server_ack), queued, retry, unknown or failed.
function classify_send(int $status, ?string $code, $ambiguous): string {
    if ($status === 200) return 'sent';   // server_ack, not delivered
    if ($status === 202) return 'queued'; // observe webhooks, do not resend
    // Refused before sending: repeat the same key and body after retry_after seconds.
    $refused = ['upstream_unavailable', 'hopsmart_settings_unavailable', 'operator_unresolved'];
    if ($status === 429 || ($status === 503 && ($ambiguous === false || in_array($code, $refused, true)))) return 'retry';
    // Possibly sent: look at the chat, never resend.
    if ($status === 0 || $status >= 500 || $code === 'idempotency_uncertain') return 'unknown';
    return 'failed'; // an unlinked channel, idempotency_conflict, a bad request: fix it, do not retry
}

if (PHP_SAPI === 'cli' && realpath($argv[0]) === __FILE__) {
    $result = send_whatsapp('welcome-66XXXXXXXXX-1', ['type' => 'text', 'to' => '66XXXXXXXXX', 'text' => 'Hello from PHP']);
    echo json_encode($result), "\n";
}

What the handler does with each send result

Send resultWhat your handler does
200 server_ackWhatsApp acknowledged the send. Mark it sent and save id; this is not a delivery receipt.
202 queuedPersisted in the queue, not delivered. A channel that queues its sends (for example while accepted sends drain) answers every text send this way, with id, status queued, position and expires_at. Mark it queued and save id. Observe the result through webhooks without resending.
429, or 503 with ambiguous false or code upstream_unavailable, hopsmart_settings_unavailable or operator_unresolvedRefused before sending. Honor Retry-After, keep the same key and body, and retry later. The Node helper waits at most 30 seconds before returning retry with a retryAt time, and the pages that keep a due time skip the row until then; the other Node timer pages retry on their next run. The Flask, Python reminder and PHP samples return the time to retry instead of waiting; the Sheets helper saves next_attempt_at and skips the row until then.
504 send_unconfirmed, 409 idempotency_uncertain, 503 with ambiguous true, a timeout or a lost responsePossibly delivered. Mark it unknown and look at the chat before any resend. Never force a resend of an uncertain key with a new key.
409 idempotency_conflictThe same key was used for a different message. Fix how you build the key. The first message is unchanged.
Any other 409Mark it failed. Fix the channel before scheduling another attempt.
Other 4xxThe request is wrong and a retry will not fix it. Log it and move on.

Subscribe to message and message.status webhooks and verify their signatures. The result of a queued send arrives as a message event: match the saved response id to payload.id. message.status reports delivered or read for each id in payload.ids. A send held by HopSmart™ expires 72 hours after acceptance, and the 202 carries expires_at. If no event arrives, read the job with GET /hopsmart/jobs/{id}. Save event ids to deduplicate. Do not resend queued work while waiting for a result.

WooCommerce: message a customer when an order completes

WooCommerce fires woocommerce_order_status_completed when an order moves to completed and passes the order id and the order object as its first two arguments. The hook below calls the same send_whatsapp function. Because the key is built from the order id and status, a hook that runs twice within 24 hours returns the first result instead of sending again. The hook skips an order whose billing phone does not start with +, because the send needs the country code. A retry outcome leaves the order unmarked and the hook does not fire again on its own, so schedule the same call yourself with the same key. The call waits up to 15 seconds, so on a busy shop run it from a background job. Send only to customers who agreed to WhatsApp messages: has_whatsapp_consent reads an order meta field as a stand-in, so replace it with your real consent record. Under PHP-FPM with clear_env on, getenv() returns nothing: pass CHANNEL_ID and CHANNEL_TOKEN from wp-config.php instead, or the send fails and the order is marked failed for good. Take the wording from your own copy.

PHP: WooCommerce hook
<?php
// WooCommerce fires woocommerce_order_status_{new status} from WC_Order::status_transition() with the order id and the order:
// https://github.com/woocommerce/woocommerce/blob/trunk/plugins/woocommerce/includes/class-wc-order.php
// Put this beside send_whatsapp() in a small plugin.
// Replace this stub with your real consent source: here it reads an order meta field that your checkout would set to 'yes'.
function has_whatsapp_consent($order): bool {
    return $order->get_meta('_whatsapp_consent') === 'yes';
}

add_action('woocommerce_order_status_completed', function (int $order_id, $order) {
    if ($order->get_meta('_wa_outcome')) return; // already sent, queued or unknown
    $phone = $order->get_billing_phone();
    if (substr($phone, 0, 1) !== '+' || !has_whatsapp_consent($order)) return;
    // The key names the order and status, so a second hook run returns the first result instead of sending again.
    $result = send_whatsapp("woo-{$order_id}-completed", [
        'type' => 'text',
        'to' => preg_replace('/\D/', '', $phone),
        'text' => 'Hi ' . $order->get_billing_first_name() . ', order ' . $order->get_order_number() . ' is complete.',
    ]);
    if ($result['outcome'] !== 'retry') { // retry stays unmarked: run it again later with the same key
        $order->update_meta_data('_wa_outcome', $result['outcome']);
        $order->save();
    }
}, 10, 2);

Questions

Do I need Composer or a library?
No. The sample uses the PHP curl extension and no Composer package. Run php -m to check that curl is enabled.
What is the Idempotency-Key for?
Repeating the same key and body within 24 hours returns the first result without another send, so a retried request or a repeated hook does not message the customer twice.
Does a 200 mean the customer received it?
No. It means WhatsApp acknowledged the send. Delivery and read receipts arrive as message.status webhooks.
What if the request times out?
Treat it as unknown. The message may have gone out, so look at the chat before sending again.
Why might a send answer 202?
A channel that queues its sends, for example while accepted sends are still draining, answers 202 with status queued. That means accepted into the queue, not delivered. The result arrives later as a message webhook.

Free sandbox, forever. Upgrade when you're ready.

Start free

HelloHop is an independent WhatsApp API for developers: send and receive messages, automate groups and communities, and get signed webhooks. Not affiliated with WhatsApp or Meta. You link your own number.

Send a WhatsApp Message from PHP (cURL) | HelloHop