gotchatypescriptMajor
DALL-E and Stable Diffusion APIs return URLs that expire — download immediately
Viewed 0 times
openai@4.x
dall-eimage-generationurl-expirystorageb64_jsondownload
Problem
OpenAI's image generation API returns temporary URLs to generated images. These URLs expire after 1 hour. Storing the URL in a database and displaying it later results in broken images.
Solution
Download the image binary immediately after generation and upload it to durable storage (S3, Cloudflare R2, Supabase Storage). Store your own permanent URL. Do not store the OpenAI-provided URL as the canonical reference.
Why
OpenAI generates images in temporary Azure Blob Storage with time-limited SAS tokens. After expiry the URL returns 404. Your users expect permanent image persistence.
Gotchas
- For b64_json response format, the image is base64 encoded in the response body — no URL needed, decode directly
- DALL-E 3 supports 1024x1024, 1024x1792, 1792x1024 — other sizes cause 400 errors
- Revised prompts from DALL-E 3 are available in response.data[0].revised_prompt — useful for display
Code Snippets
Download and store DALL-E image immediately
const image = await openai.images.generate({ model: 'dall-e-3', prompt, n: 1, size: '1024x1024', response_format: 'b64_json' });
const base64 = image.data[0].b64_json!;
const buffer = Buffer.from(base64, 'base64');
await s3.putObject({ Bucket: 'my-bucket', Key: `images/${uuid()}.png`, Body: buffer, ContentType: 'image/png' }).promise();Context
Applications that generate and persistently display AI-generated images
Revisions (0)
No revisions yet.