Back to APIs

Integrate data collection and management for user analytics.

Using the Segment API with Trigger.dev

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

Below are some working code examples of how you can use Segment 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 crypto from "crypto";
3
4
// Create an HTTP Endpoint, with the Segment details
5
const segment = client.defineHttpEndpoint({
6
id: "segment",
7
source: "segment.com",
8
icon: "segment",
9
verify: async (request) => {
10
const signature = request.headers.get("X-Signature");
11
12
if (!signature) {
13
return { success: false, reason: "Missing header" };
14
}
15
16
const body = await request.text();
17
const bodyDigest = crypto
18
.createHmac("sha1", process.env.SEGMENT_WEBHOOK_SIGNING_SECRET!)
19
.update(body)
20
.digest("hex");
21
22
if (signature !== bodyDigest) {
23
return { success: false, reason: "Failed sha1 verification" };
24
}
25
26
return { success: true };
27
},
28
});
29
30
client.defineJob({
31
id: "http-segment",
32
name: "HTTP Segment",
33
version: "1.0.0",
34
enabled: true,
35
// Create a trigger from the HTTP endpoint
36
trigger: segment.onRequest(),
37
run: async (request, io, ctx) => {
38
const body = await request.json();
39
await io.logger.info(`Body`, body);
40
},
41
});