Gemini 3.1 Flash Image
gemini-3.1-flash-image
Thông số
- Nhà cung cấp
- Loại
- Tạo ảnh
- Đầu vào
- Văn bản, Ảnh
- Đầu ra
- Văn bản, Ảnh
- Ngữ cảnh
- 200K
- Định dạng API
- Images, Gemini
Giá
| Mục | VNĐ | Credit | Đơn vị |
|---|---|---|---|
| Mỗi ảnh | 1.386đ | 1.386 credit | / ảnh |
1 credit = 1đ. Credit trừ theo usage thực tế sau mỗi request. Xem bảng giá và gói nạp
Bắt đầu nhanh
- Tạo API key tại trang API key (cần đăng ký trước).
- Lưu key vào biến môi trường
WIROUTER_API_KEY. - Gọi API với base URL
https://api.wirouter.store/v1và modelgemini-3.1-flash-image.
export WIROUTER_API_KEY="your-api-key"
curl -s https://api.wirouter.store/v1/models -H "Authorization: Bearer $WIROUTER_API_KEY"
Gọi API
Tạo ảnh
Tạo ảnh (OpenAI Images) POST /v1/images/generations
curl -sS --fail-with-body https://api.wirouter.store/v1/images/generations \
-H "Authorization: Bearer $WIROUTER_API_KEY" \
-H "Content-Type: application/json" \
--data @- <<'EOF' | jq -er '.data[0].b64_json' | base64 -d > image.png
{
"model": "gemini-3.1-flash-image",
"prompt": "Một chú cáo đỏ ngồi trên tuyết, tranh minh họa phẳng",
"aspect_ratio": "16:9",
"resolution": "2K",
"n": 1
}
EOF
import base64
import os
import requests
API_KEY = os.environ["WIROUTER_API_KEY"]
resp = requests.post(
"https://api.wirouter.store/v1/images/generations",
headers={"Authorization": f"Bearer {API_KEY}"},
json={
"model": "gemini-3.1-flash-image",
"prompt": "Một chú cáo đỏ ngồi trên tuyết, tranh minh họa phẳng",
"aspect_ratio": "16:9",
"resolution": "2K",
"n": 1
},
timeout=300,
)
resp.raise_for_status()
data = resp.json()
open("image.png", "wb").write(base64.b64decode(data["data"][0]["b64_json"]))
// Node 18+ (lưu thành file .mjs)
import fs from "node:fs";
const apiKey = process.env.WIROUTER_API_KEY;
const resp = await fetch("https://api.wirouter.store/v1/images/generations", {
method: "POST",
headers: { Authorization: `Bearer ${apiKey}`, "Content-Type": "application/json" },
body: JSON.stringify({
"model": "gemini-3.1-flash-image",
"prompt": "Một chú cáo đỏ ngồi trên tuyết, tranh minh họa phẳng",
"aspect_ratio": "16:9",
"resolution": "2K",
"n": 1
}),
});
if (!resp.ok) throw new Error(`HTTP ${resp.status}: ${await resp.text()}`);
const data = await resp.json();
fs.writeFileSync("image.png", Buffer.from(data.data[0].b64_json, "base64"));
Tạo ảnh (Gemini native) POST /v1beta/models/gemini-3.1-flash-image:generateContent
curl -sS --fail-with-body https://api.wirouter.store/v1beta/models/gemini-3.1-flash-image:generateContent \
-H "x-goog-api-key: $WIROUTER_API_KEY" \
-H "content-type: application/json" \
--data @- <<'EOF' | jq -er '.candidates[0].content.parts[] | select(.inlineData) | .inlineData.data' | base64 -d > image.png
{
"contents": [
{
"role": "user",
"parts": [
{
"text": "Một chú cáo đỏ ngồi trên tuyết, tranh minh họa phẳng"
}
]
}
],
"generationConfig": {
"responseModalities": [
"IMAGE",
"TEXT"
],
"imageConfig": {
"aspectRatio": "16:9",
"imageSize": "2K"
}
}
}
EOF
import base64
import os
import requests
API_KEY = os.environ["WIROUTER_API_KEY"]
resp = requests.post(
"https://api.wirouter.store/v1beta/models/gemini-3.1-flash-image:generateContent",
headers={"x-goog-api-key": API_KEY},
json={
"contents": [
{
"role": "user",
"parts": [
{
"text": "Một chú cáo đỏ ngồi trên tuyết, tranh minh họa phẳng"
}
]
}
],
"generationConfig": {
"responseModalities": [
"IMAGE",
"TEXT"
],
"imageConfig": {
"aspectRatio": "16:9",
"imageSize": "2K"
}
}
},
timeout=300,
)
resp.raise_for_status()
data = resp.json()
for part in data["candidates"][0]["content"]["parts"]:
if "inlineData" in part:
open("image.png", "wb").write(base64.b64decode(part["inlineData"]["data"]))
// Node 18+ (lưu thành file .mjs)
import fs from "node:fs";
const apiKey = process.env.WIROUTER_API_KEY;
const resp = await fetch("https://api.wirouter.store/v1beta/models/gemini-3.1-flash-image:generateContent", {
method: "POST",
headers: { "x-goog-api-key": apiKey, "Content-Type": "application/json" },
body: JSON.stringify({
"contents": [
{
"role": "user",
"parts": [
{
"text": "Một chú cáo đỏ ngồi trên tuyết, tranh minh họa phẳng"
}
]
}
],
"generationConfig": {
"responseModalities": [
"IMAGE",
"TEXT"
],
"imageConfig": {
"aspectRatio": "16:9",
"imageSize": "2K"
}
}
}),
});
if (!resp.ok) throw new Error(`HTTP ${resp.status}: ${await resp.text()}`);
const data = await resp.json();
const part = data.candidates[0].content.parts.find((p) => p.inlineData);
fs.writeFileSync("image.png", Buffer.from(part.inlineData.data, "base64"));
Sửa ảnh
Sửa ảnh có sẵn POST /v1beta/models/gemini-3.1-flash-image:generateContent
IMG=$(base64 < photo.jpg | tr -d '\n')
curl -sS --fail-with-body https://api.wirouter.store/v1beta/models/gemini-3.1-flash-image:generateContent \
-H "x-goog-api-key: $WIROUTER_API_KEY" \
-H "content-type: application/json" \
--data @- <<EOF | jq -er '.candidates[0].content.parts[] | select(.inlineData) | .inlineData.data' | base64 -d > image.png
{
"contents": [
{
"role": "user",
"parts": [
{
"text": "Giữ nguyên chủ thể, đổi nền thành bãi biển lúc hoàng hôn"
},
{
"inlineData": {
"mimeType": "image/jpeg",
"data": "${IMG}"
}
}
]
}
],
"generationConfig": {
"responseModalities": [
"IMAGE",
"TEXT"
]
}
}
EOF
import base64
import os
import requests
API_KEY = os.environ["WIROUTER_API_KEY"]
image_b64 = base64.b64encode(open("photo.jpg", "rb").read()).decode()
resp = requests.post(
"https://api.wirouter.store/v1beta/models/gemini-3.1-flash-image:generateContent",
headers={"x-goog-api-key": API_KEY},
json={
"contents": [
{
"role": "user",
"parts": [
{
"text": "Giữ nguyên chủ thể, đổi nền thành bãi biển lúc hoàng hôn"
},
{
"inlineData": {
"mimeType": "image/jpeg",
"data": image_b64
}
}
]
}
],
"generationConfig": {
"responseModalities": [
"IMAGE",
"TEXT"
]
}
},
timeout=300,
)
resp.raise_for_status()
data = resp.json()
for part in data["candidates"][0]["content"]["parts"]:
if "inlineData" in part:
open("image.png", "wb").write(base64.b64decode(part["inlineData"]["data"]))
// Node 18+ (lưu thành file .mjs)
import fs from "node:fs";
const apiKey = process.env.WIROUTER_API_KEY;
const imageB64 = fs.readFileSync("photo.jpg").toString("base64");
const resp = await fetch("https://api.wirouter.store/v1beta/models/gemini-3.1-flash-image:generateContent", {
method: "POST",
headers: { "x-goog-api-key": apiKey, "Content-Type": "application/json" },
body: JSON.stringify({
"contents": [
{
"role": "user",
"parts": [
{
"text": "Giữ nguyên chủ thể, đổi nền thành bãi biển lúc hoàng hôn"
},
{
"inlineData": {
"mimeType": "image/jpeg",
"data": imageB64
}
}
]
}
],
"generationConfig": {
"responseModalities": [
"IMAGE",
"TEXT"
]
}
}),
});
if (!resp.ok) throw new Error(`HTTP ${resp.status}: ${await resp.text()}`);
const data = await resp.json();
const part = data.candidates[0].content.parts.find((p) => p.inlineData);
fs.writeFileSync("image.png", Buffer.from(part.inlineData.data, "base64"));
Lỗi thường gặp
401- API key sai, đã bị thu hồi hoặc tài khoản hết credit. Kiểm tra key và số dư.
429- Model tạm hết quota hoặc đang cooldown. Thử lại sau số giây trong header
Retry-After. 400- Request không hợp lệ, thường do sai model ID. Dùng đúng ID
gemini-3.1-flash-image. 524- Request chạy quá 100 giây nên bị cắt. Dùng stream hoặc rút gọn yêu cầu.