Lesson 2 of 3 · AI Image & Video Creation
Generating Images
Let us get practical. The Images API is your primary tool for generating images programmatically. Whether you are building a product feature, automating content creation, or prototyping designs, this endpoint is where you start.
The Basic API Call
That is it. One API call, one image. But the parameters you choose dramatically affect both quality and cost.
Parameters Deep Dive
Aspect Ratios and Resolutions
Choosing the right aspect ratio is not just about dimensions -- it affects composition. The model adjusts its framing based on the aspect ratio you request.
| Use Case | Recommended Size | Aspect Ratio |
|---|---|---|
| Social media post (Instagram) | 1024x1024 | 1:1 |
| Social media story | 1024x1536 | 9:16 (vertical) |
| Blog header / banner | 1536x1024 | 3:2 (landscape) |
| YouTube thumbnail | 1792x1024 | 16:9 (wide) |
| Product image | 1024x1024 | 1:1 |
| Hero section | 1792x1024 | 16:9 (wide) |
The maximum output resolution is constrained by the model. If you need images larger than the model's maximum output (currently around 1792x1024), you will need to upscale in post-processing. Tools like Real-ESRGAN or commercial upscalers can increase resolution while preserving quality.
Best Prompting Techniques
The difference between a mediocre image and a stunning one is almost always the prompt. Here is the structure that consistently produces good results:
Mediocre prompt: "A dog in a park"
Better prompt: "A golden retriever playing fetch in a sunlit autumn park, leaves falling around it, warm golden hour lighting, shallow depth of field, photorealistic, shot from a low angle"
The Five Prompt Dimensions
- Subject: What is the main focus? Be specific about appearance, pose, and expression.
- Setting: Where is this happening? Indoor/outdoor, specific location, background elements.
- Style: Photorealistic, illustration, watercolor, 3D render, flat design, anime, editorial.
- Lighting: Natural light, studio lighting, golden hour, neon, dramatic shadows, soft diffused.
- Technical: Camera angle, depth of field, lens type, aspect ratio considerations.
Front-load the most important elements of your prompt. The model pays more attention to the beginning. If the text on a sign is critical, mention it early: 'A storefront sign reading Morning Brew, cozy coffee shop exterior, warm lighting.'
Bury important details at the end of a long prompt. In a 200-word prompt, the model may give less weight to instructions at the end. Keep prompts focused -- 2-4 sentences is usually optimal.
Handling the Response
The API returns either a URL (temporary, expires in 60 minutes) or base64-encoded image data. For production use, always download and store the image immediately.
Image URLs returned by the API are temporary. They expire in approximately 60 minutes. Always download and store images immediately after generation. If you are building a user-facing application, save to your own storage (S3, Vercel Blob, Cloudflare R2) before serving to users. Never pass temporary OpenAI URLs directly to end users.
Batch Generation Pattern
For generating multiple images efficiently:
Write 3 image generation prompts for a boutique hotel's marketing campaign, each targeting a different platform: (1) An Instagram square post showcasing the hotel pool. (2) A website hero banner (wide landscape) for the homepage. (3) A Pinterest pin (vertical) featuring the hotel restaurant. For each prompt, include all five prompt dimensions (subject, setting, style, lighting, technical) and specify the appropriate size parameter.
Use ← → to navigate, Space to mark complete