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 { TriggerClient, eventTrigger } from "@trigger.dev/sdk";
2
import { OpenAI } from "@trigger.dev/openai";
3
import { z } from "zod";
4
5
const openai = new OpenAI({
6
id: "openai",
7
apiKey: process.env.OPENAI_API_KEY!,
8
});
9
10
// This Job will use OpenAI GPT-3.5 Turbo to tell you a joke
11
client.defineJob({
12
id: "openai-tell-me-a-joke",
13
name: "OpenAI: tell me a joke",
14
version: "1.0.0",
15
trigger: eventTrigger({
16
name: "openai.tasks",
17
schema: z.object({
18
jokePrompt: z.string(),
19
}),
20
}),
21
integrations: {
22
openai,
23
},
24
run: async (payload, io, ctx) => {
25
await io.openai.retrieveModel("get-model", {
26
model: "gpt-3.5-turbo",
27
});
28
29
const models = await io.openai.listModels("list-models");
30
31
const jokeResult = await io.openai.backgroundCreateChatCompletion(
32
"background-chat-completion",
33
{
34
model: "gpt-3.5-turbo",
35
messages: [
36
{
37
role: "user",
38
content: payload.jokePrompt,
39
},
40
],
41
}
42
);
43
44
return {
45
joke: jokeResult.choices[0]?.message?.content,
46
};
47
},
48
});