Edge function <> Trigger
I am trying to receieve an email when a row is created. I am using Resend and i tested the API manually on postman and it is working fine. I tried everything i added the trigger on the front-end and i also created the triggers with the API on supabase and nothing is working. I need help
8 Replies
You'll have to explain a bit more on how you are sending the emails.
Postgres trigger functions can call pg_net or http extensions which can call an edge function (or even a REST API directly). Supabase has webhooks which is basically a packaged trigger function using pg_net.
The most common way is to have those call an edge function and then have that function call your email provider.
You can debug the edge function separately from the trigger.
You are not clear on what method you are attempting or what your issue is.
I created a function with the Resend API and domain which created a trigger dependent on it. It also reflected on my front en code under supabase index.ts
Please show the code. Also we don't know what supabase index.ts is as Supabase doesn't have any set structure for a project besides it's edge functions which are created in specific directories.
BEGIN
PERFORM net.http_post(
url := 'https://api.resend.com/emails',
headers := jsonb_build_object(
'Content-Type', 'application/json',
'Authorization', 'Bearer API' -- API key
),
body := jsonb_build_object(
'from', 'Startup-Sprint <taymour@startup-sprint.com>', -- Verified sender email
'to', 'taymour@startup-sprint.com', -- Verified receiving email
'subject', 'New Startup Sprint Booking: ' NEW.company,
'html', '
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
<h1 style="color: #2563eb; margin-bottom: 24px;">New Startup Sprint Booking</h1>
<div style="background: #f8fafc; padding: 24px; border-radius: 8px;">
<p style="margin-bottom: 16px;"><strong>Name:</strong> ' NEW.name '</p>
<p style="margin-bottom: 16px;"><strong>Email:</strong> ' NEW.email '</p>
<p style="margin-bottom: 16px;"><strong>Company:</strong> ' NEW.company '</p>
<p style="margin-bottom: 16px;"><strong>Message:</strong></p>
<p style="background: white; padding: 16px; border-radius: 4px;">' NEW.message || '</p>
</div>
</div>
'
)::text
);
RETURN NEW;
EXCEPTION
WHEN OTHERS THEN
-- Log error but don't prevent insert
RAISE WARNING 'Failed to send email: %', SQLERRM;
RETURN NEW;
END;
the API is filled in though i just removed it
You can add
raise log 'function called and new = %',new;
and check postgres logs to see if it is called. That can also be used for debugging.
Have you tried running the function from the SQL editor?
Any errors in the Postgres logs or in the response table?
Also your title says edge function but this is a database trigger function.
What is your trigger to call it?
Ill add the raise log line.
I don't see the _http_response table in my setup. Could you confirm if this is supposed to be created automatically or if I need to set up a custom response logging mechanism manually?
That table is created in the net schema when pg_net extension is installed