Svix Blog
Published on

Send Webhooks from Supabase using Database Triggers

  • avatar
    Luciano Bianchi

Cover image

Svix is the enterprise-ready webhooks sending service. With Svix, you can build a secure, reliable, and scalable webhook platform in minutes. Looking to send webhooks? Give it a try!

Supabase Database Triggers are a great way to start sending webhooks from Supabase. They are a convenient way of ensuring that the webhooks we send to our users are always consistent with what's in our database, without the need to spin up any new service or write complex code to do so. This article will show you how to use Svix from a database function to send a webhook every time a database event occurs.

To provide webhooks as part of your API, Svix is a robust alternative to Supabase Database Webhooks. By using Svix, you get all the features needed to provide a production-ready webhooks solution: signatures, retries, logging and monitoring, and a lot more.

This implementation also guarantees that our solution will be compatible with the Standard Webhooks specification, which is a set of conventions to be followed by webhook producers (senders) to provide webhook consumers (receivers) a secure, consistent, and interoperable interface for webhooks.

Setup Svix with Supabase Database triggers

Create a Svix account

Sign up with Svix and follow the Getting Started instructions. Get your API Authentication Token from the Svix dashboard.

Save the auth token in Supabase Vault

Create a secret in Supabase Vault to save the Svix API Key. We'll use it to call the Svix API from functions in Supabase.

SELECT vault.create_secret('<your-svix-api-key>', 'SVIX_API_KEY', 'Svix API Key');

Create a Svix application

In Svix, applications are where messages are sent to. In most cases, you would want to have one application for each of your users. In this example, we'll manually create an application with a curl command.

curl -X 'POST' \
  '' \
  -H 'Authorization: Bearer <SVIX_API_KEY>' \
  -H 'Accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
        "name": "My application",
        "uid": "unique-app-identifier"

In a real setting, the Svix application can be created automatically through the API when the user signs up. We could use a similar database function like the one we use to send messages.

Create a function to send a message to Svix

In Supabase, declare the function that will call Svix to send a webhook. It will be in charge of building the webhook payload and deciding the application id where the message will be sent to.

We're using an example invoices table that contains information for each new invoice we create, along with the user_id that the invoice belongs to.

CREATE OR REPLACE FUNCTION public.invoice_created_webhook()
    RETURNS trigger
    SET search_path = public
    LANGUAGE 'plpgsql'
    svix_api_key text;
    payload jsonb;
    svix_response jsonb;
    -- Read SVIX API Key from the vault
    SELECT decrypted_secret INTO svix_api_key FROM vault.decrypted_secrets WHERE name = 'SVIX_API_KEY' LIMIT 1;

    -- Generate the webhook payload
    payload = jsonb_build_object(
        'eventType', 'invoice.created',
        'payload', new

    -- Send the SVIX request
            '' || new.user_id || '/msg/', -- using user_id as the Svix app id. This can change depending on your setup.
                'Content-Type', 'application/json',
                'Authorization', 'Bearer ' || svix_api_key
        ) INTO svix_response;

    RETURN new;

Create a database trigger

To send a webhook every time a database event occurs, create a trigger that will fire the webhook function declared above.

CREATE OR REPLACE TRIGGER invoice_created_webhook
    ON public.invoice
EXECUTE FUNCTION invoice_created_webhook();

In this example, every time a new row is added to the table, a message will be sent.

Add webhook endpoints

We showed how to send messages, though they are not being sent to any specific URL. In order for users to start listening to our webhooks, we need to add endpoints.

Check out the Svix docs for reference on how to add webhook endpoints using the API or using the Svix App Portal.

That's it! We've demonstrated how to implement a powerful, scalable webhook system using Svix and Supabase with little effort.

If you want to know more about how to get started with Svix, check out our docs.

For more content like this, make sure to follow us on Twitter, Github, or RSS for the latest updates for the Svix webhook service, or join the discussion on our community Slack.