Prompt
How do I generate images from text in an app using an API?
Latest observation
Oct 11, 2026
OpenAI APIWeb search: off
To generate images from text in an app using an API, the basic flow is:
-
User enters a prompt
Example: “A futuristic city at sunset in watercolor style” -
Your app sends that prompt to an image-generation API
-
The API returns either
- an image file/URL, or
- base64 image data
-
Your app displays or saves the image
Typical implementation steps
1) Choose an image generation API
Common options include:
- OpenAI image generation
- Stability AI
- Replicate
- Hugging Face inference APIs
- Cloud providers or model-hosted services
2) Create an API request
Usually you send:
- the text prompt
- optional size, style, number of images, seed, etc.
Example request fields might look like:
{
"prompt": "A futuristic city at sunset in watercolor style",
"size": "1024x1024",
"n": 1
}
3) Handle the response
The API may return:
- a hosted image URL
- base64-encoded image content
- a job ID if generation is asynchronous
4) Show the image in your app
- Web: set
img.srcto the returned URL - Mobile: download the image and render it in an image component
Example: JavaScript frontend calling your backend
It’s best to call the image API from your backend, not directly from the frontend, so your API key stays private.
Frontend
async function generateImage(prompt) {
const res = await fetch("/api/generate-image", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ prompt })
});
const data = await res.json();
return data.imageUrl;
}
Backend example (Node.js/Express)
import express from "express";
import fetch from "node-fetch";
const app = express();
app.use(express.json());
app.post("/api/generate-image", async (req, res) => {
const { prompt } = req.body;
const apiResponse = await fetch("https://api.example.com/images/generate", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${process.env.API_KEY}`
},
body: JSON.stringify({
prompt,
size: "1024x1024"
})
});
const result = await apiResponse.json();
res.json({
imageUrl: result.image_url
});
});
app.listen(3000);
Important best practices
- Keep API keys on the server
- Add moderation/filtering for unsafe prompts if needed
- Rate limit requests to prevent abuse
- Use async jobs if generation takes a long time
- Cache results if users may request the same prompt repeatedly
If you want a quick architecture
Frontend app → Your backend → Image generation API → Backend returns image URL/data → Frontend displays image
If you want, I can give you:
- a real example using OpenAI’s image API
- a Python Flask example
- a React app example
- or a mobile app example