文生图:根据文本提示词生成图片(官转)
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 此页面对您有帮助吗?