验证码
验证码令牌校验与控件发放。
概览
基础路径:
/v1/captchaPython 命名空间:
infra.captchatext
# Python
from infrai import infra
# TypeScript
import { InfraiClient } from "@infrai/sdk";
const client = new InfraiClient({ apiKey: process.env.INFRAI_API_KEY! });
// → client.captcha方法
captcha.verify
POST /v1/captcha/verify
校验来自浏览器的验证码令牌。
Python
python
infra.captcha.verify(token=..., vendor=None, remote_ip=None, min_score=None)TypeScript
typescript
client.captcha.verify(opts: CaptchaVerifyOptions): Promise<CaptchaVerifyResult>参数
| 名称 | 类型 | 必填 | 说明 |
|---|---|---|---|
token | string | 必填 | 验证码控件返回的令牌。 |
vendor | "hcaptcha" | "recaptcha" | "turnstile" | "infrai" | 可选 | 固定使用某个供应商,而非自动路由。 |
remote_ip | string | 可选 | 用于风险评分的客户端 IP。 |
min_score | number | 可选 | 可接受的最低分(基于分数的供应商)。 |
返回
CaptchaVerifyResult { valid, score?, vendor, hostname?, action? }示例
一次性前置(每个范例都假定已完成):
bash
pip install infrai
# one-time auth (no secret needed): anonymous account + trial, writes ~/.infrai/credentials
python -c "from infrai import infra; infra.activate()"
# returning user instead: export INFRAI_API_KEY=ifr_pk_proj_...python
from infrai import infra
res = infra.captcha.verify(token=user_submitted_token, remote_ip="203.0.113.7")
print("valid:", res.get("valid"), "| score:", res.get("score"))captcha.widget.create
POST /v1/captcha/widgets
发放验证码控件代码段与脚本 URL。
Python
python
infra.captcha.widget.create(vendor=None, site_key_id=None)TypeScript
typescript
client.captcha.widget(opts): Promise<{ html, script_url }>参数
| 名称 | 类型 | 必填 | 说明 |
|---|---|---|---|
vendor | string | 可选 | 固定使用某个供应商,而非自动路由。 |
site_key_id | string | 可选 | 已配置的 site-key id。 |
返回
{ html, script_url }示例
一次性前置(每个范例都假定已完成):
bash
pip install infrai
# one-time auth (no secret needed): anonymous account + trial, writes ~/.infrai/credentials
python -c "from infrai import infra; infra.activate()"
# returning user instead: export INFRAI_API_KEY=ifr_pk_proj_...python
from infrai import infra
w = infra.captcha.widget.create(vendor="turnstile")
print("embed script:", w.get("script_url"))