> ## 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를 생성하는 것입니다. 이 API는 다음과 같은 경우에 매우 적합합니다:

* `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 호출 시 오류가 발생하면, 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에 전달해 이미지 생성을 진행합니다. 궁금한 점이 있으시면 언제든지 기술 지원팀에 문의해 주시기 바랍니다.
