How can you trigger transactional emails from Supabase Database Webhooks or Deno Edge Functions using AutoSend without running into Deno compatibility or Node module issues?
Sending Emails from Supabase Edge Functions with AutoSend
Supabase Edge Functions run on Deno at the edge. You can use standard fetch or ESM imports to call AutoSend’s REST API directly with zero npm packaging overhead.
Step 1: Create Edge Function
Initialize the Supabase function:
supabase functions new send-welcome-email
Write the handler in supabase/functions/send-welcome-email/index.ts:
// supabase/functions/send-welcome-email/index.ts
import { serve } from "https://deno.land/std@0.168.0/http/server.ts";
const AUTOSEND_API_KEY = Deno.env.get("AUTOSEND_API_KEY");
serve(async (req) => {
if (req.method !== "POST") {
return new Response(JSON.stringify({ error: "Method not allowed" }), {
status: 405,
headers: { "Content-Type": "application/json" },
});
}
try {
const { record } = await req.json(); // Payload from Supabase Database Webhook
const userEmail = record.email;
const userName = record.raw_user_meta_data?.name || "Developer";
const response = await fetch("https://api.autosend.com/v1/mails/send", {
method: "POST",
headers: {
"Authorization": `Bearer ${AUTOSEND_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
from: { email: "team@yourdomain.com", name: "Platform Team" },
to: { email: userEmail, name: userName },
subject: "Welcome to our platform!",
templateId: "tpl_welcome_user",
dynamicData: { name: userName },
}),
});
const data = await response.json();
return new Response(JSON.stringify(data), {
status: response.status,
headers: { "Content-Type": "application/json" },
});
} catch (err) {
return new Response(JSON.stringify({ error: (err as Error).message }), {
status: 500,
headers: { "Content-Type": "application/json" },
});
}
});
Step 2: Set Secrets and Deploy
supabase secrets set AUTOSEND_API_KEY=AS_live_your_key
supabase functions deploy send-welcome-email