Back to APIs

Integrate and automate code security and scan for vulnerabilities.

Using the Snyk API with Trigger.dev

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

Below are some working code examples of how you can use Snyk 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 Snyk
5
// https://docs.snyk.io/snyk-api/authentication-for-api
6
const endpointURL = `${process.env.SNYK_BASE_URL}/user/me`;
7
const createOrgEndpontUrl = `${process.env.SNYK_BASE_URL}/org`;
8
9
// Create request options
10
const requestOptions: RequestInit = {
11
headers: {
12
"Content-Type": "application/json; charset=utf-8",
13
// You can find your token in your General Account Settings on
14
// https://snyk.io/account/. See Authentication for API details.
15
Authorization: `${process.env.SNYK_AUTH_TOKEN}`,
16
},
17
};
18
19
client.defineJob({
20
id: "snyk-get-my-user-details",
21
name: "Snyk get my user details",
22
version: "1.0.0",
23
trigger: eventTrigger({
24
name: "snyk-get-profile",
25
}),
26
run: async (payload, io, ctx) => {
27
// Wrap an SDK call in io.runTask so it's resumable and displays in logs
28
await io.runTask(
29
"Get Snyk me user details",
30
async () => {
31
const response = await fetch(endpointURL, {
32
method: "GET",
33
...requestOptions,
34
});
35
36
// Return the response body
37
const res = await response.json();
38
return res;
39
},
40
41
// Add metadata to improve how the task displays in the logs
42
{ name: "get snyk me user details", icon: "snyk" }
43
);
44
},
45
});
46
47
// Create organization in Snyk
48
49
// client.defineJob({
50
// id: "snyk-create-org",
51
// name: "Snyk create new organization",
52
// version: "1.0.0",
53
// trigger: eventTrigger({
54
// name: "snyk-add-org",
55
// schema: z.object({
56
// name: z.string(),
57
// groupId: z.string(),
58
// sourceOrgId: z.string(),
59
// }),
60
// }),
61
// run: async (payload, io, ctx) => {
62
// // Wrap an SDK call in io.runTask so it's resumable and displays in logs
63
// await io.runTask(
64
// "Create Organization",
65
// async () => {
66
// // Make request using Fetch API
67
// const response = await fetch(createOrgEndpontUrl, {
68
// method: "POST",
69
// ...requestOptions,
70
// body: JSON.stringify(payload),
71
// });
72
73
// // Return the response body
74
// const res = await response.json();
75
// return res;
76
// },
77
78
// // Add metadata to improve how the task displays in the logs
79
// { name: "create new organization in snyk", icon: "snyk" }
80
// );
81
// },
82
// });