Prompt
How do I turn a text prompt into an image with an API?
Latest observation
Oct 11, 2026
OpenAI APIWeb search: off
To turn a text prompt into an image with an API, you generally:
- Send a prompt like
"a red panda wearing sunglasses on a beach" - Call an image-generation endpoint
- Receive the generated image either as:
- a URL, or
- base64-encoded image data
- Save/display the image
Example using the OpenAI API
Python
from openai import OpenAI
client = OpenAI()
result = client.images.generate(
model="gpt-image-1",
prompt="a red panda wearing sunglasses on a beach, sunset, cinematic style"
)
# If the API returns base64 data:
image_base64 = result.data[0].b64_json
print(image_base64)
JavaScript
import OpenAI from "openai";
const client = new OpenAI();
const result = await client.images.generate({
model: "gpt-image-1",
prompt: "a red panda wearing sunglasses on a beach, sunset, cinematic style",
});
console.log(result.data[0]);
If the API returns a URL
Some image APIs return a temporary image URL instead of raw image data:
image_url = result.data[0].url
print(image_url)
Then you can download it with requests or open it in a browser.
Typical request fields
Depending on the API, you may also be able to set:
size— image dimensionsquality— speed vs fidelitystyle— artistic stylen— number of imagesresponse_format— URL or base64
Basic workflow
- Write a clear prompt
- Call the image generation API
- Get the output image
- Decode/download it
- Save it as
.pngor.jpg
If you want, I can give you a complete working example in Python, JavaScript, or cURL.
Brands