Intermediate

OpenAI Images API

Integrate DALL-E image generation into your applications. Learn the generations, edits, and variations endpoints with Python and Node.js code.

Setup

Installation
# Python
pip install openai

# Node.js
npm install openai

# Set your API key
export OPENAI_API_KEY="sk-your-key-here"

Image Generation (Python)

Python
from openai import OpenAI
import requests

client = OpenAI()

# Generate an image
response = client.images.generate(
    model="dall-e-3",
    prompt="A serene Japanese garden with a red bridge over a koi pond, cherry blossoms falling, watercolor painting style",
    size="1024x1024",       # 1024x1024, 1024x1792, 1792x1024
    quality="hd",            # "standard" or "hd"
    n=1,                     # DALL-E 3 only supports n=1
    style="vivid",           # "vivid" or "natural"
)

# Get the image URL
image_url = response.data[0].url

# Get the revised prompt (DALL-E 3 rewrites your prompt)
revised_prompt = response.data[0].revised_prompt
print(f"Revised prompt: {revised_prompt}")

# Download the image
img_data = requests.get(image_url).content
with open("output.png", "wb") as f:
    f.write(img_data)

Image Generation (Node.js)

JavaScript
import OpenAI from "openai";

const openai = new OpenAI();

async function generateImage() {
  const response = await openai.images.generate({
    model: "dall-e-3",
    prompt: "A cozy bookstore with warm lighting",
    size: "1024x1024",
    quality: "standard",
    n: 1,
  });

  console.log(response.data[0].url);
  console.log(response.data[0].revised_prompt);
}

generateImage();

Image Edits (Inpainting)

Python
# Edit/inpaint an existing image (DALL-E 2 only)
response = client.images.edit(
    model="dall-e-2",
    image=open("original.png", "rb"),
    mask=open("mask.png", "rb"),  # Transparent areas get edited
    prompt="A cute tabby cat sitting on the couch",
    n=1,
    size="1024x1024",
)

Image Variations

Python
# Create variations of an image (DALL-E 2 only)
response = client.images.create_variation(
    model="dall-e-2",
    image=open("original.png", "rb"),
    n=3,
    size="1024x1024",
)

for i, img in enumerate(response.data):
    print(f"Variation {i+1}: {img.url}")

API Parameters Reference

Parameters
model:    "dall-e-3" or "dall-e-2"
prompt:   Text description (max 4000 chars for DALL-E 3)
size:     "1024x1024", "1024x1792", "1792x1024" (DALL-E 3)
           "256x256", "512x512", "1024x1024" (DALL-E 2)
quality:  "standard" ($0.040) or "hd" ($0.080) per image
style:    "vivid" (default, more dramatic) or "natural" (realistic)
n:        Number of images (1 for DALL-E 3, 1-10 for DALL-E 2)

Response format:
  response_format: "url" (default) or "b64_json"
Note: Image edits and variations are currently only available with DALL-E 2 through the API. DALL-E 3 supports only the generation endpoint. For editing with DALL-E 3, use ChatGPT's conversational interface.

What's Next?

The next lesson covers image editing techniques including inpainting, outpainting, style transfer, and creating variations.