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, eventTrigger } from "@trigger.dev/sdk";
2
import z from "zod";
3
import * as PublicAPI from "@segment/public-api-sdk-typescript";
4
5
// Guide to create a segment public api: https://segment.com/docs/api/public-api/
6
// You need to upgrade your account to a team account or business to get access to the public API.
7
const publicAPI = PublicAPI.configureApis(
8
process.env.SEGMENT_PUBLIC_API_KEY!
9
);
10
11
client.defineJob({
12
id: "segment-get-source",
13
name: "Segment Get Source",
14
version: "1.0.0",
15
trigger: eventTrigger({
16
name: "segment-get-source",
17
schema: z.object({
18
sourceId: z.string(),
19
}),
20
}),
21
run: async (payload, io, ctx) => {
22
const { sourceId } = payload;
23
24
// Wrap an SDK call in io.runTask so it's resumable and displays in logs
25
await io.runTask(
26
"segment get source",
27
async () => {
28
const source = await publicAPI.sources.getSource(sourceId);
29
return JSON.parse(JSON.stringify(source));
30
},
31
32
// Add metadata to improve how the task displays in the logs
33
{ name: "segment get source", icon: "segment" }
34
);
35
},
36
});