Text-to-Image: Generate an image from a text prompt
curl --request POST \
--url https://api.apiyi.com/v1beta/models/gemini-3.1-flash-image-preview:generateContent \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"contents": [
{
"parts": [
{
"text": "A cute Shiba Inu sitting under cherry blossom trees, watercolor style, HD details"
}
]
}
],
"generationConfig": {
"responseModalities": [
"IMAGE"
],
"imageConfig": {
"aspectRatio": "16:9",
"imageSize": "2K"
}
}
}
'import requests
url = "https://api.apiyi.com/v1beta/models/gemini-3.1-flash-image-preview:generateContent"
payload = {
"contents": [{ "parts": [{ "text": "A cute Shiba Inu sitting under cherry blossom trees, watercolor style, HD details" }] }],
"generationConfig": {
"responseModalities": ["IMAGE"],
"imageConfig": {
"aspectRatio": "16:9",
"imageSize": "2K"
}
}
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
contents: [
{
parts: [
{
text: 'A cute Shiba Inu sitting under cherry blossom trees, watercolor style, HD details'
}
]
}
],
generationConfig: {
responseModalities: ['IMAGE'],
imageConfig: {aspectRatio: '16:9', imageSize: '2K'}
}
})
};
fetch('https://api.apiyi.com/v1beta/models/gemini-3.1-flash-image-preview:generateContent', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.apiyi.com/v1beta/models/gemini-3.1-flash-image-preview:generateContent",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'contents' => [
[
'parts' => [
[
'text' => 'A cute Shiba Inu sitting under cherry blossom trees, watercolor style, HD details'
]
]
]
],
'generationConfig' => [
'responseModalities' => [
'IMAGE'
],
'imageConfig' => [
'aspectRatio' => '16:9',
'imageSize' => '2K'
]
]
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.apiyi.com/v1beta/models/gemini-3.1-flash-image-preview:generateContent"
payload := strings.NewReader("{\n \"contents\": [\n {\n \"parts\": [\n {\n \"text\": \"A cute Shiba Inu sitting under cherry blossom trees, watercolor style, HD details\"\n }\n ]\n }\n ],\n \"generationConfig\": {\n \"responseModalities\": [\n \"IMAGE\"\n ],\n \"imageConfig\": {\n \"aspectRatio\": \"16:9\",\n \"imageSize\": \"2K\"\n }\n }\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.apiyi.com/v1beta/models/gemini-3.1-flash-image-preview:generateContent")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"contents\": [\n {\n \"parts\": [\n {\n \"text\": \"A cute Shiba Inu sitting under cherry blossom trees, watercolor style, HD details\"\n }\n ]\n }\n ],\n \"generationConfig\": {\n \"responseModalities\": [\n \"IMAGE\"\n ],\n \"imageConfig\": {\n \"aspectRatio\": \"16:9\",\n \"imageSize\": \"2K\"\n }\n }\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.apiyi.com/v1beta/models/gemini-3.1-flash-image-preview:generateContent")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"contents\": [\n {\n \"parts\": [\n {\n \"text\": \"A cute Shiba Inu sitting under cherry blossom trees, watercolor style, HD details\"\n }\n ]\n }\n ],\n \"generationConfig\": {\n \"responseModalities\": [\n \"IMAGE\"\n ],\n \"imageConfig\": {\n \"aspectRatio\": \"16:9\",\n \"imageSize\": \"2K\"\n }\n }\n}"
response = http.request(request)
puts response.read_body{
"candidates": [
{
"content": {
"parts": [
{
"inlineData": {
"mimeType": "image/png",
"data": "<string>"
}
}
]
},
"finishReason": "STOP"
}
],
"usageMetadata": {
"promptTokenCount": 10,
"candidatesTokenCount": 258
}
}Nano Banana 2 Image Gen
Text-to-Image API Reference
Nano Banana 2 text-to-image API reference and interactive playground — generate images from text prompts
POST
/
v1beta
/
models
/
gemini-3.1-flash-image-preview:generateContent
Text-to-Image: Generate an image from a text prompt
curl --request POST \
--url https://api.apiyi.com/v1beta/models/gemini-3.1-flash-image-preview:generateContent \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"contents": [
{
"parts": [
{
"text": "A cute Shiba Inu sitting under cherry blossom trees, watercolor style, HD details"
}
]
}
],
"generationConfig": {
"responseModalities": [
"IMAGE"
],
"imageConfig": {
"aspectRatio": "16:9",
"imageSize": "2K"
}
}
}
'import requests
url = "https://api.apiyi.com/v1beta/models/gemini-3.1-flash-image-preview:generateContent"
payload = {
"contents": [{ "parts": [{ "text": "A cute Shiba Inu sitting under cherry blossom trees, watercolor style, HD details" }] }],
"generationConfig": {
"responseModalities": ["IMAGE"],
"imageConfig": {
"aspectRatio": "16:9",
"imageSize": "2K"
}
}
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
contents: [
{
parts: [
{
text: 'A cute Shiba Inu sitting under cherry blossom trees, watercolor style, HD details'
}
]
}
],
generationConfig: {
responseModalities: ['IMAGE'],
imageConfig: {aspectRatio: '16:9', imageSize: '2K'}
}
})
};
fetch('https://api.apiyi.com/v1beta/models/gemini-3.1-flash-image-preview:generateContent', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.apiyi.com/v1beta/models/gemini-3.1-flash-image-preview:generateContent",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'contents' => [
[
'parts' => [
[
'text' => 'A cute Shiba Inu sitting under cherry blossom trees, watercolor style, HD details'
]
]
]
],
'generationConfig' => [
'responseModalities' => [
'IMAGE'
],
'imageConfig' => [
'aspectRatio' => '16:9',
'imageSize' => '2K'
]
]
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.apiyi.com/v1beta/models/gemini-3.1-flash-image-preview:generateContent"
payload := strings.NewReader("{\n \"contents\": [\n {\n \"parts\": [\n {\n \"text\": \"A cute Shiba Inu sitting under cherry blossom trees, watercolor style, HD details\"\n }\n ]\n }\n ],\n \"generationConfig\": {\n \"responseModalities\": [\n \"IMAGE\"\n ],\n \"imageConfig\": {\n \"aspectRatio\": \"16:9\",\n \"imageSize\": \"2K\"\n }\n }\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.apiyi.com/v1beta/models/gemini-3.1-flash-image-preview:generateContent")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"contents\": [\n {\n \"parts\": [\n {\n \"text\": \"A cute Shiba Inu sitting under cherry blossom trees, watercolor style, HD details\"\n }\n ]\n }\n ],\n \"generationConfig\": {\n \"responseModalities\": [\n \"IMAGE\"\n ],\n \"imageConfig\": {\n \"aspectRatio\": \"16:9\",\n \"imageSize\": \"2K\"\n }\n }\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.apiyi.com/v1beta/models/gemini-3.1-flash-image-preview:generateContent")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"contents\": [\n {\n \"parts\": [\n {\n \"text\": \"A cute Shiba Inu sitting under cherry blossom trees, watercolor style, HD details\"\n }\n ]\n }\n ],\n \"generationConfig\": {\n \"responseModalities\": [\n \"IMAGE\"\n ],\n \"imageConfig\": {\n \"aspectRatio\": \"16:9\",\n \"imageSize\": \"2K\"\n }\n }\n}"
response = http.request(request)
puts response.read_body{
"candidates": [
{
"content": {
"parts": [
{
"inlineData": {
"mimeType": "image/png",
"data": "<string>"
}
}
]
},
"finishReason": "STOP"
}
],
"usageMetadata": {
"promptTokenCount": 10,
"candidatesTokenCount": 258
}
}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
imageSizeto the smallest tier (e.g.512/1K) to shrink the response.
Code Examples
Python
import requests
import base64
API_KEY = "sk-your-api-key"
PROMPT = "A cute Shiba Inu sitting under cherry blossom trees, watercolor style, HD details"
response = requests.post(
"https://api.apiyi.com/v1beta/models/gemini-3.1-flash-image-preview:generateContent",
headers={"Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json"},
json={
"contents": [{"parts": [{"text": PROMPT}]}],
"generationConfig": {
"responseModalities": ["IMAGE"],
"imageConfig": {"aspectRatio": "16:9", "imageSize": "2K"}
}
},
timeout=300
).json()
img_data = response["candidates"][0]["content"]["parts"][0]["inlineData"]["data"]
with open("output.png", 'wb') as f:
f.write(base64.b64decode(img_data))
print("Image saved to output.png")
cURL
curl -X POST "https://api.apiyi.com/v1beta/models/gemini-3.1-flash-image-preview:generateContent" \
-H "Authorization: Bearer sk-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"contents": [{"parts": [{"text": "Futuristic city night view, neon lights, cyberpunk style"}]}],
"generationConfig": {
"responseModalities": ["IMAGE"],
"imageConfig": {"aspectRatio": "16:9", "imageSize": "2K"}
}
}'
Node.js
import fs from "fs";
const API_KEY = "sk-your-api-key";
const response = await fetch(
"https://api.apiyi.com/v1beta/models/gemini-3.1-flash-image-preview:generateContent",
{
method: "POST",
headers: {
"Authorization": `Bearer ${API_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
contents: [{ parts: [{ text: "Futuristic city night view, neon lights, cyberpunk style" }] }],
generationConfig: {
responseModalities: ["IMAGE"],
imageConfig: { aspectRatio: "16:9", imageSize: "2K" }
}
})
}
);
const data = await response.json();
const imgBase64 = data.candidates[0].content.parts[0].inlineData.data;
fs.writeFileSync("output.png", Buffer.from(imgBase64, "base64"));
Parameter Quick Reference
| Parameter | Type | Required | Description |
|---|---|---|---|
contents[].parts[].text | string | Yes | Text prompt |
generationConfig.responseModalities | array | Yes | ["IMAGE"] or ["TEXT","IMAGE"] |
generationConfig.imageConfig.aspectRatio | string | No | 14 ratios, default 1:1 |
generationConfig.imageConfig.imageSize | string | No | 512 / 1K / 2K / 4K, default 1K |
generationConfig.thinkingConfig.thinkingLevel | string | No | minimal (fast) / High (deep reasoning), default minimal |
generationConfig.thinkingConfig.includeThoughts | boolean | No | Return thinking process text, default false |
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.Unsupported FeaturesThe following Google official features are not supported via APIYI and require separate billing:
- Grounding with Google Search: Real-time search info via
tools: [{"google_search": {}}] - Image Search Grounding (Nano Banana 2 exclusive): Visual context from Google image search
Authorizations
API Key obtained from APIYI Console
Body
application/json
Was this page helpful?
⌘I