DALL-E API Integration for Image Generation
DALL-E 3 from OpenAI is the most controllable model for business use cases: precisely follows text instructions, understands complex composite prompts, requires no GPU infrastructure. API is available through OpenAI SDK.
Core Features
from openai import AsyncOpenAI
import base64
import httpx
client = AsyncOpenAI()
async def generate(prompt: str, **kwargs) -> bytes:
response = await client.images.generate(
model="dall-e-3",
prompt=prompt,
size=kwargs.get("size", "1024x1024"), # 1024×1024, 1792×1024, 1024×1792
quality=kwargs.get("quality", "standard"), # standard ($0.04), hd ($0.08)
style=kwargs.get("style", "vivid"), # vivid / natural (realistic)
n=1, # DALL-E 3 supports only n=1
response_format="b64_json"
)
return base64.b64decode(response.data[0].b64_json)
async def edit_image(image_bytes: bytes, mask_bytes: bytes, prompt: str) -> bytes:
"""Edit existing image (inpainting)"""
response = await client.images.edit(
model="dall-e-2", # Edit API only for DALL-E 2
image=image_bytes,
mask=mask_bytes,
prompt=prompt,
size="1024x1024",
n=1,
response_format="b64_json"
)
return base64.b64decode(response.data[0].b64_json)
async def create_variation(image_bytes: bytes) -> bytes:
"""Create variations of existing image"""
response = await client.images.create_variation(
model="dall-e-2",
image=image_bytes,
n=1,
size="1024x1024",
response_format="b64_json"
)
return base64.b64decode(response.data[0].b64_json)
Prompt Engineering for DALL-E 3
DALL-E 3 rewrites the prompt internally (automatic enhancement). To track it:
response = await client.images.generate(
model="dall-e-3",
prompt=prompt,
# System instruction through "I NEED to..." for exact following
)
# Actually used prompt (after internal processing):
print(response.data[0].revised_prompt)
Effective templates:
# Commercial photography
"{product}, professional commercial photography, white background, studio lighting, 8k, product shot"
# Article illustration
"flat illustration of {concept}, modern minimal style, pastel colors, no text"
# Banner
"{subject}, banner composition, horizontal format, space for text on left side, professional"
FastAPI Integration
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class ImageRequest(BaseModel):
prompt: str
size: str = "1024x1024"
quality: str = "standard"
@app.post("/generate")
async def generate_image(request: ImageRequest):
image_bytes = await generate(request.prompt, size=request.size, quality=request.quality)
filename = f"{uuid.uuid4()}.png"
url = await upload_to_s3(filename, image_bytes)
return {"url": url, "filename": filename}
Cost: standard 1024×1024 — $0.040, HD — $0.080, 1792×1024 HD — $0.120. For 10,000 images per month — $400–800. For high loads, self-hosted FLUX or SDXL is more economical. Integration timeline — 1–3 days.







