Sending WhatsApp Messages from Supabase Edge Functions
If your backend is Supabase, you already have Postgres, auth, storage, and a Deno-based serverless runtime in one place — which means you don't need a separate Node server just to send a WhatsApp notification when a row changes. A Supabase Edge Function can call the TextMeFlow API directly, and a database webhook can trigger that function the moment a row is inserted or updated. No polling, no cron job checking "did anything change since last time."
This covers the two ways to wire it up: a database webhook firing on a table event, and a scheduled Edge Function for reminders that aren't tied to a single row change.
Create the Edge Function
Scaffold a new function with the Supabase CLI:
supabase functions new send-whatsapp
This creates supabase/functions/send-whatsapp/index.ts. Write it to accept a phone number and text, then call the TextMeFlow messages endpoint:
import "jsr:@supabase/functions-js/edge-runtime.d.ts"
const API_KEY = Deno.env.get("TEXTMEFLOW_API_KEY")!
Deno.serve(async (req) => {
const { to, text } = await req.json()
if (!to || !text) {
return new Response(JSON.stringify({ error: "to and text are required" }), {
status: 400,
})
}
const res = await fetch("https://api.textmeflow.eu/v1/messages", {
method: "POST",
headers: {
"Authorization": `Bearer ${API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ to, text }),
})
const body = await res.json()
return new Response(JSON.stringify(body), { status: res.status })
})
Store the API key as a function secret rather than hardcoding it, and deploy:
supabase secrets set TEXTMEFLOW_API_KEY=tmf_your_api_key_here
supabase functions deploy send-whatsapp
A tmf_ key is what every TextMeFlow account gets at signup, and this function treats it as a secret the same way you'd treat a database password — never in client-side code, never in a migration file.
Trigger it from a database webhook
Supabase's database webhooks (under Database → Webhooks in the dashboard, or via a supabase_functions trigger in SQL) fire an HTTP request whenever a row is inserted, updated, or deleted on a table you pick. Point one at your Edge Function and you get a WhatsApp message on any data change, with no polling loop anywhere.
Say you have an orders table and want a WhatsApp message the moment status flips to shipped. Create the webhook in SQL:
create trigger notify_order_shipped
after update on orders
for each row
when (old.status is distinct from new.status and new.status = 'shipped')
execute function supabase_functions.http_request(
'https://<project-ref>.supabase.co/functions/v1/send-whatsapp',
'POST',
'{"Content-Type":"application/json"}',
'{}',
'1000'
);
The trigger passes the row data as the request body by default, so your Edge Function needs to pull to and text out of the Supabase webhook payload shape (record.customer_phone, record.status, etc.) instead of a flat {to, text} object — adjust the function to build the message text from whatever columns are relevant, then call the TextMeFlow API the same way. This pattern works for anything transactional: a signup confirmation, a support ticket closing, a payment row landing in a payments table synced from Stripe or Mollie — see sending WhatsApp confirmations from Stripe webhooks if Supabase is just your database layer and Stripe is the actual trigger source.
Scheduled reminders with pg_cron
Not every message maps to a single row change — a "your appointment is tomorrow" reminder needs to fire based on the clock, not an update. Supabase supports pg_cron as a Postgres extension, which you can use to call your Edge Function on a schedule:
select cron.schedule(
'appointment-reminders',
'0 9 * * *',
$$
select
net.http_post(
url := 'https://<project-ref>.supabase.co/functions/v1/send-whatsapp',
headers := '{"Content-Type":"application/json"}'::jsonb,
body := jsonb_build_object('to', phone, 'text', 'Reminder: your appointment is tomorrow at ' || appt_time)
)
from appointments
where appt_date = current_date + 1;
$$
);
This runs daily at 09:00 UTC, finds every appointment happening tomorrow, and fires one WhatsApp send per row via pg_net's http_post. It's the same mechanism several hosted automation tools use under the hood, just running inside your own database instead of a third-party scheduler — useful if you'd rather not add another external dependency for something this simple.
Handle the response, don't fire-and-forget
A 202 Accepted with a message_id and status: "queued" means TextMeFlow accepted the job, not that WhatsApp delivered it. If you want delivery confirmation, write the message_id back into a whatsapp_messages table and let your incoming webhook handler update the row when the status webhook arrives — that way a failed or undelivered send shows up in a query instead of silently disappearing into a function log you'll never open again.
Database webhooks also retry on a non-2xx response, so make sure your Edge Function returns the TextMeFlow API's actual status code rather than always returning 200 — otherwise a real failure (bad phone format, session disconnected, rate limit) looks like success to Supabase and never gets retried or surfaced.
Why not just call the API from your frontend
It's tempting to skip the Edge Function and call TextMeFlow directly from a Supabase client-side insert, but that puts your API key in the browser bundle, where anyone can extract it and send messages on your account's quota. Routing through an Edge Function keeps the key server-side, lets you validate and rate-limit before the call goes out, and gives you one place to add logging if a particular trigger starts firing more than expected.
Sign up for the free plan to get an API key and test this against your own Supabase project — 50 messages a month costs nothing while you get the trigger wiring right.
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