執行工作
一個工作會對一或多個已上傳的檔案執行一種操作。工作會立即啟動、在背景執行,並回報每個檔案的處理結果。
本頁內容
操作就是工作要做的事,例如 video.compress。GET /v1/operations 會列出所有操作,GET /v1/operations/{id} 則會傳回單一操作及其設定 schema。
curl https://api.filesokay.com/v1/operations/video.compressconst res = await fetch("https://api.filesokay.com/v1/operations/video.compress");
const operation = await res.json();import requests
res = requests.get(
"https://api.filesokay.com/v1/operations/video.compress",
)
operation = res.json()每個操作都會說明它接受什麼,以及如何運作:
| 欄位 | 說明 |
|---|---|
input_formats | 接受的副檔名。若檔案使用其他副檔名,但您為它傳送的 type 屬於該操作處理的類型(例如 video/mp4),也會被接受。 |
max_files | 單一工作最多可接受的輸入檔案數。 |
limits.max_file_bytes | 輸入檔案的大小上限,以位元組為單位。 |
grouping | separate:每個檔案一個任務,各自產生結果。together:所有檔案共用一個任務,例如合併。 |
settings_schema | 設定所遵循的 JSON Schema,並附有每項設定的預設值。 |
傳送操作、輸入檔案的 ID,並視需要附上操作設定和您自己的參考編號。也請傳送 Idempotency-Key,這樣重試時絕不會啟動第二個工作。
curl -X POST https://api.filesokay.com/v1/jobs \
-H "Authorization: Bearer $FILESOKAY_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: $(uuidgen)" \
-d '{
"operation": "video.compress",
"inputs": [
{
"file_id": "file_3cf0737aefa34b89b7b4443c9467d60c"
}
],
"settings": {
"method": "target_size",
"target": {
"unit": "percent",
"value": 50
}
},
"client_reference": "lecture-0412"
}'const res = await fetch("https://api.filesokay.com/v1/jobs", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.FILESOKAY_KEY}`,
"Content-Type": "application/json",
"Idempotency-Key": crypto.randomUUID(),
},
body: JSON.stringify({
operation: "video.compress",
inputs: [
{
file_id: "file_3cf0737aefa34b89b7b4443c9467d60c"
}
],
settings: {
method: "target_size",
target: {
unit: "percent",
value: 50
}
},
client_reference: "lecture-0412"
}),
});
const job = await res.json();import os
import uuid
import requests
res = requests.post(
"https://api.filesokay.com/v1/jobs",
headers={
"Authorization": f"Bearer {os.environ['FILESOKAY_KEY']}",
"Idempotency-Key": str(uuid.uuid4()),
},
json={
"operation": "video.compress",
"inputs": [
{
"file_id": "file_3cf0737aefa34b89b7b4443c9467d60c"
}
],
"settings": {
"method": "target_size",
"target": {
"unit": "percent",
"value": 50
}
},
"client_reference": "lecture-0412"
},
)
job = res.json(){
"id": "job_a1c9531eeb00434aba0058796f1ca517",
"operation": "video.compress",
"status": "waiting",
"client_reference": "lecture-0412",
"settings": {
"audio": "keep",
"codec": "same",
"method": "target_size",
"target": {
"unit": "percent",
"value": 50
},
"frame_rate": "original",
"resolution": "auto",
"output_format": "same"
},
"created_at": "2026-12-03T09:23:14Z",
"reserved_credits": 2,
"charged_credits": 0,
"tasks": [
{
"id": "task_3f648f64472b4ea4b88568eea9d4f88c",
"status": "waiting",
"progress": 0,
"reserved_credits": 2,
"inputs": [
{
"file_id": "file_3cf0737aefa34b89b7b4443c9467d60c",
"name": "lecture.mp4"
}
],
"outputs": []
}
]
}settings會依操作的 schema 進行檢查。未提供的設定會採用預設值,工作中會顯示所有設定,並填入預設值。tasks依您提供的順序,為每個輸入檔案列出一個任務。reserved_credits是這個工作最多可能花費的點數。實際只扣除用量:請參閱點數。
POST /v1/estimates 接受與 POST /v1/jobs 相同的本文,但不會執行任何處理。它會回應工作最多可能花費的點數、您擁有的點數、還差多少點,以及每個檔案會變成什麼樣子。
curl -X POST https://api.filesokay.com/v1/estimates \
-H "Authorization: Bearer $FILESOKAY_KEY" \
-H "Content-Type: application/json" \
-d '{
"operation": "video.compress",
"inputs": [
{
"file_id": "file_3cf0737aefa34b89b7b4443c9467d60c"
}
],
"settings": {
"method": "target_size",
"target": {
"unit": "percent",
"value": 50
}
}
}'const res = await fetch("https://api.filesokay.com/v1/estimates", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.FILESOKAY_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
operation: "video.compress",
inputs: [
{
file_id: "file_3cf0737aefa34b89b7b4443c9467d60c"
}
],
settings: {
method: "target_size",
target: {
unit: "percent",
value: 50
}
}
}),
});
const estimate = await res.json();import os
import requests
res = requests.post(
"https://api.filesokay.com/v1/estimates",
headers={
"Authorization": f"Bearer {os.environ['FILESOKAY_KEY']}",
},
json={
"operation": "video.compress",
"inputs": [
{
"file_id": "file_3cf0737aefa34b89b7b4443c9467d60c"
}
],
"settings": {
"method": "target_size",
"target": {
"unit": "percent",
"value": 50
}
}
},
)
estimate = res.json(){
"available_credits": 1908,
"operation": "video.compress",
"reserved_credits": 2,
"settings": {
"method": "target_size",
"target": {
"unit": "percent",
"value": 50
},
"resolution": "auto",
"codec": "same",
"frame_rate": "original",
"audio": "keep",
"output_format": "same"
},
"shortfall": 0,
"tasks": [
{
"status": "waiting",
"inputs": [
{
"file_id": "file_3cf0737aefa34b89b7b4443c9467d60c",
"name": "lecture.mp4"
}
],
"reserved_credits": 2,
"preview": {
"output": {
"format": "mp4",
"extension": "mp4",
"file_name": "lecture-compressed.mp4"
},
"video": {
"codec": "h264",
"width": 1280,
"height": 720,
"frame_rate": "original",
"hdr": "none"
},
"smallest_bytes": 634179
}
}
]
}| 工作狀態 | 說明 |
|---|---|
waiting | 沒有任務正在執行,且至少有一個任務仍在等待開始。 |
processing | 至少有一個任務正在執行。 |
completed | 除了已取消的任務以外,所有任務都已完成。 |
partly_failed | 部分任務完成,部分任務失敗。 |
failed | 除了已取消的任務以外,所有任務都失敗。 |
cancelled | 在任何任務完成之前就已取消。 |
每個任務都有各自的 status:waiting、processing、completed、failed、cancelled 或 skipped。執行中的任務會顯示 progress,範圍為 0 至 100。失敗或略過的任務會提供 reason。
一個工作可以對多個檔案執行操作,上限為該操作的 max_files。採用 separate 分組時,每個檔案都是獨立的任務:某個檔案失敗不會影響其他檔案,而且失敗的檔案不會扣點。
無法使用的檔案會被略過並附上原因,其餘檔案照常執行。略過的檔案不會花費任何點數。原因如下:
| 原因 | 說明 |
|---|---|
not_found | 沒有任何已上傳的檔案使用這個 ID。 |
not_ready | 檔案的上傳尚未完成。 |
refused | 檔案檢查拒絕了這個檔案。 |
check_failed | 檔案檢查因我們這邊的問題而失敗。 |
other_operation | 檔案是為其他操作上傳的。 |
file_in_use | 另一個工作正在使用這個檔案。 |
repeated | 這個檔案在此工作中列出了兩次。 |
如果在目前的設定下,操作不會讓某個檔案有任何改變,也可能略過該檔案,例如影片已經小於目標大小。若沒有任何檔案會執行,工作就不會啟動:要求會以 nothing_to_run 拒絕,並附上每個檔案的原因。採用 together 分組的操作會將所有檔案合成一個結果,這類操作則會改以 files_unusable 拒絕整個工作。
client_reference 用來存放您為工作自訂的 ID,例如訂單編號,最多 255 個字元。它會隨工作一起傳回,也會出現在每則 Webhook 訊息中,而且可以用 GET /v1/jobs?client_reference=… 依此找到工作。它不必是唯一值。
POST /v1/jobs/{id}/cancel 會停止仍在等待或執行中的任務。尚未開始的任務不會花費任何點數;已開始的任務則依實際執行時間扣點。取消已結束的工作不會有任何影響。
curl -X POST https://api.filesokay.com/v1/jobs/job_a1c9531eeb00434aba0058796f1ca517/cancel \
-H "Authorization: Bearer $FILESOKAY_KEY"const res = await fetch("https://api.filesokay.com/v1/jobs/job_a1c9531eeb00434aba0058796f1ca517/cancel", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.FILESOKAY_KEY}`,
},
});
const job = await res.json();import os
import requests
res = requests.post(
"https://api.filesokay.com/v1/jobs/job_a1c9531eeb00434aba0058796f1ca517/cancel",
headers={
"Authorization": f"Bearer {os.environ['FILESOKAY_KEY']}",
},
)
job = res.json()