Skip to main content
POST
Text-to-Image: Generate an image from a text prompt
The interactive Playground on the right supports dropdown selection for parameters (aspect ratio, resolution, response type, etc.). Enter your API Key in the Authorization field (format: Bearer sk-xxx) to send test requests with one click.
Scope: This page is for text-to-image generation. Just enter a prompt — no image upload required. For editing an existing image, use the Image Editing endpoint.
🖥️ Browser Playground limitation (important)This endpoint returns a base64-encoded image (inlineData.data, typically several MB) in the response. Due to browser rendering limits, the Playground on the right may show 请求时发生错误: unable to complete request after the response arrives — the request actually succeeded; the browser just can’t render such a long base64 string.Recommended workflow (beginner-friendly):
  • Copy the Python / Node.js / cURL sample below and run it locally. The code automatically base64.b64decodes the response and writes the image to a file.
  • If you must use the in-browser Playground, set imageSize to the smallest tier (1K) to shrink the response.
All image APIs are synchronous — there is no task ID to poll, and if your client disconnects the result is lost while the request is still billed. Set a generous timeout for this model; see Image API Essentials & Best Practices.

Code Examples

Python

cURL

Node.js

Parameter Quick Reference

See the field descriptions in the Playground on the right for detailed parameter docs, allowed values, and defaults. All enum-type fields (like aspectRatio, imageSize, thinkingLevel) support dropdown selection — no manual typing needed.
Google Search grounding: add "tools": [{"googleSearch": {}}] at the top level of the request. Each search query is billed an extra $0.014, and the model decides how many queries to run. See the Nano Banana 2.1 overview.

Authorizations

Authorization
string
header
required

API Key obtained from APIYI Console

Body

application/json
contents
object[]
required

Content array containing text prompts

generationConfig
object
required

Response

Successfully generated image

candidates
object[]

Generation results array

usageMetadata
object