typescriptbeginner

DALL·E 3 Image Generation

Generate images from a text prompt using the OpenAI DALL·E 3 API and return a URL.

typescript
import OpenAI from "openai";

const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

export async function generateImage(prompt: string): Promise<string> {
  const res = await client.images.generate({
    model: "dall-e-3",
    prompt,
    n: 1,
    size: "1024x1024",
    quality: "standard",
    response_format: "url",
  });

  const url = res.data[0]?.url;
  if (!url) throw new Error("No image returned");
  return url;
}

const url = await generateImage("A futuristic dashboard with neon blue charts on dark background");
console.log(url);

Use Cases

  • AI art generation
  • dynamic OG images
  • product mockups

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.