Back to APIs

Send, retrieve and delete emails, manage domains, and more.

Using the Mailgun API with Trigger.dev

You can use Trigger.dev with any existing Node SDK or even just using fetch. Using io.runTask makes your Mailgun background job resumable and appear in our dashboard.

Use io.runTask() and the official SDK or fetch.

Use our HTTP endpoint to subscribe to webhooks

Example code using Mailgun

Below are some working code examples of how you can use Mailgun with Trigger.dev. These samples are open source and maintained by the community, you can copy and paste them into your own projects.

1
import { TriggerClient } from "@trigger.dev/sdk";
2
import { createHmac } from "crypto";
3
4
// Go to your normal Mailgun account
5
// Create a private app in Sending > Webhooks
6
// With scope: Delivered
7
// And add your trigger webhooks url in target url
8
// Obtain the Webhook Key on the right of your window
9
10
// Create an HTTP Endpoint, with the Mailgun details
11
const mailgun = client.defineHttpEndpoint({
12
id: "mailgun",
13
source: "mailgun.com",
14
icon: "mailgun",
15
verify: async (request) => {
16
const body = await request.json();
17
const { timestamp, token, signature } = body.signature;
18
const mailgunKey = process.env.MAILGUN_WEBHOOK_SIGNING_KEY;
19
if (!mailgunKey)
20
return { success: false, reason: "Missing mailgun webhook signing key" };
21
if (!timestamp || !token)
22
return {
23
success: false,
24
reason: "Missing signature fields in request body",
25
};
26
const hash = createHmac("sha256", mailgunKey)
27
.update(timestamp + token)
28
.digest("hex");
29
const success = hash === signature;
30
if (success) return { success };
31
return { success: false, reason: "Failed sha256 verification" };
32
},
33
});
34
35
// Job that runs when the HTTP endpoint is called from Mailgun when an email is sent
36
client.defineJob({
37
id: "http-mailgun",
38
name: "HTTP Mailgun",
39
version: "1.0.0",
40
enabled: true,
41
// Create a trigger from the HTTP endpoint
42
trigger: mailgun.onRequest(),
43
run: async (request, io, ctx) => {
44
const body = await request.json();
45
await io.logger.info(`Body`, body);
46
},
47
});