> ## 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の一部を分析して最も重要なキーワードを識別し、より簡潔な候補Promptを5つ生成することです。このインターフェースは非常に便利です：

* `imagine` 呼び出し前に長いPromptを短縮し、出力画像の関連性を向上させるために
* 候補Promptを通じてMidjourneyが各トークンの重み付けを逆に理解し、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呼び出し時にエラーが発生した場合、対応するエラーコードとメッセージが返されます。例：

* `400 token_mismatched`：リクエスト不正、パラメータ不足または無効
* `400 api_not_implemented`：リクエスト不正、未実装
* `401 invalid_token`：認証失敗、トークン無効または未提供
* `429 too_many_requests`：リクエスト過多、レートリミット超過
* `500 api_error`：サーバー内部エラー

### エラー応答例

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

## まとめ

本ドキュメントを通じて、Midjourney Shorten APIを使ったPromptの分析と簡素化方法を理解いただけたと思います。このAPIは、Midjourney Imagine APIと併用することを推奨します。まずShorten APIで複数の候補を取得し、その中から最適なものを選び、Imagine APIに渡して画像生成を行います。ご不明点があれば、いつでもサポートチームまでご連絡ください。
