Supabase Edge Functions run on Deno, so fetch is built in. This function sends a welcome email to the signed-in user only, so nobody can use it to email other people.
Store the key
supabase secrets set ROBUSTEMAIL_API_KEY=rb_test_...
The function
// supabase/functions/send-welcome/index.ts
import { createClient } from "jsr:@supabase/supabase-js@2";
Deno.serve(async (req) => {
// Identify the caller from their Supabase session
const supabase = createClient(
Deno.env.get("SUPABASE_URL")!,
Deno.env.get("SUPABASE_ANON_KEY")!,
{ global: { headers: { Authorization: req.headers.get("Authorization") ?? "" } } },
);
const { data: { user } } = await supabase.auth.getUser();
if (!user?.email) {
return new Response("Unauthorized", { status: 401 });
}
const res = await fetch("https://robustemail.com/api/v2/emails", {
method: "POST",
headers: {
Authorization: `Bearer ${Deno.env.get("ROBUSTEMAIL_API_KEY")}`,
"Content-Type": "application/json",
"Idempotency-Key": `welcome-${user.id}`,
},
body: JSON.stringify({
from: "Acme <hello@acme.com>",
to: user.email,
subject: "Welcome to Acme",
html: "<h1>Welcome!</h1><p>Thanks for signing up.</p>",
}),
});
const body = await res.json();
return Response.json(res.ok ? { id: body.id } : { error: body.message }, { status: res.ok ? 200 : 502 });
});
SUPABASE_URL and SUPABASE_ANON_KEY are available to every Edge Function automatically.
Deploy and call it
supabase functions deploy send-welcome
// In your app, after the user signs in
const { data, error } = await supabase.functions.invoke("send-welcome");
The idempotency key is built from the user’s ID, so calling the function twice within 24 hours still sends only one welcome email.