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

# hCaptcha 图像识别 API 对接说明

> hCaptcha verification code recognition service 集成指南 - XHuoAPI

本文将介绍一种 hCaptcha 图像识别 API 对接说明，它可以通过用户输入识别的内容和 hCaptcha验证码图像，最后返回需要点击的小图像的坐标，完成验证。

## 申请流程

要使用 API，需要先到 [hCaptcha 图像识别 API](https://api.xhuoapi.ai/documents/c9a5cc2f-e410-425f-857a-f8700205da6c) 对应页面申请对应的服务，进入页面之后，点击「Acquire」按钮，如图所示：

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

如果你尚未登录或注册，会自动跳转到登录页面邀请您来注册和登录，登录注册之后会自动返回当前页面。

在首次申请时会有免费额度赠送，可以免费使用该 API。

## 基本使用

首先先了解下基本的使用方式，就是输入需要处理 hCaptcha验证码图像，便可获得处理后的结果，首先需要简单地传递一个 `queries` 字段，这个就是具体hCaptcha验证码图像，我们需要去一个有hCaptcha验证码网站中截取这个验证码图片，示例网站链接为： `https://democaptcha.com/demo-form-eng/hcaptcha.html`，点击复选框即可显示具体的完整验证码图片，如下图所示：

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

其中`queries` 字段是上文中验证码图像的截图，图片大小建议不要超过100kb，还需要对上面图片红色箭头指向的区域进行截图，并且还需要你自行压缩图片大小，并且还需要将它转换为Base64编码，如下图所示：

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

同时还需要输入跟验证码图像相关的识别内容参数 `question`，这个支持中英文翻译，可以直接传相关的识别内容，由上文网页图像中黄色箭头执行的内容可以看出 `question` 输入的应为 `Please click on the UNIQUE object among the others.`。具体的内容如下：

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

可以看到这里我们设置了 Request Headers，包括：

* `accept`：想要接收怎样格式的响应结果，这里填写为 `application/json`，即 JSON 格式。
* `authorization`：调用 API 的密钥，申请之后可以直接下拉选择。

另外设置了 Request Body，包括：

* `queries`：Base64编码的验证码图像列表。
* `question`：验证码图像相关的识别内容参数，支持直接输入中英文。

选择之后，可以发现右侧也生成了对应代码，如图所示：

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

点击「Try」按钮即可进行测试，如上图所示，这里我们就得到了如下结果：

```json theme={null}
{
  "solution": {
    "label": "Please click on the UNIQUE object among the others",
    "box": [
      "360",
      "276"
    ],
    "confidences": 0.6354503631591797
  }
}
```

返回结果一共有多个字段，介绍如下：

* `solution`，此次 hCaptcha验证码图像 任务处理后验证结果。
  * `label`，hCaptcha验证码图像所识别的内容。
  * `box`，hCaptcha验证码图像识别结果的位置信息，它是由图像的坐标信息构成。
  * `confidences`，hCaptcha验证码图像识别后满足识别内容的的置信度。

可以看到我们得到了处理 hCaptcha验证码图像 的验证结果，我们只需要根据结果中 `box` 的位置坐标信息对该验证码图像进行模拟点击该区域便可通过验证。

下面将介绍如何通过结果的 `box` 的位置信息如何来点击，首先是针对上传的验证码图片进行建立直角坐标系，其中中心原点在图像的左下角，360是对应的横坐标，276是对应的纵坐标，我们只需要通过模拟点击验证码对应的坐标进行点击即可，具体的图像信息如下图所示：

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

另外如果想生成对应的对接代码，可以直接复制生成，例如 CURL 的代码如下：

```shell theme={null}
curl -X POST 'https://api.xhuoapi.ai/v1/captcha/recognition/hcaptcha' \
-H 'accept: application/json' \
-H 'authorization: Bearer {token}' \
-H 'content-type: application/json' \
-d '{
  "question": "Please click on the UNIQUE object among the others.",
  "queries": ["iVBORw0KGgoAAAANSU.....eY+85KVlzKHav28uq/WLVhL2kHUlFMKUcZbL31S8bpd0pEPKxNllXAE2wgu3uEfj+BfAzOGelsQNFAAAAAElFTkSuQmCC"]
}'
}'
```

Python 的对接代码如下：

```python theme={null}
import requests

url = "https://api.xhuoapi.ai/v1/captcha/recognition/hcaptcha"

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

payload = {
    "question": "Please click on the UNIQUE object among the others.",
    "queries": ["iVBORw0KGgoAAAANSU.....eY+85KVlzKHav28uq/WLVhL2kHUlFMKUcZbL31S8bpd0pEPKxNllXAE2wgu3uEfj+BfAzOGelsQNFAAAAAElFTkSuQmCC"]
}

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"
}
```

## 结论

通过本文档，您已经了解了如何使用 hCaptcha 图像识别 API 让用户输入识别的内容和 hCaptcha验证码图像，最后返回需要点击的小图像的坐标，完成验证。希望本文档能帮助您更好地对接和使用该 API。如有任何问题，请随时联系我们的技术支持团队。
