跳轉到主要內容
Midjourney Tasks API 的主要功能是通過輸入 Midjourney Imagine API 或 Midjourney Describe API 生成的任務 ID 來查詢該任務的執行情況。 本文檔將詳細介紹 Midjourney Tasks API 的對接說明,幫助您輕鬆集成並充分利用該 API 的強大功能。通過 Midjourney Tasks API,您可以輕鬆實現查詢 Midjourney Imagine API 或 Midjourney Describe API 的任務執行情況。

申請流程

要使用 Midjourney Tasks API,需要先到 申請頁面 Midjourney Imagine API 申請相應的服務,然後複製 Imagine API 的任務 ID,如圖所示:

最後進入 Tasks API 頁面 Midjourney Tasks API 申請相應的服務,進入頁面之後,點擊「Acquire」按鈕,如圖所示: 申請頁面 如果您尚未登錄或註冊,會自動跳轉到 登錄頁面 邀請您來註冊和登錄,登錄註冊之後會自動返回當前頁面。 首次申請時會有免費額度贈送,可以免費使用該 API。

請求示例

Midjourney Tasks API 可以用於查詢 Midjourney Imagine API 和 Midjourney Describe API 兩個 API 的結果。關於怎樣使用 Midjourney Imagine API,請參考文檔 Midjourney Imagine API。關於怎樣使用 Midjourney Describe,請參考 Midjourney Describe API 我們以 Midjourney Imagine API 服務返回的任務 ID 一個為例,演示如何使用該 API。假設我們有一個任務 ID:7489df4c-ef03-4de0-b598-e9a590793434,接下來演示如何通過傳入一個任務 ID 來。

任務示例圖

設置請求頭和請求體

Request Headers 包括:
  • accept:指定接收 JSON 格式的響應結果,這裡填寫為 application/json
  • authorization:調用 API 的密鑰,申請之後可以直接下拉選擇。
Request Body 包括:
  • id:上傳的任務 ID。
  • ids:批量查詢時的任務 ID 陣列。
  • action:對任務的操作方式,支持 retrieve(單查)與 retrieve_batch(批量)。
設置如下圖所示:

代碼示例

可以發現,在頁面右側已經自動生成了各種語言的代碼,如圖所示:

部分代碼示例如下:

CURL

curl -X POST 'https://api.acedata.cloud/midjourney/tasks' \
-H 'accept: application/json' \
-H 'authorization: Bearer {token}' \
-H 'content-type: application/json' \
-d '{
  "id": "7489df4c-ef03-4de0-b598-e9a590793434",
  "action": "retrieve"
}'

Python

import requests

url = "https://api.acedata.cloud/midjourney/tasks"

headers = {
    "accept": "application/json",
    "authorization": "Bearer {token}",
    "content-type": "application/json"
}

payload = {
    "id": "7489df4c-ef03-4de0-b598-e9a590793434",
    "action": "retrieve"
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)

響應示例

