API guide
Nano Banana 2 API
This page shows the API settings currently used by YingTu. The example never includes your real key.
Quickstart
- 1
Create or choose a LaoZhang API Key.
- 2
Copy the model ID and code example.
- 3
Run one small request and inspect the response before scaling.
Request details
- Generation endpoint
- POST /v1beta/models/gemini-3.1-flash-image:generateContent
- Editing endpoint
- POST /v1beta/models/gemini-3.1-flash-image:generateContent
- Base URL
- https://api2.laozhang.ai
- Authentication
- Bearer API Key
- Request format
- Gemini native
- Reference images
- Supported
- Reference-image limit
- 5 images in YingTu (not a provider maximum)
- Verification status
- Not yet verified — no dated verification record is available.
Python example
Replace the placeholder key and prompt. Use the documented edit endpoint when adding reference images.
Text-to-image example
export LAOZHANG_API_KEY="replace-with-your-key"
curl --fail-with-body --max-time 500 \
-X POST 'https://api2.laozhang.ai/v1beta/models/gemini-3.1-flash-image:generateContent' \
-H "Authorization: Bearer ${LAOZHANG_API_KEY}" \
-H "Content-Type: application/json" \
--data '{"contents":[{"parts":[{"text":"A clean product photo of a matte ceramic coffee mug on a light stone table, soft window light, natural shadows, minimal styling."}]}],"generationConfig":{"responseModalities":["IMAGE"],"imageConfig":{"imageSize":"2K"}}}'
# The JSON response can contain base64 image data or an image URL.
# If this request times out, check request and billing logs before retrying.Reference-image editing example
import base64
import mimetypes
import os
import sys
import requests
API_KEY = os.environ.get("LAOZHANG_API_KEY")
if not API_KEY:
raise RuntimeError("Set the LAOZHANG_API_KEY environment variable before running this example.")
API_URL = "https://api2.laozhang.ai/v1beta/models/gemini-3.1-flash-image:generateContent"
REQUEST_TIMEOUT_SECONDS = 500
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json",
}
def encode_image(image_path):
mime_type = mimetypes.guess_type(image_path)[0] or "image/jpeg"
with open(image_path, "rb") as image_file:
data = base64.b64encode(image_file.read()).decode("ascii")
return {"mime_type": mime_type, "data": data}
reference_1 = encode_image("image_1.jpg") # Replace with your image path
payload = {
"contents": [{
"parts": [
{"text": "A clean product photo of a matte ceramic coffee mug on a light stone table, soft window light, natural shadows, minimal styling."},
{"inline_data": reference_1}
]
}],
"generationConfig": {
"responseModalities": ["IMAGE"],
"imageConfig": {
"imageSize": "2K"
}
}
}
try:
response = requests.post(
API_URL,
headers=headers,
json=payload,
timeout=REQUEST_TIMEOUT_SECONDS,
)
except requests.Timeout as error:
print(
"Request timed out after 500 seconds. The result status may be unknown; "
"check call and billing logs before retrying.",
file=sys.stderr,
)
raise SystemExit(1) from error
if response.status_code != 200:
print(f"API error {response.status_code}: {response.text}", file=sys.stderr)
raise SystemExit(1)
try:
result = response.json()
except ValueError as error:
print("API returned a non-JSON response.", file=sys.stderr)
raise SystemExit(1) from error
if not isinstance(result, dict):
print("API returned an unexpected JSON shape.", file=sys.stderr)
raise SystemExit(1)
image_part = None
for candidate in result.get("candidates", []):
for part in candidate.get("content", {}).get("parts", []):
inline_data = part.get("inlineData") or part.get("inline_data")
if inline_data and inline_data.get("data"):
image_part = inline_data
break
if image_part:
break
if not image_part:
response_id = result.get("responseId", "not provided")
print(f"No image data in response. responseId={response_id}", file=sys.stderr)
raise SystemExit(1)
mime_type = image_part.get("mimeType") or image_part.get("mime_type") or "image/png"
encoded_image = image_part["data"]
if encoded_image.startswith("data:"):
header, encoded_image = encoded_image.split(",", 1)
mime_type = header[5:].split(";", 1)[0] or mime_type
extension_by_mime = {
"image/jpeg": "jpg",
"image/png": "png",
"image/webp": "webp",
"image/gif": "gif",
}
extension = extension_by_mime.get(mime_type, "bin")
output_path = f"output.{extension}"
with open(output_path, "wb") as output_file:
output_file.write(base64.b64decode(encoded_image, validate=True))
print(f"Image saved: {output_path} ({mime_type})")
Request parameters
The names below match the active YingTu client and generated examples.
- model
- Required. Use the exact model ID shown on this page.
- prompt
- Required. Send your image instruction; never place the API key in the prompt.
- output controls
generationConfig.imageConfig.aspectRatioandimageSize; automatic ratio is omitted.- reference images
- Optional. Up to 5 images in the YingTu browser are sent as
contents[].parts[].inline_data.
Response and timeout handling
- Image response
- The client looks for base64 image data in
candidates[].content.parts[].inlineDataorinline_dataand preserves the returned MIME type. - Non-image response
- A response without image data is not treated as success. The client reports a block reason, finish reason, or response ID when present.
500-second client timeout
The existing YingTu client aborts the request after 500 seconds. At that point the upstream result and charge may still be unknown.
No automatic retry
YingTu does not retry automatically. Check request and billing logs before deciding whether to submit another paid call.
Common error paths
- Authentication
- Confirm that the environment variable contains the intended key and that the request sends it as a Bearer credential.
- Access or balance
- Check model access, current balance, billing, and account restrictions in LaoZhang.
- Rate limit
- Reduce concurrency and inspect the returned upstream or account message before trying again.
- Non-image or empty result
- Inspect block and finish reasons, the API message, and any response ID. Revise the prompt only when the response explains why.
- Timeout
- The final state may be unknown. Check call and billing logs before making another request.
Known limitations
Check these points before using a generated result.
- Inspect the downloaded file dimensions; configured output controls do not replace result verification.
- Review identity, logos, packaging, colors, and small brand details before use.
- Availability and latency can vary with upstream capacity.
- Confirm live pricing, balance, access, and charges in the LaoZhang account.
API FAQ
Where should I store the API key?
Use the environment variable shown in the code example. Do not commit, print, or place the full key in a prompt.
Does a timeout mean the request failed?
Not necessarily. After 500 seconds YingTu stops waiting, but the upstream result or charge may still be unknown. Check logs before retrying.
Is the reference-image count a provider maximum?
No. This page shows YingTu’s current limit, not an upstream provider maximum.
Where do I confirm price and access?
Use the LaoZhang account as the authority for current pricing, balance, model access, and charges.
Price and billing
The page shows a reference price. Check your LaoZhang account for the current price, balance, access, and charges.
- Model ID
- gemini-3.1-flash-image
- Reference price
- $0.055/ call
- Output sizes
- 512 / 1K / 2K / 4K
- API format
- Gemini native
Test this model in Studio
These are the settings currently available in the Image Studio.
YingTuLaoZhang