When the Bookshop orders a promo illustration from a freelance illustrator, you send a brief (a written order): what to draw, the style and the size. The illustrator sends a file, and you pay per image. Then you keep that file in the shop's own archive, not in a folder the illustrator lends you, which may be gone tomorrow. An image generation API (an API that makes images) works the same way: an endpoint that takes a text description (a prompt) and returns an image file. In this picture, the brief is the prompt, the illustrator is the model on the provider's server, the illustrator's folder is a temporary URL from the provider, and the shop's archive is your own storage (a place where files are kept). The last step, keeping the file in your own archive, is the one people forget most.
One request to OpenAI's Image API
The Dancer has just arrived, and the admin needs a 1536 × 1024 pixel promo banner. The Bookshop server calls OpenAI:
import OpenAI from "openai";
const client = new OpenAI();
const result = await client.images.generate({
model: "gpt-image-2.5-flare", // fast, for everyday images
prompt: PROMO_PROMPT, // covered in the next section
size: "1536x1024", // landscape
quality: "medium", // low, medium, high, xhigh, max, auto
output_format: "webp",
moderation: "auto", // built-in filter, the default
});
const base64 = result.data[0].b64_json; // the image, as base64 text
const bytes = Buffer.from(base64, "base64"); // back into a binary file
- The endpoint is
POST https://api.openai.com/v1/images/generations. As of October 2026, OpenAI's "Image generation" guide recommendsgpt-image-2.5-flare(fast, for everyday images) orgpt-image-2.5-sunburst(the most precise for editing images). - GPT Image models always return base64 in
b64_json, never a URL. Base64 writes a file's bytes as letters and digits, so it is about 4/3 the size of the original file.Buffer.from(base64, "base64")turns it back into the file's bytes. - The standard
sizevalues are1024x1024,1536x1024and1024x1536. Other sizes are allowed, with four conditions: both sides are multiples of 16, the ratio is between 1:3 and 3:1, the longest side is at most 3840 pixels, and the pixel count is between 655,360 and 8,294,400. - One image can take a while: the guide says complex prompts may take up to 2 minutes. So run it in a background job (work that runs separately on the server), not in a request the browser is waiting on.
DALL-E (DALL·E 2 and DALL·E 3), the name that used to be the best known, was OpenAI's previous generation of image models. According to OpenAI's "Deprecations" page, both were removed from the API on 12 May 2026 and replaced by GPT Image. The other popular name is Stable Diffusion from Stability AI. It has open weights, and it can also be called through an API. The second provider in this lesson is Black Forest Labs, the maker of the FLUX models.
Prompts for images
What decides the result most is how detailed and clear the prompt is, not the customer's internet connection or screen. The model can only use what you write. Compare these two prompts:
| Prompt | What the model guesses on its own |
|---|---|
| "a ronggeng dancer" | place, style, light, angle, empty space, whether there is text |
| all five parts (below) | almost nothing |
The full prompt is built from five parts (subject, setting, style, light, composition), plus exclusions. In the Lab, the subject is always there, and the other parts can be switched on one at a time:
A ronggeng dancer in a red shawl dancing on open ground (subject),
in a small Central Javanese hamlet with bamboo fences (setting),
a watercolour illustration in warm colours (style),
golden evening light from the side (light),
dancer on the left third, right side empty for a title (composition),
no text, letters or logos of any kind (exclusions).
- Composition (how the picture is arranged) leaves empty space on the right. The book title goes in that space with HTML and CSS, instead of being drawn by the model. The reason: OpenAI's own guide says the model can still struggle with precise text placement and clarity.
- Exclusions ("no text") prevent random letters that look like writing.
- Don't ask for an image that copies a real book's cover, a real person's face or a brand's logo. That is a copyright problem and can mislead customers.
Size, quality and price
The price of one image depends on its size and quality. Here are the prices, checked on 11 October 2026:
| Provider | Model | Price |
|---|---|---|
| OpenAI | gpt-image-2.5-flare / -sunburst |
$30 per 1 million image output tokens (OpenAI hasn't published a per-image price) |
| OpenAI | gpt-image-2 (same token rates, per-image prices published, deprecated) |
1536 × 1024: low $0.005, medium $0.041, high $0.165. 1024 × 1024: low $0.006, medium $0.053, high $0.211 |
| Black Forest Labs | FLUX 3 Image | per image: 768sq $0.041, 1k $0.048, 2k $0.100, 4k $0.607 |
The sources are OpenAI's "Pricing" and "Image generation" pages and Black Forest Labs' "Pricing" page. OpenAI publishes per-image prices only for gpt-image-2, which is deprecated and shuts down on 10 February 2027. Its replacements, gpt-image-2.6-sunburst and -flare, have the same token rates (checked 15 November 2026). So gpt-image-2 stays the yardstick for prices. For example, the admin makes 4 low drafts (4 × $0.005 = $0.02), then 1 final high version ($0.165): the total is $0.185. Going straight to 5 high images costs $0.825, more than 4 times as much. low is enough to pick an idea. Pay the high price only for the image you use.
Store the result, never hot-link a temporary URL
Black Forest Labs works asynchronously: the image isn't in the answer to the first request. The first request only returns an id and a polling_url. Then the server asks the polling_url again and again (this is called polling) until status becomes Ready. The image is at the URL in result.sample:
const submit = await fetch("https://api.bfl.ai/v1/flux-3-image", {
method: "POST",
headers: { "x-key": process.env.BFL_API_KEY, "Content-Type": "application/json" },
body: JSON.stringify({ prompt: PROMO_PROMPT, aspect_ratio: "3:2", resolution: "1k" }),
});
const { polling_url } = await submit.json();
let result;
do {
await new Promise((r) => setTimeout(r, 1500)); // wait 1.5 seconds
result = await (await fetch(polling_url, { headers: { "x-key": process.env.BFL_API_KEY } })).json();
} while (["Pending", "Reasoning", "Generating"].includes(result.status));
if (result.status !== "Ready") throw new Error(result.status); // for example "Content Moderated"
const image = Buffer.from(await (await fetch(result.result.sample)).arrayBuffer()); // download NOW
await storage.put("promo/book-4/2026-10-11.webp", image); // R2, S3 or the server's disk
Black Forest Labs' documentation says the URL in result.sample is valid for only 10 minutes, so download the image in the same job. If that URL goes straight into an <img> (this is called hot-linking), customers see a broken image a few minutes later. OpenAI doesn't even send a URL: if you don't store the base64, the image is gone as soon as the request is done.
So always take these three steps:
- Download the image (Black Forest Labs), or decode the base64 into a file (OpenAI).
- Store that file in your own storage.
- Record it in the database: book id, file path, prompt, model, time and alt text.
The promo page uses your own storage URL. And the image is made once, not on every page view: 1,000 visits × $0.041 = $41, while making it once costs $0.041.
Moderation and content policy
Providers check the prompt and the resulting image, and block what breaks their content policy. This is called moderation. At OpenAI, the moderation field is auto (the default, standard filtering) or low (less restrictive). A blocked request fails with the error.code "moderation_blocked". moderation_details.moderation_stage says which part was blocked: input (the prompt) or output (the image). Black Forest Labs has a safety_tolerance field from 0 to 7 (0 is the strictest, 2 the default), and the statuses Request Moderated and Content Moderated. Don't retry a blocked request without changing the prompt.
The provider's filter isn't the only guard. Write the shop's own rules (no real people's faces, original covers or brand logos), and let the admin see the image before customers do.
Alt text for AI-made images
An image on a page needs alt text: the sentence a screen reader reads out, which is also shown when the image fails to load. The prompt is not the alt text, because the model may draw something different from what was asked. Write the alt text from the image that actually came out, after looking at it:
<img src="https://cdn.bookshop.test/promo/book-4/2026-10-11.webp"
alt="Watercolour illustration: a ronggeng dancer in a red shawl dancing in a hamlet at dusk"
width="1536" height="1024">
The book title is already in the HTML next to the image, so the alt doesn't repeat it. An image that is only decoration uses alt="" (an empty alt).
⚠️ Broken banners, garbled letters and a swelling bill
- Hot-linking the provider's URL.
result.sampleexpires after 10 minutes, and OpenAI only sends base64. Download it and keep it in your own storage. - Asking the model to write the title in the image. The letters can come out wrong or garbled. Ask for empty space in the prompt and place the title with HTML.
- Generating the image again on every visit. Make it once, store it, then reuse it.
- Publishing without a human look. The provider's filter doesn't know your shop's rules. The admin checks the image and writes the alt text before it goes live.
TL;DR: An image generation API (an API that makes images) takes a prompt and returns an image. At OpenAI (October 2026): POST /v1/images/generations with gpt-image-2.5-flare or -sunburst, size (for example 1536x1024) and quality (low to max). The result is always base64 (the image's bytes written as text) in b64_json. DALL-E was removed from the API on 12 May 2026. Stable Diffusion and FLUX (from Black Forest Labs) are the other well-known names. What decides the result most is a detailed, clear prompt: subject, setting, style, light, composition and exclusions. The book title is added with HTML, instead of being drawn by the model. Per-image prices: gpt-image-2 at 1536 × 1024, medium quality, $0.041 as the yardstick, and FLUX 3 Image 1k $0.048. Draft at low first. Black Forest Labs' result.sample URL (the address of its image) stops working after 10 minutes, so always download the image and store it in your own storage (a place where files are kept), once. Respect moderation (the provider's content checks: moderation_blocked, Content Moderated), check the image before it goes live, and write the alt text from the finished image, not from the prompt.
⚡ A prompt in, an image out
Write:
const result = await client.images.generate({
model: "gpt-image-2.5-flare", // fast, for everyday images
prompt: PROMO_PROMPT, // covered in the next section
size: "1536x1024", // landscape
quality: "medium", // low, medium, high, xhigh, max, auto
output_format: "webp",
moderation: "auto", // built-in filter, the default
});
const base64 = result.data[0].b64_json; // the image, as base64 text
Meaning: The server asks OpenAI's Image API to make an image from the prompt PROMO_PROMPT (a description of the promo banner). GPT Image models always return base64 (the image's bytes written as text) in b64_json, never a URL. DALL-E, OpenAI's previous generation of image models, has been removed from the API. Other well-known names: Stable Diffusion, and FLUX from the provider Black Forest Labs.
⚡ The prompt decides
Guess first: what decides most whether the image is good, the size, the quality, or the prompt?
How detailed and clear the prompt is. The model can only use what you write: subject, setting, style, light, composition and exclusions (for example "no text").
Meaning: Use the composition (how the picture is arranged) to ask for empty space in the image, then place the book title in that space with HTML. The reason: letters the model draws can come out wrong or garbled.
⚡ Store it yourself, once
Meaning: Black Forest Labs sends the image as a URL in result.sample, and that URL stops working after 10 minutes. OpenAI only sends base64. So download the image (or decode the base64 into a file), store it in your own storage (a place where files are kept), then record it in the database. The image is made once, not on every page view.
⚡ Alt text from the finished image
Meaning: Don't copy the alt text (the text a screen reader reads out instead of the image) from the prompt, because the model may draw something else. Look at the image first, then write its alt text. The admin checks the image before it goes live.