To send an SMS from Supabase or Firebase, call your provider's REST API from a server-side function, with the API key stored as a function secret, and trigger the function from a database change or an HTTP call. Never put the key in a client app that talks to the database directly.
We make Ready, so the examples use the Ready REST API and only the request shape we document. Both platforms run TypeScript or JavaScript with fetch, so the request is nearly identical.
Why a function and not the client
Supabase and Firebase apps often run most logic in the browser or mobile app. That's fine for reading data, but a texting key is a credential that can spend money and message real people. Put it in a function the client can't read, and let the function check who's asking. Row-level security rules and auth checks should decide whether a user may trigger a text to a given number.
Supabase Edge Function (Deno)
Set the secret once with the Supabase CLI: supabase secrets set READY_API_KEY=your-key. Then the function:
// supabase/functions/notify/index.ts
Deno.serve(async (req) => {
const { phone, text, dedupeKey } = await req.json();
if (!dedupeKey) return new Response("dedupeKey required", { status: 400 });
const res = await fetch("https://api.tryready.com/v1/messages", {
method: "POST",
headers: {
"X-API-Key": Deno.env.get("READY_API_KEY")!,
"Content-Type": "application/json",
"Idempotency-Key": dedupeKey,
},
body: JSON.stringify({ to: phone, body: text, consent_attested: true }),
signal: AbortSignal.timeout(10000),
});
return new Response(await res.text(), {
status: res.status,
headers: { "Content-Type": "application/json" },
});
}); To text when a row changes, create a database webhook (or a pg_net call from a trigger) that invokes this function with the row's ID as the dedupeKey. Using the row ID plus the event name means a retried webhook can't send twice. The function refuses a call with no dedupeKey, because a missing key would pin every text to one key and later sends would replay the first.
Firebase Cloud Function
Store the secret with firebase functions:secrets:set READY_API_KEY. Then trigger on a Firestore write:
import { onDocumentCreated } from "firebase-functions/v2/firestore";
import { defineSecret } from "firebase-functions/params";
const readyKey = defineSecret("READY_API_KEY");
export const textOnOrder = onDocumentCreated(
{ document: "orders/{orderId}", secrets: [readyKey] },
async (event) => {
const order = event.data?.data();
if (!order?.phone) return;
const res = await fetch("https://api.tryready.com/v1/messages", {
method: "POST",
headers: {
"X-API-Key": readyKey.value(),
"Content-Type": "application/json",
"Idempotency-Key": `order-${event.params.orderId}-confirm`,
},
body: JSON.stringify({
to: order.phone,
body: "We got your order. We'll text again when it ships.",
consent_attested: true,
}),
signal: AbortSignal.timeout(10000),
});
// 429 and 500 (not sent) are safe to retry with the same key. A 503 was not sent either,
// but the same key replays it: it falls through to the console.error below.
if (res.status === 429 || res.status === 500) {
throw new Error(`retryable ${res.status}`);
}
if (res.status === 409 && (await res.clone().text()).includes("idempotency_in_progress")) {
throw new Error(`retryable ${res.status}`);
}
if (!res.ok) {
console.error("order text not sent", { orderId: event.params.orderId, status: res.status, body: await res.text() });
}
}
); Firestore triggers deliver at least once, which means the function can run twice for one document. The idempotency key built from the order ID is what makes that harmless. A non-retryable refusal (an opted-out recipient, a missing card) is logged by the console.error line; set an alert on that log message so a text that never went out doesn't pass unseen. If you enable retries on the function, throwing on a retryable status (a timeout or a 429) lets the platform try again with the same key. A 503 was not sent, but the same key replays that stored error, so resend it under a new key; after a 504, or a code ending in OUTCOME_UNKNOWN, check GET /v1/messages first. Read why duplicate texts happen for the contract behind this.
Receiving replies
Both platforms can host a webhook endpoint: an Edge Function or an HTTPS Cloud Function. Read the raw body first, verify the signature as described in the docs, return 2xx fast and write the message to a table for later processing. Event names, fields and signature headers are in the docs quickstart; our inbound webhooks guide covers the patterns, and signing and authentication the checks.
Limits and gotchas
- Cold starts and timeouts. A text is one HTTP call, so it's quick, but set a client timeout under the platform's limit.
- Fan-out. A trigger on a big table update can start thousands of invocations. Queue them, or call the bulk endpoint (up to 100 numbers per call, docs).
- Phone formats. Normalize to E.164 before the call (guide).
- Consent. Store each person's opt-in yourself.
consent_attestedis your statement, not a stored record.
Cost
A segment on the Standard rate is $0.0075 plus a $0.0045 carrier fee, passed through at cost and excluding optional screening. An account can send up to 500 texts a month before it registers for 10DLC; most approvals take 2 to 7 days. See pricing.
FAQ
Can I call the SMS API directly from the Supabase client library?
Technically you could, but your key would ship to every browser. Always go through an Edge Function or another server-side endpoint.
How do I stop a database trigger from texting twice?
Derive the idempotency key from the row ID and the event, such as order-123-confirm. Repeated deliveries of the same event then collapse into one text.
Which is better for texting, Supabase or Firebase?
Neither. Both are fine as a host. Pick on the rest of your stack; the SMS call is the same HTTPS request either way.
The short version
Keep the key in function secrets, trigger from data changes, build idempotency keys from row IDs and queue large fan-outs. For a plain server version, see how to add SMS to your app.