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 supabaseManagement = new SupabaseManagement({
6
id: "supabase-management",
7
});
8
9
const openai = new OpenAI({
10
id: "open-ai",
11
apiKey: process.env.OPENAI_API_KEY!,
12
});
13
14
// Use Supabase integration to run authenticated tasks using the service_role key
15
const supabase = new Supabase({
16
id: "supabase",
17
supabaseKey: process.env.SUPABASE_SERVICE_ROLE_KEY!,
18
supabaseUrl: "https://<project id>.supabase.co",
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: "auto-generate-blog-title",
26
name: "Auto generate blog title",
27
version: "1.0.0",
28
// Subscribe to new blog posts being created
29
trigger: db.onInserted({
30
table: "blog_posts",
31
}),
32
// Define the integrations that this Job will use
33
integrations: {
34
supabase,
35
openai,
36
},
37
run: async (payload, io, ctx) => {
38
const result = await io.openai.backgroundCreateChatCompletion(
39
"create-blog-title",
40
{
41
model: "gpt-3.5-turbo",
42
messages: [
43
{
44
role: "user",
45
content: `Suggest some great titles for this blog post: \n ${payload.record.content}`,
46
},
47
],
48
}
49
);
50
51
const blogTitle = result.choices[0].message.content;
52
53
if (blogTitle) {
54
// Set the title for the blog post
55
const { data, error } = await io.supabase.runTask(
56
"update-blog-post",
57
async (db) => {
58
return db
59
.from("blog_posts")
60
.update({ title: blogTitle })
61
.eq("id", payload.record.id)
62
.select("*");
63
}
64
);
65
}
66
},
67
});