Code beginner · 3 min read

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
bash
pip install openai
Env vars
OPENAI_API_KEY
Imports
python
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

  1. Install the OpenAI Python SDK and set your API key in the environment variable OPENAI_API_KEY.
  2. Import os and OpenAI from the SDK.
  3. Initialize the client with OpenAI(api_key=os.environ["OPENAI_API_KEY"]).
  4. Call client.images.generate() with the prompt, model (e.g., "dall-e-3"), and image size.
  5. Extract the image URL from the response's data[0].url field.
  6. Use or display the returned image URL as needed.

Full code

python
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
json
{"model": "dall-e-3", "prompt": "A futuristic cityscape at sunset, digital art", "size": "1024x1024"}
Response
json
{"created": 1680000000, "data": [{"url": "https://openai-generated-images.s3.amazonaws.com/abc123def456.png"}]}
Extractresponse.data[0].url

Variants

Streaming Image Generation ›

Use when the API supports streaming image generation to improve UX with progressive image loading.

python
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.

python
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.

python
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.
ApproachLatencyCost/callBest for
Standard sync call~1-3s~$0.02 per 1024x1024 imageSimple scripts and quick tests
Async callsConcurrent ~1-3s per call~$0.02 per 1024x1024 imageHigh throughput or web apps
Smaller image size (256x256)~0.5-1s~$0.005 per imageLow-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.

Verified 2026-04 · dall-e-3
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.