curl --request POST \
--url https://api.apiyi.com/v1/videos \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"model": "grok-imagine-video-1.5-lite"
}
'import requests
url = "https://api.apiyi.com/v1/videos"
payload = { "model": "grok-imagine-video-1.5-lite" }
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-video-1.5-lite'})
};
fetch('https://api.apiyi.com/v1/videos', 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/videos",
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-video-1.5-lite'
]),
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/videos"
payload := strings.NewReader("{\n \"model\": \"grok-imagine-video-1.5-lite\"\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/videos")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"model\": \"grok-imagine-video-1.5-lite\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.apiyi.com/v1/videos")
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-video-1.5-lite\"\n}"
response = http.request(request)
puts response.read_body{
"id": "task_xxxxxxxxxxxxxxxx",
"object": "video",
"model": "grok-imagine-video-1.5-lite",
"status": "queued",
"progress": 0,
"seconds": "<string>",
"created_at": 123,
"completed_at": 123
}Grok Imagine 影片生成 API 參考
Grok Imagine 影片生成 API 參考與線上除錯 — 文生影片 / 圖生影片,非同步任務三步流程,1–15 秒,480p / 720p / 1080p,與官網同價按秒計費
curl --request POST \
--url https://api.apiyi.com/v1/videos \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"model": "grok-imagine-video-1.5-lite"
}
'import requests
url = "https://api.apiyi.com/v1/videos"
payload = { "model": "grok-imagine-video-1.5-lite" }
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-video-1.5-lite'})
};
fetch('https://api.apiyi.com/v1/videos', 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/videos",
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-video-1.5-lite'
]),
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/videos"
payload := strings.NewReader("{\n \"model\": \"grok-imagine-video-1.5-lite\"\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/videos")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"model\": \"grok-imagine-video-1.5-lite\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.apiyi.com/v1/videos")
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-video-1.5-lite\"\n}"
response = http.request(request)
puts response.read_body{
"id": "task_xxxxxxxxxxxxxxxx",
"object": "video",
"model": "grok-imagine-video-1.5-lite",
"status": "queued",
"progress": 0,
"seconds": "<string>",
"created_at": 123,
"completed_at": 123
}GrokOfficial 分組:Grok Imagine 影片不在 Default 預設分組,需申請開通後才能呼叫(含本頁 Playground)。未開通、或令牌計費模式為「按次計費」時,呼叫固定返回 503。申請流程見 Grok Imagine 影片概覽 · 分組介紹。Bearer sk-xxx),選擇 model、填寫 prompt / seconds / size 後傳送;需要圖生影片時再填 input_reference。Playground 只完成第 1 步提交,拿到任務 id 後按下方流程輪詢與下載。- 不傳
size/seconds預設 480p、8 秒,並按此計費。 grok-imagine-video不支援 1080p,傳1920x1080返回 400;1080p 請用-1.5或-1.5-lite。- 稽核攔截照常計費:被攔截的任務狀態仍是
completed,但帶error.code: "moderation_blocked"且沒有成片。
- 第 1 步(本頁):
POST /v1/videos→ 返回任務id+status: "queued" - 第 2 步:
GET /v1/videos/{id}輪詢,直到status: "completed"或"failed" - 第 3 步:
GET /v1/videos/{id}/content下載 MP4
程式碼示例
Python(文生影片,完整三步)
import os, time, requests
BASE = "https://api.apiyi.com/v1"
H = {"Authorization": f"Bearer {os.environ['APIYI_API_KEY']}"}
# 第 1 步:提交
task = requests.post(f"{BASE}/videos", headers=H, json={
"model": "grok-imagine-video-1.5",
"prompt": "黃昏海邊的燈塔,鏡頭緩慢推進,海浪輕拍礁石,海鳥叫聲,電影級光影",
"seconds": "5",
"size": "1280x720", # 720p 橫屏;不傳預設 480p
}, timeout=60).json()
vid = task["id"]
# 第 2 步:輪詢
for _ in range(75): # 最多約 10 分鐘
time.sleep(8)
job = requests.get(f"{BASE}/videos/{vid}", headers=H, timeout=60).json()
if job["status"] in ("completed", "failed"):
break
if job["status"] != "completed" or job.get("error"):
# moderation_blocked:被稽核攔截,無成片但已計費
raise RuntimeError(job.get("error"))
# 第 3 步:下載並立即轉存
mp4 = requests.get(f"{BASE}/videos/{vid}/content", headers=H, timeout=300)
open("out.mp4", "wb").write(mp4.content)
Python(圖生影片,base64 輸入圖)
import base64, os, requests
H = {"Authorization": f"Bearer {os.environ['APIYI_API_KEY']}"}
img = base64.b64encode(open("first-frame.jpg", "rb").read()).decode()
task = requests.post("https://api.apiyi.com/v1/videos", headers=H, json={
"model": "grok-imagine-video",
"prompt": "the cat slowly turns its head and blinks",
"seconds": "3",
"input_reference": f"data:image/jpeg;base64,{img}", # 也可直接傳公網圖片 URL
}, timeout=60).json()
print(task["id"]) # 之後同樣輪詢 + 下載;輸出畫幅跟隨輸入圖
cURL
# 第 1 步:提交文生影片(1.5-lite,2 秒 480p)
curl -X POST "https://api.apiyi.com/v1/videos" \
-H "Authorization: Bearer sk-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "grok-imagine-video-1.5-lite",
"prompt": "a red paper boat drifting on a calm lake, gentle ripples",
"seconds": "2",
"size": "854x480"
}'
# 圖生影片也可以用 multipart 上傳本地檔案
curl -X POST "https://api.apiyi.com/v1/videos" \
-H "Authorization: Bearer sk-your-api-key" \
-F "model=grok-imagine-video" \
-F "prompt=the cat slowly turns its head" \
-F "seconds=2" \
-F "[email protected]"
# 第 2 步:查詢狀態
curl "https://api.apiyi.com/v1/videos/task_xxxxxxxx" \
-H "Authorization: Bearer sk-your-api-key"
# 第 3 步:下載成片
curl -o out.mp4 "https://api.apiyi.com/v1/videos/task_xxxxxxxx/content" \
-H "Authorization: Bearer sk-your-api-key"
Node.js(原生 fetch)
import fs from 'node:fs';
const BASE = 'https://api.apiyi.com/v1';
const H = { 'Authorization': `Bearer ${process.env.APIYI_API_KEY}`, 'Content-Type': 'application/json' };
const task = await fetch(`${BASE}/videos`, {
method: 'POST',
headers: H,
body: JSON.stringify({
model: 'grok-imagine-video-1.5-lite',
prompt: 'A serene Japanese garden with cherry blossoms drifting, koi pond, golden hour',
seconds: '4',
size: '720x1280' // 720p 豎屏
})
}).then(r => r.json());
let job;
for (let i = 0; i < 75; i++) {
await new Promise(r => setTimeout(r, 8000));
job = await fetch(`${BASE}/videos/${task.id}`, { headers: H }).then(r => r.json());
if (['completed', 'failed'].includes(job.status)) break;
}
if (job.status !== 'completed' || job.error) throw new Error(JSON.stringify(job.error));
const mp4 = await fetch(`${BASE}/videos/${task.id}/content`, { headers: H });
fs.writeFileSync('out.mp4', Buffer.from(await mp4.arrayBuffer()));
引數說明速查
| 引數名 | 型別 | 必填 | 預設 | 說明 |
|---|---|---|---|---|
model | string | ✅ | — | grok-imagine-video / grok-imagine-video-1.5 / grok-imagine-video-1.5-lite |
prompt | string | 文生必填 | — | 場景、主體、動作、鏡頭、光影;圖生影片用來描述動作 |
seconds | string / integer | ❌ | 8 | 1–15 秒,超出返回 400 |
size | string | ❌ | 480p | 寬x高 畫素寫法,短邊定解析度、寬高比取最近一檔;也可傳 480p / 720p / 1080p |
resolution / aspect_ratio | string | ❌ | — | xAI 原生寫法,與 size 同時傳時以它們為準 |
input_reference | string / file | ❌ | — | 圖生影片輸入圖:公網 URL、Data URI,或 multipart 檔案 |
| 模型 | 480p | 720p | 1080p | 輸入圖 |
|---|---|---|---|---|
grok-imagine-video | $0.05 | $0.07 | 不支援 | $0.002 / 張 |
grok-imagine-video-1.5 | $0.08 | $0.14 | $0.25 | $0.01 / 張 |
grok-imagine-video-1.5-lite | $0.02 | $0.03 | $0.14 | $0.01 / 張 |
響應示例
提交(第 1 步):{
"id": "task_xxxxxxxxxxxxxxxx",
"object": "video",
"model": "grok-imagine-video-1.5-lite",
"status": "queued",
"progress": 0,
"created_at": 1792320668
}
{
"id": "task_xxxxxxxxxxxxxxxx",
"object": "video",
"model": "grok-imagine-video-1.5-lite",
"status": "completed",
"progress": 100,
"seconds": "2",
"created_at": 1792320668,
"completed_at": 1792320715
}
status 仍為 completed,但多一個 error 欄位、沒有可下載的成片:
{
"id": "task_xxxxxxxxxxxxxxxx",
"status": "completed",
"error": { "code": "moderation_blocked", "message": "..." }
}
相關文件
- Grok Imagine 影片概覽 - 定價、分組申請、引數詳解、常見問題
- Grok Imagine 官轉生圖 - 同一分組的圖片生成
授權
在 API易控制台获取的 API Key(令牌分组需包含 GrokOfficial)
主體
模型 ID(按秒 × 分辨率单价计费):
grok-imagine-video—— 480p $0.05/秒、720p $0.07/秒,不支持 1080pgrok-imagine-video-1.5—— 480p $0.08、720p $0.14、1080p $0.25 每秒grok-imagine-video-1.5-lite—— 480p $0.02、720p $0.03、1080p $0.14 每秒
grok-imagine-video, grok-imagine-video-1.5, grok-imagine-video-1.5-lite 提示词:场景 + 主体 + 动作 + 镜头 + 光影。文生视频必填;图生视频用来描述动作与镜头
"a red paper boat drifting on a calm lake, gentle ripples, soft morning light"
视频时长 1–15 秒,字符串或数字均可,默认 8。超出范围返回 400。费用与秒数成正比,测试建议 1–2 秒。
"2"
输出尺寸,宽x高 像素写法:按短边换算分辨率(≥1080 为 1080p、≥720 为 720p、其余 480p),宽高比取最接近的一档。
也可直接传 480p / 720p / 1080p。不传时默认 480p。
854x480—— 480p 横屏(实测 848×480)1280x720/720x1280—— 720p 横 / 竖屏1920x1080/1080x1920—— 1080p(仅 1.5 / 1.5-lite)
854x480, 480x854, 1280x720, 720x1280, 1920x1080, 1080x1920 图生视频的输入图:公网图片 URL,或 data:image/jpeg;base64,... 形式的 Data URI。传了即为图生视频,输出画幅跟随输入图。
每张输入图另收 $0.002(grok-imagine-video)或 $0.01(1.5 / 1.5-lite)。
"https://example.com/first-frame.jpg"
回應
任务已提交,返回任务 id 与 queued 状态
任务 ID,用于轮询和下载
"task_xxxxxxxxxxxxxxxx"
固定 video
"video"
"grok-imagine-video-1.5-lite"
任务状态:
queued—— 已提交,排队中in_progress—— 生成中completed—— 完成,可通过/v1/videos/{id}/content下载;若带error.code: moderation_blocked则为审核拦截、无成片failed—— 失败
queued, in_progress, completed, failed "queued"
进度 0–100,粗粒度,不建议做精确进度条
0
成片时长,完成后返回
任务创建 Unix 时间戳(秒)
任务完成 Unix 时间戳(秒),完成后返回
這個頁面有幫助嗎?