How to use DALL-E 3 API in Python
Direct answer
Use the OpenAI Python SDK with client.images.generate() specifying model="dall-e-3" and a prompt to generate images via the DALL-E 3 API.
Setup
Install
pip install openai Env vars
OPENAI_API_KEY Imports
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"]) Examples
inA futuristic cityscape at sunset
outGenerated image URL: https://openai-generated-images.com/abc123.png
inA cute puppy wearing a superhero cape
outGenerated image URL: https://openai-generated-images.com/def456.png
inAn astronaut riding a horse on Mars
outGenerated image URL: https://openai-generated-images.com/ghi789.png
Integration steps
- Install the OpenAI Python SDK and set your API key in the environment variable OPENAI_API_KEY.
- Import the OpenAI client and initialize it with your API key from os.environ.
- Call the images.generate() method with model='dall-e-3' and provide a descriptive prompt.
- Specify the number of images and size (e.g., 1024x1024) as needed.
- Extract the image URL from the response's data array.
- Use or display the returned image URL in your application.
Full code
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
response = client.images.generate(
model="dall-e-3",
prompt="A futuristic cityscape at sunset",
n=1,
size="1024x1024"
)
image_url = response.data[0].url
print(f"Generated image URL: {image_url}") output
Generated image URL: https://openai-generated-images.com/abc123.png
API trace
Request
{"model": "dall-e-3", "prompt": "A futuristic cityscape at sunset", "n": 1, "size": "1024x1024"} Response
{"created": 1680000000, "data": [{"url": "https://openai-generated-images.com/abc123.png"}]} Extract
response.data[0].urlVariants
Streaming image generation (for progressive loading) ›
Use streaming when you want to progressively receive image data or URLs for faster UX.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
stream = client.images.generate(
model="dall-e-3",
prompt="A futuristic cityscape at sunset",
n=1,
size="1024x1024",
stream=True
)
for chunk in stream:
print(chunk.data[0].url, end='') Async image generation ›
Use async when integrating image generation into asynchronous applications or frameworks.
import os
import asyncio
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
async def generate_image():
response = await client.images.generate(
model="dall-e-3",
prompt="A futuristic cityscape at sunset",
n=1,
size="1024x1024"
)
print(f"Generated image URL: {response.data[0].url}")
asyncio.run(generate_image()) Generate multiple images at once ›
Use this to generate multiple variations of an image prompt in a single API call.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
response = client.images.generate(
model="dall-e-3",
prompt="A futuristic cityscape at sunset",
n=3,
size="512x512"
)
for i, img in enumerate(response.data):
print(f"Image {i+1} URL: {img.url}") Performance
Latency~1.5 to 3 seconds per image generation call depending on size and load
Cost~$0.02 per 1024x1024 image generated with DALL-E 3
Rate limitsDefault tier: 60 requests per minute, 1000 images per day (check OpenAI docs for updates)
- Keep prompts concise but descriptive to reduce token usage.
- Generate multiple images in one call (n>1) to optimize rate limits.
- Use smaller image sizes (e.g., 512x512) to reduce cost and latency.
| Approach | Latency | Cost/call | Best for |
|---|---|---|---|
| Standard generate() | ~1.5-3s | ~$0.02/image | Simple image generation |
| Streaming generate() | Faster perceived | ~$0.02/image | Progressive loading UX |
| Async generate() | ~1.5-3s | ~$0.02/image | Async app integration |
Quick tip
Always specify the image size explicitly (e.g., "1024x1024") to control output resolution and cost.
Common mistake
Forgetting to set the environment variable OPENAI_API_KEY or using deprecated SDK methods causes authentication or runtime errors.