> ## 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, 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 Shorten API 对 Prompt 进行分析与精简。建议将该接口与 Midjourney Imagine API 结合使用：先用 Shorten API 获取多条精简候选，再选择最合适的一条传入 Imagine API 进行出图。如有任何问题，请随时联系我们的技术支持团队。