請求成功後,API 將返回此處圖片任務的詳細信息。例如:
{
  "_id": "668aae3f550a4144a540803b",
  "id": "7489df4c-ef03-4de0-b598-e9a590793434",
  "application_id": "9dec7b2a-1cad-41ff-8536-d4ddaf2525d4",
  "created_at": 1720364607.967,
  "credential_id": "68253cc8-505d-47f4-97ad-0050a62e4975",
  "request": {
    "mode": "fast",
    "prompt": "A cat sitting on a table",
    "action": "generate"
  },
  "type": "imagine",
  "hold": false,
  "image_id": "1259525319472185344",
  "job_id": "da317da6-f500-48e6-bf32-dd48b3e6f84f",
  "response": {
    "image_url": "https://platform.cdn.acedata.cloud/midjourney/7489df4c-ef03-4de0-b598-e9a590793434.png?imageMogr2/thumbnail/!50p",
    "image_width": 1024,
    "image_height": 1024,
    "actions": [
      "upscale1",
      "upscale2",
      "upscale3",
      "upscale4",
      "reroll",
      "variation1",
      "variation2",
      "variation3",
      "variation4"
    ],
    "raw_image_url": "https://platform.cdn.acedata.cloud/midjourney/7489df4c-ef03-4de0-b598-e9a590793434.png",
    "raw_image_width": 2048,
    "raw_image_height": 2048,
    "progress": 100,
    "image_id": "1259525319472185344",
    "task_id": "7489df4c-ef03-4de0-b598-e9a590793434",
    "success": true,
    "job_id": "da317da6-f500-48e6-bf32-dd48b3e6f84f",
    "hold": false
  },
  "duration": 29.437000036239624,
  "finished_at": 1720364637.404
}
返回結果一共有多個字段,request 字段就是發起任務時的 request body,同時 response 字段是任務完成後返回的 response body,如果 type = imagine,其結果和 Midjourney Imagine API 的請求和返回一致,如果 type = describe,其結果和 Midjourney Describe API 的請求和返回一致。字段介紹如下。
  • id,生成此圖像任務的 ID,用於唯一標識此次圖像生成任務。
  • type,如果 type = imagine 代表是 Midjourney Imagine API 的結果,如果 type = describe 代表是 Midjourney Describe API 的結果。
  • job_id,生成此次查詢圖像任務的 ID,用於唯一標識此次查詢圖像任務。
  • image_id,此處查詢的圖片任務重圖片的唯一標識,在下次需要對圖片進行變換操作時需要傳此參數。
  • request,查詢圖片任務中的請求信息。
  • response,查詢圖片任務中的返回信息。

批量查詢操作

這是針對多個任務 ID 來進行查詢圖片任務詳情,與上面不同的是需要將 action 選中為 retrieve_batch。 Request Body 包括:
  • ids:上傳的任務 ID 陣列。
  • action:對任務的操作方式。
設置如下圖所示:

代碼示例

可以發現,在頁面右側已經自動生成了各種語言的代碼,如圖所示:

部分代碼示例如下:

響應示例

