Back to APIs

Automate posting media and manage social interactions.

Using the Instagram API with Trigger.dev

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

Below are some working code examples of how you can use Instagram 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, verifyRequestSignature } from "@trigger.dev/sdk";
2
3
// Create an HTTP endpoint to listen to Instagram webhooks
4
// (This will create the endpoint URL on the `trigger.dev` dashboard)
5
// Create a Meta developer account (if you don't have one).
6
// Create an app on the Meta developer platform.
7
// Copy the `App Secret` from Dashboard > App Settings > Basic.
8
// Go to Dashboard > Webhooks and setup the subscriptions.
9
// Provide the Webhook URL and Verification Token (random string).
10
// Set the `INSTAGRAM_WEBHOOK_SIGNING_SECRET` (App Secret) and `INSTAGRAM_VERIFICATION_TOKEN` (Verification Token) in the .env file.
11
const instagram = client.defineHttpEndpoint({
12
id: "instagram",
13
source: "instagram.com",
14
icon: "instagram",
15
// This is only needed for certain APIs like Instagram which don't set up the webhook until you pass the test
16
respondWith: {
17
// Don't trigger runs if they match this filter
18
skipTriggeringRuns: true,
19
filter: {
20
method: ["GET"],
21
query: {
22
"hub.mode": [{ $ignoreCaseEquals: "subscribe" }],
23
},
24
},
25
// Handler for the webhook setup test
26
handler: async (request, verify) => {
27
const searchParams = new URL(request.url).searchParams;
28
if (
29
searchParams.get("hub.verify_token") !==
30
process.env.INSTAGRAM_VERIFICATION_TOKEN
31
) {
32
return new Response("Unauthorized", { status: 401 });
33
}
34
return new Response(searchParams.get("hub.challenge") ?? "OK", {
35
status: 200,
36
});
37
},
38
},
39
verify: async (request) => {
40
return await verifyRequestSignature({
41
request,
42
headerName: "x-hub-signature-256",
43
secret: process.env.INSTAGRAM_WEBHOOK_SIGNING_SECRET!,
44
algorithm: "sha256",
45
});
46
},
47
});
48
49
client.defineJob({
50
id: "http-instagram",
51
name: "HTTP Instagram",
52
version: "1.0.0",
53
enabled: true,
54
trigger: instagram.onRequest(),
55
run: async (request, io, ctx) => {
56
const body = await request.json();
57
await io.logger.info(`Body`, body);
58
},
59
});