> ## 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 Translate API 的對接和使用

> Midjourney generation 集成指南 - XHuoAPI

Midjourney Translate API 的主要功能是透過輸入中文描述詞content來獲取英文的描述詞content。

本文檔將詳細介紹 Midjourney Translate API 的對接說明，幫助您輕鬆集成並充分利用該 API 的強大功能。透過 Midjourney Translate API，您可以輕鬆實現將中文描述詞content轉換為英文的描述詞content。

## 申請流程

要使用 Midjourney Translate API，需要先到 申請頁面 [Midjourney Translate API ](https://api.xhuoapi.ai/documents/e067d19b-7a66-4458-a45f-0fe88c1d5d34)申請相應的服務，進入頁面之後，點擊「Acquire」按鈕，如圖所示：

![申請頁面](https://cdn.xhuoapi.ai/rci31i.png)

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

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

## 請求示例

我們以一個中文描述詞為例，演示如何使用該 API。假設中文描述詞為：精緻，無暇，潔白的天使，接下來演示如何上傳中文描述詞並獲取英文描述詞。

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

**Request Headers** 包括：

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

**Request Body** 包括：

* `content`：上傳的中文描述詞。

> 僅此一個字段即可完成翻譯，請確保 `content` 使用中文描述詞。

設置如下圖所示：

![](https://cdn.xhuoapi.ai/db8lrn.png)

### 代碼示例

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

<p>
  <img src="https://cdn.xhuoapi.ai/oj1cck.png" width="500" className="m-auto" />
</p>

部分代碼示例如下：

#### CURL

```bash theme={null}
curl -X POST 'https://api.xhuoapi.ai/v1/midjourney/translate' \
-H 'accept: application/json' \
-H 'authorization: Bearer {token}' \
-H 'content-type: application/json' \
-d '{
  "content": "精緻，無暇，潔白的天使"
}'
```

#### Python

```python theme={null}
import requests

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

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

payload = {
    "content": "精緻，無暇，潔白的天使"
}

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

### 響應示例

請求成功後，API 將返回中文描述詞翻譯後的 1 條描述信息。例如：

```json theme={null}
{
  "content": "Exquisite, flawless, pure white angel"
}
```

可以看到，結果中有一個 `content` 字段，裡面包含了翻譯後的英文描述詞，它是對應中文描述詞的翻譯結果。

* `content`，生成對應的英文描述詞，可以用於圖像生成任務的描述詞。

## 錯誤處理

在調用 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 Translate API 實現將上傳的中文描述詞翻譯為英文的描述詞。希望本文檔能幫助您更好地對接和使用該 API。如有任何問題，請隨時聯繫我們的技術支持團隊。
