Files API
為 Rivya API 生成請求上傳圖片、影片或音訊參考檔案,並處理 MIME 檢查、大小限制和 duration tokens。
最近審閱於 2026/05/11
使用 POST /api/v1/files 上傳模型需要的圖片、影片或音訊輸入參考媒體。
Files API 僅用於參考輸入。它本身不會建立生成任務。上傳後,請將回傳的 url 和 metadata 放入模型 params,通常是透過 params.referenceMediaItems。
Endpoint
POST https://rivya.ai/api/v1/files
GET https://rivya.ai/api/v1/files/{fileId}必要 headers:
Authorization: Bearer rvya_sk_...
Content-Type: multipart/form-dataAPI key 必須包含 files:create scope 才能上傳,並包含 files:read 才能取回 metadata。新建立的 Rivya API keys 預設包含這兩個 scopes。
Multipart 欄位
| 欄位 | 類型 | 必填 | 說明 |
|---|---|---|---|
file | binary | yes | 要上傳的圖片、影片或音訊檔案。 |
kind | string | yes | image、video 或 audio 其中之一。 |
model | string | no | 公開模型 ID。提供時,Rivya 會驗證該模型是否接受這種檔案類型。 |
client_request_id | string | no | 你的 trace ID,最多 128 個字元。 |
如果檔案是要用於特定模型,請使用 model。這可讓你在檔案被接受前,先取得模型專屬的 MIME 和大小驗證。
上傳限制
Files API 使用與 Rivya 參考上傳相同的上傳政策。
預設限制:
| 類型 | 預設最大大小 | 常見 MIME 類型 |
|---|---|---|
image | 10 MB | image/jpeg, image/png, image/webp |
video | 50 MB | video/mp4, video/quicktime, video/webm |
audio | 10 MB | audio/mpeg, audio/mp4, audio/wav, audio/x-wav, audio/aac, audio/ogg |
有些模型有不同限制。例如,特定 reference-image 模型允許較大的圖片,特定 video-reference 模型允許檔案達到產品邊界內安全的上傳上限。當你知道目標模型時,請一律傳入 model,並在接受使用者上傳前閱讀 模型 API 參考。
Rivya 會驗證偵測到的檔案簽章,而不只看檔名副檔名。
curl 範例
curl https://rivya.ai/api/v1/files \
-H "Authorization: Bearer rvya_sk_..." \
-F "file=@./reference.png" \
-F "kind=image" \
-F "model=nano-banana-2" \
-F "client_request_id=asset-123"JavaScript 範例
import { readFile } from "node:fs/promises";
const form = new FormData();
const file = new Blob([await readFile("./reference.png")], {
type: "image/png"
});
form.set("file", file, "reference.png");
form.set("kind", "image");
form.set("model", "nano-banana-2");
form.set("client_request_id", "asset-123");
const response = await fetch("https://rivya.ai/api/v1/files", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.RIVYA_API_KEY}`
},
body: form
});
const uploadedFile = await response.json();
console.log(uploadedFile.id, uploadedFile.url);Python 範例
import os
import requests
with open("./reference.png", "rb") as file_handle:
response = requests.post(
"https://rivya.ai/api/v1/files",
headers={
"Authorization": f"Bearer {os.environ['RIVYA_API_KEY']}",
},
files={"file": ("reference.png", file_handle, "image/png")},
data={
"kind": "image",
"model": "nano-banana-2",
"client_request_id": "asset-123",
},
timeout=60,
)
uploaded_file = response.json()
print(uploaded_file["id"], uploaded_file["url"])回應
{
"id": "file_...",
"object": "file",
"kind": "image",
"file_name": "reference.png",
"mime_type": "image/png",
"size_bytes": 482314,
"url": "https://...",
"duration_seconds": null,
"duration_token": null,
"created_at": "2026-05-11T00:00:00.000Z",
"expires_at": null
}對於影片和音訊上傳,duration_seconds 可能會有值。當模型需要驗證時長時,請將 duration_token 複製到相關生成參數中的 durationToken。
取回檔案 Metadata
使用 GET /api/v1/files/{fileId} 讀取同一 Rivya 帳號擁有的檔案 metadata:
curl https://rivya.ai/api/v1/files/file_... \
-H "Authorization: Bearer rvya_sk_..."回應會使用與上傳相同的 PublicApiFile shape。如果檔案屬於另一個帳號或已不再可用,API 會回傳 not_found。
在生成 Params 中使用上傳結果
不要將頂層 files 欄位傳送到 POST /api/v1/generations。
對於新的整合,請透過 params.referenceMediaItems 傳遞上傳結果:
{
"model": "nano-banana-2",
"prompt": "Restyle this product photo for a clean editorial catalog page",
"params": {
"referenceMediaItems": [
{
"url": "https://...",
"kind": "image",
"name": "reference.png",
"mimeType": "image/png"
}
]
},
"client_request_id": "order-123-preview"
}對於影片或音訊:
{
"url": "https://...",
"kind": "video",
"name": "source.mov",
"mimeType": "video/quicktime",
"durationSeconds": 12.4,
"durationToken": "duration_token_from_files_api"
}有些較舊的模型參數仍使用模型專屬 URL 欄位。如果 模型 API 參考 記錄了特定參數,請依照該模型頁面,而不是自行發明新欄位。
錯誤
Files API 使用與 Rivya API 其他部分相同的公開錯誤 envelope:
{
"error": {
"code": "validation_failed",
"message": "The request is invalid.",
"requestId": "req_..."
}
}常見情況:
| HTTP | Code | 原因 |
|---|---|---|
| 400 | validation_failed | 缺少 file、不支援的 kind、不支援的 MIME 類型、檔案過大,或模型不接受所選類型。 |
| 401 | api_key_missing / api_key_invalid | 缺少或無效的 Bearer API key。 |
| 403 | api_scope_denied | key 未包含所要求動作需要的 files:create 或 files:read。 |
| 429 | rate_limited | 目前分鐘內檔案上傳次數過多。 |
| 503 | public_api_disabled | Public API 在目前環境中停用。 |
安全性注意事項
不要在瀏覽器、行動用戶端、日誌、analytics events 或截圖中儲存完整 API keys。
請將上傳的檔案 URL 和 duration_token 值視為暫時的整合材料。只用它們建立後續生成請求,不要暴露在公開頁面中。