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 generate an image from a prompt, using OpenAI's image API
11
client.defineJob({
12
id: "openai-generate-image",
13
name: "OpenAI: generate image from a prompt",
14
version: "1.0.0",
15
trigger: eventTrigger({
16
name: "openai.images",
17
schema: z.object({}),
18
}),
19
integrations: {
20
openai,
21
},
22
run: async (payload, io, ctx) => {
23
const imageResults = await io.openai.createImage("image", {
24
prompt: "A hedgehog wearing a party hat",
25
n: 2,
26
size: "256x256",
27
response_format: "url",
28
});
29
30
return {
31
images: imageResults.data?.map((image) => image.url),
32
};
33
},
34
});