How to use DALL-E API in python
Direct answer
Use the OpenAI Python SDK's
client.images.generate() method with your API key from os.environ to create images via the DALL-E API.Setup
Install
pip install openai Env vars
OPENAI_API_KEY Imports
import os
from openai import OpenAI Examples
inGenerate a 512x512 image of a futuristic cityscape at sunset.
outURL to the generated 512x512 futuristic cityscape image.
inCreate a 1024x1024 image of a cute robot holding a flower.
outURL to the generated 1024x1024 image of a cute robot holding a flower.
inGenerate a 256x256 image of an astronaut riding a horse on Mars.
outURL to the generated 256x256 image of an astronaut riding a horse on Mars.
Integration steps
- Install the OpenAI Python SDK and set your API key in the environment variable OPENAI_API_KEY.
- Import
osandOpenAIfrom the SDK. - Initialize the client with
OpenAI(api_key=os.environ["OPENAI_API_KEY"]). - Call
client.images.generate()with the prompt, model (e.g., "dall-e-3"), and image size. - Extract the image URL from the response's
data[0].urlfield. - Use or display the returned image URL as needed.
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, digital art",
size="1024x1024"
)
image_url = response.data[0].url
print("Generated image URL:", image_url) output
Generated image URL: https://openai-generated-images.s3.amazonaws.com/abc123def456.png
API trace
Request
{"model": "dall-e-3", "prompt": "A futuristic cityscape at sunset, digital art", "size": "1024x1024"} Response
{"created": 1680000000, "data": [{"url": "https://openai-generated-images.s3.amazonaws.com/abc123def456.png"}]} Extract
response.data[0].urlVariants
Streaming Image Generation ›
Use when the API supports streaming image generation to improve UX with progressive image loading.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# Note: DALL-E image generation currently does not support streaming responses in the SDK.
# This is a placeholder for future streaming support if added. Async Image Generation ›
Use async version for concurrent image generation calls in an async Python application.
import os
import asyncio
from openai import OpenAI
async def generate_image():
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
response = await client.images.generate(
model="dall-e-3",
prompt="A cute robot holding a flower",
size="512x512"
)
print("Generated image URL:", response.data[0].url)
asyncio.run(generate_image()) Alternative Model for Smaller Images ›
Use smaller image sizes to reduce latency and cost when high resolution is not required.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
response = client.images.generate(
model="dall-e-3",
prompt="An astronaut riding a horse on Mars",
size="256x256"
)
print("Generated image URL:", response.data[0].url) Performance
Latency~1-3 seconds per image generation depending on size
Cost~$0.02 per 1024x1024 image generated with DALL-E 3
Rate limitsDefault tier: 60 requests per minute, 1000 images per day
- Keep prompts concise to reduce processing time.
- Use smaller image sizes when high resolution is unnecessary.
- Batch requests asynchronously to optimize throughput.
| Approach | Latency | Cost/call | Best for |
|---|---|---|---|
| Standard sync call | ~1-3s | ~$0.02 per 1024x1024 image | Simple scripts and quick tests |
| Async calls | Concurrent ~1-3s per call | ~$0.02 per 1024x1024 image | High throughput or web apps |
| Smaller image size (256x256) | ~0.5-1s | ~$0.005 per image | Low-cost, fast previews |
Quick tip
Always specify the image size explicitly in <code>client.images.generate()</code> to control output resolution and cost.
Common mistake
Forgetting to set the <code>OPENAI_API_KEY</code> environment variable or using deprecated SDK methods causes authentication or runtime errors.