typescriptbeginner
DALL·E 3 Image Generation
Generate images from a text prompt using the OpenAI DALL·E 3 API and return a URL.
typescriptPress ⌘/Ctrl + Shift + C to copy
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.
pythonbeginner
OpenAI DALL-E Image Generation
Generate and save images using the DALL-E 3 API with quality and style control.
Best for: AI image creation
#openai#dall-e
typescriptbeginner
Content Moderation with OpenAI
Check user input for harmful content using the OpenAI Moderation API before processing.
Best for: user input safety
#openai#moderation
typescriptintermediate
Next.js AI Streaming Route Handler
Stream OpenAI responses from a Next.js App Router route handler using the Vercel AI SDK.
Best for: AI chatbot backend
#nextjs#openai
pythonintermediate
OpenAI Structured Output with Pydantic
Force GPT to return validated JSON matching a Pydantic schema.
Best for: Review analysis
#ai#openai