> ## 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.

# 人像動漫化 API 對接說明

> Face Transformation 集成指南 - XHuoAPI

本文將介紹一種 人像動漫化 API 對接說明，它可以通過輸入一張人臉照片，生成個性化的二次元動漫形象，可用於打造個性頭像、趣味活動、特效類應用等場景，提升社交娛樂的體驗。

## 申請流程

要使用 API，需要先到 [人像動漫化 API](https://api.xhuoapi.ai/documents/6b6313f1-f398-4d29-933d-5a652cfa1c37) 對應頁面申請對應的服務，進入頁面之後，點擊「Acquire」按鈕，如圖所示：

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

如果你尚未登錄或註冊，會自動跳轉到登錄頁面邀請您來註冊和登錄，登錄註冊之後會自動返回當前頁面。

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

## 基本使用

首先先了解下基本的使用方式，就是輸入圖片鏈接，便可獲得處理後結果圖片，首先需要簡單地傳遞一個 `image_url` 字段，人臉圖片如下圖所示：

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

我們接下來就可以在界面上填寫對應的內容，如圖所示：

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

可以看到這裡我們設置了 Request Headers，包括：

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

另外設置了 Request Body，包括：

* `image_url`：需要處理的人臉圖片鏈接。

選擇之後，可以發現右側也生成了對應代碼，如圖所示：

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

點擊「Try」按鈕即可進行測試，如上圖所示，這裡我們就得到了如下結果：

```json theme={null}
{
  "image_url": "https://faceeffect-1254418846.cos.ap-guangzhou.myqcloud.com/ft/FaceCartoonPic/1256437459/8567833b-7831-406c-9ff5-890e5f7e020e"
}
```

可以看到，這裡返回的結果中有一個 `image_url` 字段，就是變化後的卡通人臉圖片。其中變化後人臉信息如下所示：

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

可以看到圖片中人臉已經生成個性化的二次元動漫形象。

另外如果想生成對應的對接代碼，可以直接複製生成，例如 CURL 的代碼如下：

```shell theme={null}
curl -X POST 'https://api.xhuoapi.ai/v1/face/cartoon' \
-H 'accept: application/json' \
-H 'authorization: Bearer {token}' \
-H 'content-type: application/json' \
-d '{
  "image_url": "https://cdn.xhuoapi.ai/lrbtcn.jpg"
}'
```

Python 的對接代碼如下：

```python theme={null}
import requests

url = "https://api.xhuoapi.ai/v1/face/cartoon"

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

payload = {
    "image_url": "https://cdn.xhuoapi.ai/lrbtcn.jpg"
}

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

## 錯誤處理

在調用 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.

### 錯誤響應示例

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

## 結論

通過本文檔，您已經了解了如何使用 人像動漫化 API 對輸入的圖片進行生成個性化的二次元動漫形象。希望本文檔能幫助您更好地對接和使用該 API。如有任何問題，請隨時聯繫我們的技術支持團隊。
