Code examples, official WhatsApp API. Published November 9, 2026

Send a WhatsApp message with an API: working code in cURL, PHP, Node.js and Python

One POST request sends a WhatsApp message from your code. Below: the request in four languages, templates for when the 24-hour window is closed, media, and a webhook to receive replies.

What you need before the first request

To send a WhatsApp message with an API you need three things: a number connected to Meta's official WhatsApp Cloud API, the ID of that number, and an API key.

  1. Create a free trial account at api.ubli.io (3 days, no credit card).
  2. Connect your Cloud API number. If you do not have one yet, follow the WhatsApp Cloud API setup guide.
  3. Create an API key in the API & Tokens menu and copy the ID of your number (device_id).

Send a text message in four languages

The same request in cURL, PHP, Node.js and Python. Free-form text works during the 24 hours after the customer last wrote to you. The phone number goes with the country code, digits only.

WhatsApp API with cURL

curl -X POST "https://api.ubli.io/api/messages/send" \
  -H "Content-Type: application/json" \
  -H "x-api-key: zapbr_YOUR_KEY" \
  -d '{
    "device_id": "YOUR_NUMBER_ID",
    "phone_number": "15551234567",
    "message": "Hi! Your order is out for delivery."
  }'

WhatsApp API in PHP

<?php
$ch = curl_init("https://api.ubli.io/api/messages/send");
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        "Content-Type: application/json",
        "x-api-key: zapbr_YOUR_KEY",
    ],
    CURLOPT_POSTFIELDS => json_encode([
        "device_id" => "YOUR_NUMBER_ID",
        "phone_number" => "15551234567",
        "message" => "Hi! Your order is out for delivery.",
    ]),
]);
$response = json_decode(curl_exec($ch), true);
curl_close($ch);
var_dump($response["success"]);

WhatsApp API in Node.js

const res = await fetch("https://api.ubli.io/api/messages/send", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "x-api-key": process.env.UBLI_API_KEY,
  },
  body: JSON.stringify({
    device_id: "YOUR_NUMBER_ID",
    phone_number: "15551234567",
    message: "Hi! Your order is out for delivery.",
  }),
});
const data = await res.json();
console.log(data.success, data.data?.status);

WhatsApp API in Python

import os
import requests

res = requests.post(
    "https://api.ubli.io/api/messages/send",
    headers={"x-api-key": os.environ["UBLI_API_KEY"]},
    json={
        "device_id": "YOUR_NUMBER_ID",
        "phone_number": "15551234567",
        "message": "Hi! Your order is out for delivery.",
    },
    timeout=30,
)
print(res.json()["success"])

A successful call returns "success": true and the message status in data.status. Full reference in the API documentation.

Send a template when the window is closed

To start a conversation, or to write more than 24 hours after the customer's last message, Meta requires an approved template. Variables go in components, in the same order as in the template.

curl -X POST "https://api.ubli.io/api/messages/send-template" \
  -H "Content-Type: application/json" \
  -H "x-api-key: zapbr_YOUR_KEY" \
  -d '{
    "device_id": "YOUR_NUMBER_ID",
    "phone_number": "15551234567",
    "template_name": "order_shipped",
    "language_code": "en_US",
    "components": [
      { "type": "body", "parameters": [ { "type": "text", "text": "#1042" } ] }
    ]
  }'

Send an image, video, audio or document

Pass a public URL and Ubli downloads the file and sends it. The caption is optional.

curl -X POST "https://api.ubli.io/api/messages/send-media" \
  -H "Content-Type: application/json" \
  -H "x-api-key: zapbr_YOUR_KEY" \
  -d '{
    "device_id": "YOUR_NUMBER_ID",
    "phone_number": "15551234567",
    "media_url": "https://example.com/invoice-1042.pdf",
    "caption": "Your invoice"
  }'

Receive replies with a webhook

Register your URL in the Webhooks menu. Every event arrives as a POST signed with HMAC SHA-256 in the X-Webhook-Signature header, using your webhook secret. Check the signature before trusting the payload.

import crypto from "node:crypto";
import express from "express";

const app = express();

// Keep the raw body: the signature is calculated over the exact bytes we send.
app.post("/whatsapp/webhook", express.raw({ type: "application/json" }), (req, res) => {
  const expected = crypto
    .createHmac("sha256", process.env.UBLI_WEBHOOK_SECRET)
    .update(req.body)
    .digest("hex");
  const received = req.get("X-Webhook-Signature") || "";
  if (received.length !== expected.length ||
      !crypto.timingSafeEqual(Buffer.from(received), Buffer.from(expected))) {
    return res.sendStatus(401);
  }

  const event = JSON.parse(req.body.toString());
  if (event.event === "message.received") {
    console.log("From", event.data.from, ":", event.data.content);
  }
  res.sendStatus(200); // answer fast; do the heavy work in a queue
});

app.listen(3000);

Other events: message.sent, message.ack (delivered and read), message.failed, device.connected and device.disconnected.

Frequently asked questions

How do I send a WhatsApp message with an API?+

You send an HTTPS POST request with the destination number and the text to a WhatsApp API endpoint, authenticated with an API key. On Ubli, that is POST https://api.ubli.io/api/messages/send with the x-api-key header and a JSON body with device_id, phone_number and message.

How do I get a WhatsApp API key?+

Meta does not hand out a single API key: the Cloud API uses an access token from your Meta app. On Ubli, you connect your number once and then create API keys in the API & Tokens menu of the dashboard. Each key goes in the x-api-key header.

Why does my WhatsApp API message fail outside the 24-hour window?+

Because Meta only allows free-form text during the 24 hours after the customer last wrote to you. Outside that window you have to send an approved message template, through the send-template endpoint.

What phone number format does the API expect?+

Digits only, with the country code and without plus signs, spaces or dashes. A US number like (555) 123-4567 becomes 15551234567.

How do I receive WhatsApp messages in my system?+

With a webhook. You register an HTTPS URL in the dashboard and Ubli sends a POST with the message.received event every time a customer writes, signed with HMAC SHA-256 in the X-Webhook-Signature header. Failed deliveries are retried.

Can I send the same message to many customers?+

Only to people who agreed to hear from you, and only with templates approved by Meta, within Meta's messaging limits and policies. Meta currently does not deliver marketing templates to US numbers. Sending unsolicited messages gets numbers blocked.

Send your first WhatsApp message today

Create your account, connect your number and run the cURL example above. $39 per month after the trial.