文生图:根据文本提示词生成图片(官转)
curl --request POST \
--url https://api.apiyi.com/v1/images/generations \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"model": "grok-imagine-image",
"prompt": "A photorealistic red wooden boat moored on a glassy alpine lake at dawn, mist over the water, snow-capped peaks behind, cinematic photography"
}
'import requests
url = "https://api.apiyi.com/v1/images/generations"
payload = {
"model": "grok-imagine-image",
"prompt": "A photorealistic red wooden boat moored on a glassy alpine lake at dawn, mist over the water, snow-capped peaks behind, cinematic photography"
}
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({
model: 'grok-imagine-image',
prompt: 'A photorealistic red wooden boat moored on a glassy alpine lake at dawn, mist over the water, snow-capped peaks behind, cinematic photography'
})
};
fetch('https://api.apiyi.com/v1/images/generations', 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/v1/images/generations",
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([
'model' => 'grok-imagine-image',
'prompt' => 'A photorealistic red wooden boat moored on a glassy alpine lake at dawn, mist over the water, snow-capped peaks behind, cinematic photography'
]),
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/v1/images/generations"
payload := strings.NewReader("{\n \"model\": \"grok-imagine-image\",\n \"prompt\": \"A photorealistic red wooden boat moored on a glassy alpine lake at dawn, mist over the water, snow-capped peaks behind, cinematic photography\"\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/v1/images/generations")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"model\": \"grok-imagine-image\",\n \"prompt\": \"A photorealistic red wooden boat moored on a glassy alpine lake at dawn, mist over the water, snow-capped peaks behind, cinematic photography\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.apiyi.com/v1/images/generations")
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 \"model\": \"grok-imagine-image\",\n \"prompt\": \"A photorealistic red wooden boat moored on a glassy alpine lake at dawn, mist over the water, snow-capped peaks behind, cinematic photography\"\n}"
response = http.request(request)
puts response.read_body{
"created": 0,
"data": [
{
"url": "<string>",
"b64_json": "<string>"
}
],
"usage": {
"prompt_tokens": 1000,
"total_tokens": 1000
}
}Grok Imagine 官轉 生圖
Grok Imagine 官轉文生圖 API 參考
Grok Imagine 官轉文生圖 API 參考與線上除錯 — 三款模型、1k / 1.5k / 2k 三檔解析度、2.0 支援 low / medium 畫質,與官網同價按檔計費
POST
/
v1
/
images
/
generations
文生图:根据文本提示词生成图片(官转)
curl --request POST \
--url https://api.apiyi.com/v1/images/generations \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"model": "grok-imagine-image",
"prompt": "A photorealistic red wooden boat moored on a glassy alpine lake at dawn, mist over the water, snow-capped peaks behind, cinematic photography"
}
'import requests
url = "https://api.apiyi.com/v1/images/generations"
payload = {
"model": "grok-imagine-image",
"prompt": "A photorealistic red wooden boat moored on a glassy alpine lake at dawn, mist over the water, snow-capped peaks behind, cinematic photography"
}
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({
model: 'grok-imagine-image',
prompt: 'A photorealistic red wooden boat moored on a glassy alpine lake at dawn, mist over the water, snow-capped peaks behind, cinematic photography'
})
};
fetch('https://api.apiyi.com/v1/images/generations', 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/v1/images/generations",
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([
'model' => 'grok-imagine-image',
'prompt' => 'A photorealistic red wooden boat moored on a glassy alpine lake at dawn, mist over the water, snow-capped peaks behind, cinematic photography'
]),
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/v1/images/generations"
payload := strings.NewReader("{\n \"model\": \"grok-imagine-image\",\n \"prompt\": \"A photorealistic red wooden boat moored on a glassy alpine lake at dawn, mist over the water, snow-capped peaks behind, cinematic photography\"\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/v1/images/generations")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"model\": \"grok-imagine-image\",\n \"prompt\": \"A photorealistic red wooden boat moored on a glassy alpine lake at dawn, mist over the water, snow-capped peaks behind, cinematic photography\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.apiyi.com/v1/images/generations")
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 \"model\": \"grok-imagine-image\",\n \"prompt\": \"A photorealistic red wooden boat moored on a glassy alpine lake at dawn, mist over the water, snow-capped peaks behind, cinematic photography\"\n}"
response = http.request(request)
puts response.read_body{
"created": 0,
"data": [
{
"url": "<string>",
"b64_json": "<string>"
}
],
"usage": {
"prompt_tokens": 1000,
"total_tokens": 1000
}
}🔒 需申請開通
GrokOfficial 分組:Grok Imagine 官轉不在 Default 預設分組,需申請開通後才能呼叫(含本頁 Playground)。未開通、或令牌計費模式為「按次計費」時,呼叫固定返回 503。申請流程見 Grok Imagine 官轉概覽 · 分組介紹。右側的互動式 Playground 支援直接線上除錯。請在 Authorization 中填入你的 API Key(格式:
Bearer sk-xxx),輸入 prompt,選擇模型、resolution、aspect_ratio(2.0 再選 quality)後一鍵傳送即可。場景說明:本頁用於「文本生成圖片」,每次 1 張。本通道暫不開放圖片編輯,需要基於參考圖改圖或單次多張,請使用 Grok Imagine 官逆。
⚠️ 三個最容易踩的坑
- 用
resolution,不要用size:size不會被換算成解析度檔,傳了會按預設1k出圖和計費。 - 引數寫錯會報錯:
resolution/aspect_ratio不在列舉內返回 422,quality: "high"返回 400——這與官逆通道「靜默回退」的行為相反。 n固定傳 1:需要多張請併發發起多次請求。
圖片 API 全部為同步呼叫:沒有非同步任務 ID,客戶端斷開連線結果即丟失、但請求仍會計費。
grok-imagine-image-2.0 的 medium 檔 2K 約 60–80 秒,建議客戶端超時設到 360 秒,詳見 圖片 API 呼叫須知與最佳實踐。程式碼示例
Python(OpenAI SDK 直連)
from openai import OpenAI
import urllib.request
client = OpenAI(
api_key="sk-your-api-key",
base_url="https://api.apiyi.com/v1",
timeout=360.0 # 圖片 API 是同步呼叫,超時要給足
)
resp = client.images.generate(
model="grok-imagine-image-2.0",
prompt="A photorealistic red wooden boat moored on a glassy alpine lake at dawn, "
"mist over the water, snow-capped peaks behind, cinematic photography",
n=1,
# resolution / aspect_ratio / quality 不是 OpenAI SDK 的標準欄位,要放進 extra_body
extra_body={
"resolution": "1.5k",
"aspect_ratio": "16:9",
"quality": "medium"
}
)
# 預設返回圖片連結,請立即下載轉存
urllib.request.urlretrieve(resp.data[0].url, "out.png")
Python(原生 requests)
import requests
import base64
API_KEY = "sk-your-api-key"
response = requests.post(
"https://api.apiyi.com/v1/images/generations",
headers={
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
},
json={
"model": "grok-imagine-image-quality",
"prompt": "賽博朋克城市雨夜,霓虹招牌特寫,電影感打光",
"n": 1,
"resolution": "2k",
"aspect_ratio": "9:16",
"response_format": "b64_json"
},
timeout=360
).json()
# b64_json 是純 base64(不帶 data: 字首),直接 decode 寫檔案
with open("out.png", "wb") as f:
f.write(base64.b64decode(response["data"][0]["b64_json"]))
cURL
curl -X POST "https://api.apiyi.com/v1/images/generations" \
-H "Authorization: Bearer sk-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "grok-imagine-image",
"prompt": "一隻戴墨鏡的橘貓坐在海邊吧檯,寫實攝影,暖色調夕陽",
"n": 1,
"resolution": "1k",
"aspect_ratio": "3:2"
}'
Node.js(原生 fetch)
import fs from 'node:fs';
// 需要 3 張圖:併發 3 次請求,每次 n=1
const prompts = Array(3).fill('A serene Japanese garden with cherry blossoms, koi pond, golden hour');
const results = await Promise.all(prompts.map(prompt =>
fetch('https://api.apiyi.com/v1/images/generations', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer sk-your-api-key'
},
body: JSON.stringify({
model: 'grok-imagine-image-2.0',
prompt,
n: 1,
resolution: '1k',
aspect_ratio: '4:3',
quality: 'low'
}),
signal: AbortSignal.timeout(360000)
}).then(r => r.json())
));
for (const [i, data] of results.entries()) {
const img = await fetch(data.data[0].url);
fs.writeFileSync(`out-${i}.img`, Buffer.from(await img.arrayBuffer()));
}
引數說明速查
| 引數名 | 型別 | 必填 | 預設 | 說明 |
|---|---|---|---|---|
model | string | ✅ | — | grok-imagine-image / grok-imagine-image-quality / grok-imagine-image-2.0 |
prompt | string | ✅ | — | 提示詞,支援中英文。建議描述主體、場景、風格、光線 |
n | integer | ❌ | 1 | 本通道固定傳 1,多張請併發呼叫 |
resolution | string | ❌ | 1k | 1k / 1.5k / 2k(小寫),決定計費檔;列舉外的值返回 422 |
aspect_ratio | string | ❌ | 1:1 | 常用 1:1 / 16:9 / 9:16 / 4:3 / 3:4 / 3:2 / 2:3;列舉外的值返回 422 |
quality | string | ❌ | low | 僅 grok-imagine-image-2.0:low 或 medium;傳 high 返回 400 |
response_format | string | ❌ | url | url 返回圖片連結;b64_json 返回純 base64(不帶 data: 字首) |
| 模型 | 1k | 1.5k | 2k |
|---|---|---|---|
grok-imagine-image | $0.02 | $0.02 | $0.02 |
grok-imagine-image-quality | $0.05 | $0.06 | $0.07 |
grok-imagine-image-2.0(low) | $0.04 | $0.05 | $0.06 |
grok-imagine-image-2.0(medium) | $0.06 | $0.07 | $0.08 |
模型價格與官網對齊,且可能隨官網調整;上表僅供參考,具體以頂部導航「模型價格」欄目為準:模型價格。
響應格式
{
"created": 0,
"data": [
{ "url": "https://.../sync_xxxxxxxx_0.png" }
],
"usage": {
"prompt_tokens": 1000,
"total_tokens": 1000
}
}
data[]裡每項只有url或b64_json二選一,取決於response_format。- 返回的圖片連結請立即下載轉存,不要長期引用。
usage是佔位值(prompt_tokens恆為1000 × 張數),不能用來核賬,以控制台賬單為準。created恆為 0,不要拿來計時。
相關文件
- Grok Imagine 官轉概覽 - 定價、分組申請、官轉與官逆對比
- Grok Imagine 影片生成 - 同一分組,按秒計費
- Grok Imagine 官逆 - 支援圖片編輯與單次多張
授權
在 API易控制台获取的 API Key(令牌分组需包含 GrokOfficial)
主體
application/json
模型 ID:
grok-imagine-image—— 标准版,各分辨率均 $0.02/张grok-imagine-image-quality—— 高质量版,1k / 1.5k / 2k 为 $0.05 / $0.06 / $0.07grok-imagine-image-2.0—— 新一代,low$0.04 / $0.05 / $0.06,medium$0.06 / $0.07 / $0.08
可用選項:
grok-imagine-image, grok-imagine-image-quality, grok-imagine-image-2.0 提示词,支持中英文。建议详细描述主体、场景、风格、光线
範例:
"A photorealistic red wooden boat moored on a glassy alpine lake at dawn, mist over the water, snow-capped peaks behind, cinematic photography"
出图数量。本通道固定为 1,需要多张请并发调用
可用選項:
1 分辨率档位(小写),决定计费档:1k(默认)/ 1.5k / 2k。
枚举外的值(如 4k、1K)返回 422。不要用 size 代替,size 不会被换算成分辨率档。
可用選項:
1k, 1.5k, 2k 输出宽高比,默认 1:1。实测示例:grok-imagine-image 的 1k + 3:2 输出 1248×832;-quality 的 2k + 9:16 输出 1584×2816。
枚举外的值(如 5:7)返回 422。
可用選項:
1:1, 16:9, 9:16, 4:3, 3:4, 3:2, 2:3 画质档,仅 grok-imagine-image-2.0 有效:low(默认)或 medium(单价高 $0.02,更慢)。
传 high 返回 400。另外两个模型不需要传。
可用選項:
low, medium 返回格式:url(默认,图片链接,请立即下载转存)或 b64_json(纯 base64,不带 data: 前缀)
可用選項:
url, b64_json 這個頁面有幫助嗎?