Back to APIs

Send transactional emails, manage contacts and get custom lists.

Using the Loops API with Trigger.dev

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

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

Example code using Loops

Below is a working code example of how you can use Loops 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
4
// Replace this URL with the actual API endpoint you want to call in Loops
5
const endpointURL = `${process.env.LOOPS_BASE_URL}/contacts/create`;
6
7
// Create request options
8
const requestOptions: RequestInit = {
9
method: "POST",
10
headers: {
11
"Content-Type": "application/json",
12
// To create an API key for Loops, go to the API settings in your account,
13
// generate a new key and set permissions: https://loops.so/docs/api
14
Authorization: `Bearer ${process.env.LOOPS_API_KEY}`,
15
},
16
};
17
18
client.defineJob({
19
id: "loops-create-contract",
20
name: "Loops create contract",
21
version: "1.0.0",
22
trigger: eventTrigger({
23
name: "loops-create-contract",
24
schema: z.object({
25
email: z.string(),
26
firstName: z.string(),
27
lastName: z.string(),
28
favoriteColor: z.string(),
29
userGroup: z.string(),
30
source: z.string(),
31
}),
32
}),
33
run: async (payload, io, ctx) => {
34
// Wrap an SDK call in io.runTask so it's resumable and displays in logs
35
await io.runTask(
36
"loops create contact",
37
async () => {
38
const response = await fetch(endpointURL, {
39
...requestOptions,
40
body: JSON.stringify(payload),
41
});
42
43
return await response.json();
44
},
45
46
// Add metadata to improve how the task displays in the logs
47
{ name: "loops create contract", icon: "loops" }
48
);
49
},
50
});