Back to APIs

Create and manage community interactions with bots and webhooks.

Using the Discord API with Trigger.dev

You can use Trigger.dev with any existing Node SDK or even just using fetch. Using io.runTask makes your Discord 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 Discord

Below are some working code examples of how you can use Discord 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 nacl from "tweetnacl";
2
import { TriggerClient } from "@trigger.dev/sdk";
3
4
const verifyRequestSignature = async (request: Request): Promise<any[]> => {
5
const body = await request.text();
6
const jsonBody = JSON.parse(body);
7
const signature = request.headers.get("x-signature-ed25519");
8
const timestamp = request.headers.get("x-signature-timestamp");
9
const discordKey = process.env.DISCORD_APPLICATION_KEY;
10
if (!discordKey || !signature || !timestamp) return [false, jsonBody];
11
return [
12
nacl.sign.detached.verify(
13
Buffer.from(timestamp + body),
14
Buffer.from(signature, "hex"),
15
Buffer.from(discordKey, "hex")
16
),
17
jsonBody,
18
];
19
};
20
21
const discord = client.defineHttpEndpoint({
22
id: "discord",
23
source: "discord.com",
24
icon: "discord",
25
// This is only needed for APIs like Discord which don't setup the webhook until you pass the test
26
respondWith: {
27
// Don't trigger runs if they match this filter
28
skipTriggeringRuns: true,
29
filter: {
30
method: ["POST"],
31
},
32
handler: async (request) => {
33
const success = await verifyRequestSignature(request);
34
// If Discord signature and timestamp don't match, return 401
35
if (!success[0]) return new Response("Unauthorized", { status: 401 });
36
// If successful, get the Interaction Type sent by Discord of the request
37
const { type } = success[1];
38
// If it's type 1, it's a PING from discord, just respond with the type as is
39
if (Number(type) === 1)
40
return new Response(JSON.stringify({ type }), {
41
headers: { "Content-Type": "application/json" },
42
});
43
// If it's type 2, it's a Slash Command from Discord, respond with what you want to be replied
44
if (Number(type) === 2)
45
return new Response(
46
JSON.stringify({
47
type: 4,
48
data: {
49
content: `Hello, New!`,
50
},
51
}),
52
{ headers: { "Content-Type": "application/json" } }
53
);
54
// If not either of the above types, just return a 400
55
return new Response(JSON.stringify({ error: "bad request" }), {
56
status: 400,
57
});
58
},
59
},
60
verify: async (request) => {
61
const success = await verifyRequestSignature(request);
62
if (success[0]) return { success: success[0] };
63
return { success: false, reason: "Failed ed25519 verification" };
64
},
65
});
66
67
// A job that runs when the HTTP endpoint is called from Discord
68
client.defineJob({
69
id: "http-discord",
70
name: "HTTP Discord",
71
version: "1.0.0",
72
enabled: true,
73
// Create a trigger from the HTTP endpoint
74
trigger: discord.onRequest(),
75
run: async (request, io, ctx) => {
76
const body = await request.json();
77
await io.logger.info(`Body`, body);
78
},
79
});