Prompt
How do I generate images from text with an API?
Latest observation
Oct 18, 2026
OpenAI APIWeb search: off
You can generate images from text with an image-generation API by sending a prompt (the text description) to the model and receiving back an image URL or image bytes.
Basic flow
- Choose an image model/provider
- Examples: OpenAI, Stability AI, Adobe, Replicate, etc.
- Create an API key
- Store it securely in an environment variable.
- Send a text prompt
- Example: “A futuristic city at sunset, cinematic lighting, ultra-detailed”
- Get the result
- Usually returned as:
- a hosted image URL
- base64-encoded image data
- a file you can save locally
- Usually returned as:
- Display or store the image
Example with OpenAI-style API
Python
from openai import OpenAI
client = OpenAI(api_key="YOUR_API_KEY")
response = client.images.generate(
model="gpt-image-1",
prompt="A red fox wearing a spacesuit on Mars, realistic, high detail"
)
image_url = response.data[0].url
print(image_url)
JavaScript
import OpenAI from "openai";
const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const response = await client.images.generate({
model: "gpt-image-1",
prompt: "A red fox wearing a spacesuit on Mars, realistic, high detail"
});
console.log(response.data[0].url);
Common options
Depending on the API, you may also pass:
size— image dimensions like1024x1024style— realistic, artistic, etc.n— number of images to generatenegative_prompt— what to avoidseed— for repeatabilityquality— speed vs detail
Practical tips
- Be specific: subject, style, lighting, composition, mood.
- Add constraints: “no text,” “white background,” “centered composition.”
- Iterate by refining the prompt.
- Check the provider’s content policy and rate limits.
If you want the simplest possible implementation
Tell me:
- your language: Python / JavaScript / cURL / another
- which provider you want to use
…and I’ll give you a copy-paste example.