Build webhook subscriptions for your users with Sequin
Learn how to use Sequin to build a webhook subscription system for your users. Let your users receive webhook notifications whenever their data changes in your Postgres database.
Webhooks are a convenient way for your users to subscribe to data changes in your application. But creating a robust webhook system that can reliably deliver every change, handle errors, and scale to millions of messages per day is not an easy task.
Sequin’s webhook sinks combined with Sequin’s Management API give you the tools to build a user-facing webhook system that is reliable and feature rich:
- Capture and deliver every change that happens in Postgres to your users
- Customizable filters so users can subscribe to only the data they care about
- Backfill support so users can catch up on missed data
- Automatic retries and backoff
- Monitoring and alerting via Prometheus and Grafana
In this tutorial, you’ll build a simple Node.js application that allows users to subscribe to webhooks for their own data. You’ll use the Management API to dynamically create HTTP endpoints and webhook sinks that are specific to each user. You’ll also learn how backfills and transforms can be used to enable more complex use cases. Then, you’ll see how you can monitor the health of the system using Prometheus and Grafana.
Prerequisites
- Sequin installed and running locally
- A Postgres database connected to Sequin
- Node.js installed on your machine (if you want to run the code examples)
Retrieve your API token
To interact with the Management API, you’ll need an API token:
Log in to the Sequin console
With Sequin running, log in to the Sequin console at https://localhost:7376.
Navigate to the accounts page
Click the “Settings” gear icon in the bottom left corner and select “Manage Account”.
Copy your API key
In the API tokens section, copy the API token to your clipboard.
Setup your database tables
In the Postgres database connected to Sequin, create a table containing data you want to send to users. For this tutorial, we’ll work with a simple schema with three tables:
users
: The list of users that can subscribe to webhooks.notifications
: Notifications that are sent to users. This is an example of the kinds of data you might want to send to users.webhook_subscriptions
: This table tracks the webhook subscriptions for each user. In this case, it’ll store the webhook URL and the Sequin sink ID for each subscription.
Now, insert a user to work with:
Build the webhook subscription system
With the infrastructure in place, you’ll build a Node.js application that exposes an endpoint to create a webhook subscription for a user. You’ll use Express for the webserver, node-postgres (pg
) to connect to the database you just configured, and Axios to interact with the Management API.
Initialize the project
- Create a new directory for the project and navigate into it:
- Initialize the project with Express:
- Create a new file called
server.js
:
Write the server code
Add the following code to the server.js
file:
Stepping through this code:
- You standup a simple Express app that can connect to your local postgres database and make requests to the Sequin Management API.
- You define a
/subscribe
endpoint that allows a user to create a new webhook subscription by providing a webhook URL and a user ID.- The function calls the Management API to create a new HTTP endpoint using the webhook URL provided in the request.
- Then it creates a new webhook sink for the notifications table, filtered to the provided user.
- Finally, it saves the subscription details to your local postgres database.
Test your webhook subscription system
-
Run the server:
-
Create a test webhook URL to receive notifications. We suggest using webhook.site to easily inspect the incoming requests.
-
In a new terminal, make a POST request to the
/subscribe
endpoint to create a new webhook subscription: -
In the terminal running your server, you should see the following output:
You can confirm that the subscription was created by checking your
webhook_subscriptions
table in postgres as well. -
Now, insert a new notification into the
notifications
table in your database: -
You should see the notification appear in your test webhook:
-
You’ll also see the message appear on the messages tab for the webhook sink in the Sequin console.
Advanced: Custom payloads and backfills
You now have a basic webhook subscription system that can capture and deliver every change to your users. But there are some additional features you can add to make the system more robust and flexible.
Custom payloads
Using transforms you can define the exact data payload delivered to your users. For example, you probably don’t need to deliver all the metadata about the notification, just the notification data itself.
Navigate to the transforms page
In the Sequin console, navigate to the “Transforms” page and click to create a new transform.
Define your transform
Give your transform a name, select Function transform as the type, and enter a description if you’d like.
Now define the transform function. In this case, to just deliver the message
field, you can use the following function:
Click Create transform to save your transform.
Add the transform to your webhook sink
On the webhook sink configuration page, click the Edit button and in the Transform section select the transform you just created.
In the future, you can update your server.js
code to add this transform to the webhook sink automatically by adding the transform
key to the body of your create sink request:
Test your custom payload
Insert a new notification in your database:
You should see the following output in your test webhook URL:
Backfills
Backfills are a powerful feature that allows you to replay data for a user on demand. This is useful in several scenarios:
- When first initializing a webhook subscription, to send historical data to the user
- If a user temporarily loses connectivity and misses some webhook events
- When developers need to re-sync or debug webhook data delivery
Create a `/backfill` endpoint
In your server.js
file, add a new /backfill
endpoint that makes a POST request to the Sequin Management API to start a backfill:
This endpoint takes in the name of the webhook sink you want to backfill and makes a POST request to the Sequin Management API to start a backfill.
Test your backfill
Make a POST request to the /backfill
endpoint to start a backfill:
You should see all the messages in the notifications
table appear in your test webhook URL.
The Management API also provides a way to monitor the status of a backfill so you can show users the status of their backfill and notify them when it’s complete.
Monitoring and alerting
With your webhook subscription system in place, you can monitor the health of the system using Prometheus and Grafana.
Sequin comes with a pre-configured Grafana instance that you can access at http://localhost:3000. If you log in with the default credentials, you’ll see a dashboard with a few graphs to help you monitor your webhook sinks and backfills:
You can also ingest these metrics directly from Sequin’s /metrics
endpoint (on port 8376) to monitor Sequin in your tooling.
Resources
You now have a working proof-of-concept for a user-specific webhook subscription system. To take this to production, dig into the following resources:
Connect your production database
Learn how to connect your production database to Sequin.
Deploy Sequin in production
Learn how to deploy Sequin in production.
Webhook sinks
Learn more about how to configure your webhook sinks to deliver messages to your users.
Transforms
Learn how to transform messages before they are sent to your destination.
Configuration reference
Learn about all the configuration options available in sequin.yaml.