> ## Documentation Index
> Fetch the complete documentation index at: https://docs.xhuoapi.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Midjourney Shorten API 的對接和使用

> Midjourney generation 集成指南 - XHuoAPI

Midjourney Shorten API 的主要功能是接入 Midjourney 官方的 `/shorten`（Prompt 分析）指令，對一段 Prompt 進行分析，識別其中權重最高的關鍵詞，並生成 5 條更精簡的候選 Prompt。該介面非常適合：

* 在調用 `imagine` 之前對長 Prompt 進行瘦身，提高出圖相關性；
* 通過候選 Prompt 反向理解 Midjourney 對各個 token 的權重，便於 Prompt 工程；
* 配合自動化流水線，對用戶輸入的 Prompt 做關鍵詞歸併和精簡。

本文檔將詳細介紹 Midjourney Shorten API 的對接說明，幫助您輕鬆集成該 API。

## 申請流程

要使用 Midjourney Shorten API，需要先到申請頁面 [Midjourney Shorten API](https://api.xhuoapi.ai/documents/f0c6be1d-c084-43ac-bde6-44ef9cf824a2) 申請相應的服務，進入頁面之後，點擊「Acquire」按鈕。

如果您尚未登入或註冊，會自動跳轉到[登入頁面](https://api.xhuoapi.ai)邀請您來註冊和登入，登入註冊之後會自動返回當前頁面。

首次申請時會有免費額度贈送，可以免費使用該 API。

## 請求示例

我們以一段較長的 Prompt 為例，演示如何使用該 API 對其進行分析與精簡。

### 設置請求頭和請求體

**Request Headers** 包括：

* `accept`：指定接收 JSON 格式的響應結果，這裡填寫為 `application/json`。
* `authorization`：調用 API 的密鑰，申請之後可以直接下拉選擇。

**Request Body** 包括：

* `prompt`：要分析與精簡的 Prompt 文字，建議輸入英文。

### 代碼示例

#### CURL

```bash theme={null}
curl -X POST 'https://api.xhuoapi.ai/v1/midjourney/shorten' \
-H 'accept: application/json' \
-H 'authorization: Bearer {token}' \
-H 'content-type: application/json' \
-d '{
  "prompt": "a serene mountain lake at sunrise, mist rising from the water, towering pine trees on the shore, golden hour lighting, ultra detailed, cinematic, 35mm film photography style, masterpiece --ar 16:9 --v 6"
}'
```

#### Python

```python theme={null}
import requests

url = "https://api.xhuoapi.ai/v1/midjourney/shorten"

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

payload = {
    "prompt": (
        "a serene mountain lake at sunrise, mist rising from the water, "
        "towering pine trees on the shore, golden hour lighting, ultra "
        "detailed, cinematic, 35mm film photography style, masterpiece "
        "--ar 16:9 --v 6"
    )
}

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

### 響應示例

請求成功後，API 將返回最多 5 條精簡後的候選 Prompt。例如：

```json theme={null}
{
  "prompts": [
    "a serene mountain lake at sunrise, mist rising from the water, golden hour lighting --ar 16:9",
    "mountain lake sunrise with mist, golden light --ar 16:9 --v 6",
    "tranquil alpine lake, dawn mist, warm golden tones, cinematic --ar 16:9",
    "sunrise over a misty mountain lake, rich golden hour photography --ar 16:9 --style raw",
    "misty lake at dawn, mountains in background, golden sunrise --ar 16:9"
  ]
}
```

可以看到，結果中有一個 `prompts` 欄位，裡面包含了若干個精簡候選 Prompt，每條都保留了 Midjourney 內部權重最高的關鍵詞，並去掉了重複或冗餘的描述。

## 錯誤處理

在調用 API 時，如果遇到錯誤，API 會返回相應的錯誤代碼和資訊。例如：

* `400 token_mismatched`：Bad request，可能由於缺少或無效參數。
* `400 api_not_implemented`：Bad request，可能由於缺少或無效參數。
* `401 invalid_token`：Unauthorized，無效或缺少授權令牌。
* `429 too_many_requests`：Too many requests，您已超出速率限制。
* `500 api_error`：Internal server error，伺服器發生錯誤。

### 錯誤響應示例

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

## 結論

通過本文檔，您已經了解了如何使用 Midjourney Shorten API 對 Prompt 進行分析與精簡。建議將該介面與 Midjourney Imagine API 結合使用：先用 Shorten API 獲取多條精簡候選，再選擇最合適的一條傳入 Imagine API 進行出圖。如有任何問題，請隨時聯繫我們的技術支援團隊。