請求成功後,API 將返回此次所有批量圖片任務的具體詳情信息。例如:
{
  "items": [
    {
      "_id": "668aae3f550a4144a540803b",
      "id": "7489df4c-ef03-4de0-b598-e9a590793434",
      "application_id": "9dec7b2a-1cad-41ff-8536-d4ddaf2525d4",
      "created_at": 1720364607.967,
      "credential_id": "68253cc8-505d-47f4-97ad-0050a62e4975",
      "request": {
        "mode": "fast",
        "prompt": "A cat sitting on a table",
        "action": "generate"
      },
      "type": "imagine",
      "hold": false,
      "image_id": "1259525319472185344",
      "job_id": "da317da6-f500-48e6-bf32-dd48b3e6f84f",
      "response": {
        "image_url": "https://platform.cdn.acedata.cloud/midjourney/7489df4c-ef03-4de0-b598-e9a590793434.png?imageMogr2/thumbnail/!50p",
        "image_width": 1024,
        "image_height": 1024,
        "actions": [
          "upscale1",
          "upscale2",
          "upscale3",
          "upscale4",
          "reroll",
          "variation1",
          "variation2",
          "variation3",
          "variation4"
        ],
        "raw_image_url": "https://platform.cdn.acedata.cloud/midjourney/7489df4c-ef03-4de0-b598-e9a590793434.png",
        "raw_image_width": 2048,
        "raw_image_height": 2048,
        "progress": 100,
        "image_id": "1259525319472185344",
        "task_id": "7489df4c-ef03-4de0-b598-e9a590793434",
        "success": true,
        "job_id": "da317da6-f500-48e6-bf32-dd48b3e6f84f",
        "hold": false
      },
      "duration": 29.437000036239624,
      "finished_at": 1720364637.404
    },
    {
      "_id": "668b41d6550a4144a551d996",
      "id": "807f62de-c63e-4add-8345-7f0ae6dd18e7",
      "application_id": "9dec7b2a-1cad-41ff-8536-d4ddaf2525d4",
      "created_at": 1720402390.341,
      "credential_id": "6fd3e1d5-4bd6-47e8-8872-fab89a183b53",
      "request": {
        "mode": "fast",
        "prompt": "A beautiful girl",
        "action": "generate"
      },
      "type": "imagine",
      "hold": false,
      "image_id": "1259683790612070400",
      "job_id": "ede5c805-e231-498c-8f74-3aa76d5d6d12",
      "response": {
        "image_url": "https://platform.cdn.acedata.cloud/midjourney/807f62de-c63e-4add-8345-7f0ae6dd18e7.png?imageMogr2/thumbnail/!50p",
        "image_width": 1024,
        "image_height": 1024,
        "actions": [
          "upscale1",
          "upscale2",
          "upscale3",
          "upscale4",
          "reroll",
          "variation1",
          "variation2",
          "variation3",
          "variation4"
        ],
        "raw_image_url": "https://platform.cdn.acedata.cloud/midjourney/807f62de-c63e-4add-8345-7f0ae6dd18e7.png",
        "raw_image_width": 2048,
        "raw_image_height": 2048,
        "progress": 100,
        "image_id": "1259683790612070400",
        "task_id": "807f62de-c63e-4add-8345-7f0ae6dd18e7",
        "success": true,
        "job_id": "ede5c805-e231-498c-8f74-3aa76d5d6d12",
        "hold": false
      },
      "duration": 29.471999883651733,
      "finished_at": 1720402419.813
    }
  ],
  "count": 2
}
返回結果一共有多個字段,其中items是包含了批量圖片任務的具體詳情信息,每個圖片任務的具體信息與上文的字段一樣,字段信息如下。
  • items,批量圖片任務的所有具體詳情信息。它是一個數組,每個數組的元素和上文查詢單個任務的返回結果格式是一樣的。
  • count,此處批量查詢圖片任務的個數。

CURL

curl -X POST 'https://api.acedata.cloud/midjourney/tasks' \
-H 'accept: application/json' \
-H 'authorization: Bearer {token}' \
-H 'content-type: application/json' \
-d '{
  "action": "retrieve_batch",
  "id": "",
  "ids": ["7489df4c-ef03-4de0-b598-e9a590793434","807f62de-c63e-4add-8345-7f0ae6dd18e7"]
}'

Python

import requests

url = "https://api.acedata.cloud/midjourney/tasks"

headers = {
    "accept": "application/json",
    "authorization": "Bearer {token}",
    "content-type": "application/json"
}

payload = {
    "action": "retrieve_batch",
    "id": "",
    "ids": ["7489df4c-ef03-4de0-b598-e9a590793434","807f62de-c63e-4add-8345-7f0ae6dd18e7"]
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)

錯誤處理

在調用 API 時,如果遇到錯誤,API 會返回相應的錯誤代碼和信息。例如:
  • 400 token_mismatched:Bad request, possibly due to missing or invalid parameters.
  • 400 api_not_implemented:Bad request, possibly due to missing or invalid parameters.
  • 401 invalid_token:Unauthorized, invalid or missing authorization token.
  • 429 too_many_requests:Too many requests, you have exceeded the rate limit.
  • 500 api_error:Internal server error, something went wrong on the server.

錯誤響應示例

{
  "success": false,
  "error": {
    "code": "api_error",
    "message": "fetch failed"
  },
  "trace_id": "2cf86e86-22a4-46e1-ac2f-032c0f2a4e89"
}

結論

通過本文檔,您已經了解了如何使用 Midjourney Tasks API 進行查詢單個或批量圖片任務的所有具體詳情信息。希望本文檔能幫助您更好地對接和使用該 API。如有任何問題,請隨時聯繫我們的技術支持團隊。