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.
typescriptbeginner
Content Moderation with OpenAI
Check user input for harmful content using the OpenAI Moderation API before processing.
#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.
#nextjs#openai
typescriptintermediate
OpenAI Chat Completion with Streaming
Stream GPT responses token-by-token using the OpenAI SDK with async iteration.
#openai#streaming
typescriptbeginner
Generate Text Embeddings with OpenAI
Create vector embeddings for semantic search and similarity matching using text-embedding-3-small.
#openai#embeddings