Sending WhatsApp Messages from Cloudflare Workers
Cloudflare Workers run close to your users and close to zero cold-start cost, which makes them a good fit for two very different WhatsApp jobs: firing a message the instant an edge-side event happens, and running a scheduled sweep (reminders, digests) without keeping a server around. Here's how to do both against the TextMeFlow API, plus the one gotcha that trips people up the first time: idempotency without a database.
Sending a message from a Worker
A Worker is just a fetch handler, so sending a message is a plain HTTP call — no SDK required:
export default {
async fetch(request, env) {
const { to, text } = await request.json();
const res = await fetch("https://api.textmeflow.eu/v1/messages", {
method: "POST",
headers: {
"Authorization": `Bearer ${env.TEXTMEFLOW_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ to, type: "text", text: { body: text } }),
});
if (!res.ok) {
return new Response(await res.text(), { status: res.status });
}
return new Response("sent", { status: 200 });
},
};
Store the key as a Worker secret, not a plaintext var: wrangler secret put TEXTMEFLOW_API_KEY. It's then available on env at runtime but never shows up in wrangler.toml or your deployed bundle.
Scheduled sends with a Cron Trigger
For reminders that fire on a schedule rather than in response to a request, add a Cron Trigger in wrangler.toml:
[triggers]
crons = ["0 8 * * *"]
Then export a scheduled handler alongside (or instead of) fetch:
export default {
async scheduled(event, env, ctx) {
const due = await getDueReminders(env); // your own lookup, e.g. Workers KV or D1
for (const r of due) {
await fetch("https://api.textmeflow.eu/v1/messages", {
method: "POST",
headers: {
"Authorization": `Bearer ${env.TEXTMEFLOW_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ to: r.phone, type: "text", text: { body: r.message } }),
});
}
},
};
Cron Triggers run on UTC, so a daily 8am-local reminder needs its hour adjusted for the current offset if you want to avoid drifting across DST changes.
Idempotency without a server to hold state
The one thing a Worker doesn't give you for free is a place to remember "I already sent this." If a Cron Trigger invocation overlaps with a retry, or a fetch handler gets called twice for the same upstream event, you don't want a customer getting the same reminder twice. TextMeFlow's API doesn't deduplicate sends on your behalf — that's left to you, by design, since only you know what counts as a duplicate in your domain.
Workers KV is the natural fix: write a key for the thing you're about to send (a reminder ID, an order ID, whatever your idempotency boundary is) with a short TTL, and check it before sending:
const key = `sent:${reminderId}`;
if (await env.SENT_KV.get(key)) continue; // already sent
await env.SENT_KV.put(key, "1", { expirationTtl: 86400 });
// ...send the message
KV writes aren't instantly consistent across every edge location, so for anything stricter than "best-effort, duplicates are rare," put the KV check right next to the call that sends, not in a separate pass — the smaller the gap between check and send, the smaller the window for a race.
Handling replies
If customers reply to a Worker-sent message, route inbound webhooks to a second Worker route rather than trying to receive them on the same handler that sends. TextMeFlow signs every webhook with HMAC-SHA256, and verifying that signature in a Worker is the same crypto.subtle.importKey / crypto.subtle.verify dance as anywhere else running on V8 — no Node-specific crypto module needed, which is one less polyfill to worry about on the edge runtime. For the exact header name and payload shape, see the webhook signature guide.
Why the edge matters here
None of this requires an origin server at all — a Worker triggered by a form submission, a KV-backed idempotency check, and a Cron Trigger for anything scheduled covers most transactional WhatsApp flows without provisioning anything that has to stay running. Pair it with TextMeFlow's anti-spam pipeline — rate limits, STOP handling, quiet hours — and you get a reminder system that doesn't need its own uptime story.
Start on the free plan — 50 messages a month, no card required, enough to wire up and test a Worker end to end.
Want to send WhatsApp messages via API yourself?
Free forever up to 50 messages/month. Scan a QR and send your first message within 5 minutes.
Free forever