Back to APIs

Upload videos, manage your playlists and subscribers.

Using the YouTube API with Trigger.dev

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

Below are some working code examples of how you can use YouTube 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 { google } from "googleapis";
3
import z from "zod";
4
5
// Get your API key here: https://console.developers.google.com/apis/credentials
6
// Make sure to enable the YouTube Data API v3 https://console.cloud.google.com/apis/library/youtube.googleapis.com
7
// SDK Docs: https://developers.google.com/youtube/v3/quickstart/nodejs
8
// Initialize the YouTube API with your API key
9
const youtube = google.youtube({
10
version: "v3",
11
auth: process.env.YOUTUBE_API_KEY,
12
});
13
14
client.defineJob({
15
id: "youtube-api-search",
16
name: "YouTube API Search",
17
version: "1.0.0",
18
trigger: eventTrigger({
19
name: "youtube-search",
20
schema: z.object({
21
q: z.string(),
22
maxResults: z.number().optional(),
23
}),
24
}),
25
run: async (payload, io, ctx) => {
26
const { q, maxResults } = payload;
27
28
// Wrap an SDK call in io.runTask so it's resumable and displays in logs
29
await io.runTask(
30
"YouTube API Search",
31
async () => {
32
const searchResponse = await youtube.search.list({
33
q,
34
maxResults: maxResults || 10, // Default to 10 results if not provided
35
part: ["snippet"],
36
type: ["video"],
37
});
38
39
// Process the search results here
40
return JSON.parse(JSON.stringify(searchResponse.data));
41
},
42
43
// Add metadata to improve how the task displays in the logs
44
{ name: "YouTube API Search", icon: "youtube" }
45
);
46
},
47
});