Code beginner · 3 min read

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

  1. Install the OpenAI Python SDK and set your API key in the environment variable OPENAI_API_KEY.
  2. Import the OpenAI client and initialize it with your API key from os.environ.
  3. Call the images.generate() method with model='dall-e-3' and provide a descriptive prompt.
  4. Specify the number of images and size (e.g., 1024x1024) as needed.
  5. Extract the image URL from the response's data array.
  6. Use or display the returned image URL in your application.

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

Variants

Streaming image generation (for progressive loading)

Use streaming when you want to progressively receive image data or URLs for faster UX.

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

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

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",
    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.
ApproachLatencyCost/callBest for
Standard generate()~1.5-3s~$0.02/imageSimple image generation
Streaming generate()Faster perceived~$0.02/imageProgressive loading UX
Async generate()~1.5-3s~$0.02/imageAsync 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.

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