Changelog #34

·

HTTP endpoints: trigger jobs from any webhooks

Matt Aitken

Matt Aitken

CEO, Trigger.dev

Image for HTTP endpoints: trigger jobs from any webhooks

Often you want to trigger your Jobs from events that happen in other APIs. This is where webhooks come in.

Now you can easily subscribe to any APIs that support webhooks, without needing to use a Trigger.dev Integration. This should unlock you to create far more Jobs than was previously possible.

How to create an HTTP endpoint

We want to send a Slack message when one of our cal.com meetings is cancelled. To do this we need to create an HTTP endpoint that cal.com can send a webhook to.


_15
//create an HTTP endpoint
_15
const caldotcom = client.defineHttpEndpoint({
_15
id: "cal.com",
_15
source: "cal.com",
_15
icon: "caldotcom",
_15
verify: async (request) => {
_15
//this helper function makes verifying most webhooks easy
_15
return await verifyRequestSignature({
_15
request,
_15
headerName: "X-Cal-Signature-256",
_15
secret: process.env.CALDOTCOM_SECRET!,
_15
algorithm: "sha256",
_15
});
_15
},
_15
});

Getting the URL and secret from the Trigger.dev dashboard

There's a new section in the sidebar: "HTTP endpoints".

HTTP endpoints

From there you can select the HTTP endpoint you just created and get the URL and secret. In this case, it's cal.com.

HTTP endpoint details

Each environment has a different Webhook URL so you can control which environment you want to trigger Jobs for.

Setting up the webhook in cal.com

In cal.com you can navigate to "Settings/Webhooks/New" to create a new webhook.

cal.com webhook

Enter the URL and secret from the Trigger.dev dashboard and select the events you want to trigger Jobs for.

We could only select "Booking cancelled" but we're going to select all the events so we can reuse this webhook for more than just a single trigger.

Using HTTP endpoints to create Triggers

Then we can use that HTTP endpoint to create multiple Triggers for your Jobs. They can have different filters, using the data from the webhook.


_24
client.defineJob({
_24
id: "http-caldotcom",
_24
name: "HTTP Cal.com",
_24
version: "1.0.0",
_24
enabled: true,
_24
//create a Trigger from the HTTP endpoint above. The filter is optional.
_24
trigger: caldotcom.onRequest({
_24
filter: { body: { triggerEvent: ["BOOKING_CANCELLED"] } },
_24
}),
_24
run: async (request, io, ctx) => {
_24
//note that when using HTTP endpoints, the first parameter is the request
_24
//you need to get the body, usually it will be json so you do:
_24
const body = await request.json();
_24
_24
//this prints out "Matt Aitken cancelled their meeting"
_24
await io.logger.info(
_24
`${body.payload.attendees
_24
.map((a) => a.name)
_24
.join(", ")} cancelled their meeting ${new Date(
_24
body.payload.startTime
_24
)}`
_24
);
_24
},
_24
});

See our HTTP endpoint docs for more info. Upgrade to the latest version of the SDK to start using this feature.

How to update

The Trigger.dev Cloud is now running v2.2.4. If you are self-hosting you can upgrade by pinning to the v2.2.4 tag.

The trigger.dev/* packages are now at v2.2.5. You can update using the following command:


_10
npx @trigger.dev/cli@latest update

Ready to start building?

Build and deploy your first task in 3 minutes.

Get started now
,