Back to APIs

Automate generating text and images, fine-tune and more.

Using our official OpenAI integration

Easily subscribe to OpenAI webhooks to trigger your jobs.

Create any tasks possible with the OpenAI API.

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

OpenAI integration docs

Example code using OpenAI

Below are some working code examples of how you can use OpenAI 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 { SupabaseManagement, Supabase } from "@trigger.dev/supabase";
2
import { OpenAI } from "@trigger.dev/openai";
3
import { TriggerClient } from "@trigger.dev/sdk";
4
5
const openai = new OpenAI({
6
id: "open-ai",
7
apiKey: process.env.OPENAI_API_KEY!,
8
});
9
10
// Use Supabase integration to run authenticated tasks using the service_role key
11
const supabase = new Supabase({
12
id: "supabase",
13
supabaseKey: process.env.SUPABASE_SERVICE_ROLE_KEY!,
14
supabaseUrl: "https://<project id>.supabase.co",
15
});
16
17
const supabaseManagement = new SupabaseManagement({
18
id: "supabase-management",
19
});
20
21
// Pass the generated types to the db instance
22
const db = supabase.db<Database>("https://<project id>.supabase.co");
23
24
client.defineJob({
25
id: "ai-all-the-things",
26
name: "AI all the images",
27
version: "1.0.0",
28
// Subscribe to objects being inserted
29
trigger: db.onInserted({
30
schema: "storage",
31
table: "objects",
32
filter: {
33
record: {
34
bucket_id: ["uploads"],
35
name: [
36
{
37
$endsWith: ".png",
38
},
39
],
40
},
41
},
42
}),
43
// Define the integrations that this Job will use
44
integrations: {
45
supabase,
46
openai,
47
},
48
run: async (payload, io, ctx) => {
49
// Assuming the bucket is private, we create a signed url for temporary access
50
// Use the native supabase client to get a signed url
51
const { error, data } = await io.supabase.client.storage
52
.from("example_bucket")
53
.createSignedUrl(payload.record.name, 60);
54
55
if (error) {
56
throw error;
57
}
58
59
const imageVariation = await io.openai.createImageVariation(
60
"variation-image",
61
{
62
image: data.signedUrl,
63
n: 2,
64
response_format: "url",
65
size: "512x512",
66
}
67
);
68
69
// do something with the imageVariation response
70
},
71
});