跳到正文

验证码

验证码令牌校验与控件发放。

1. 概览

基础路径: https://api.infrai.cc/v1/captcha
鉴权头: Authorization: Bearer $INFRAI_API_KEY
bash
# Call any /v1/captcha capability over raw HTTP — no SDK to install.
# curl:
curl https://api.infrai.cc/v1/captcha/... \
  -H "Authorization: Bearer $INFRAI_API_KEY" \
  -H "Content-Type: application/json"

2. 方法 (4)

2.1captcha.verify

POST /v1/captcha/verify

校验由 Infrai 托管浏览器控件返回的验证码令牌。

参数

名称类型必填说明
widget_record_idstring
必填
captcha.widget.create 返回的 Infrai widget ID。
≥ 1 chars
tokenstring
必填
浏览器控件返回的一次性令牌。
≥ 1 chars
ipstring可选用于风险评分的客户端 IP。
score_thresholdnumber可选可接受的最低分(基于分数的供应商)。
0–1

返回

CaptchaVerification { success, score?, vendor, hostname?, action?, reasons }
名称类型说明
successboolean验证码校验是否通过
scorenumber | null0=机器人,1=人类(归一化)。
0–1
hostnamestring | null验证码被解决的站点主机名
actionstring | null执行的操作(如 created、updated、deleted)
challenge_tsstringISO 8601 时间戳:the captcha challenge was issued(ISO 8601)
format: date-time
vendorstring处理此请求的供应商
reasons("timeout-or-duplicate" | "invalid-input-response" | "invalid-sitekey" | "low-score" | "hostname-mismatch")[]reasons contributing to the risk score列表

示例

一次性前置(每个范例都假定已完成):

bash
# No SDK to install — every call is a plain HTTPS request.
# Get a project key by signing in at https://infrai.cc/login (Google/GitHub gives
# you $2 free credit; email sign-in starts at $0). On 402 INSUFFICIENT_CREDIT, add
# funds at https://infrai.cc/billing (or POST /v1/account/topup and open the
# returned checkout_url).
export INFRAI_API_KEY="ifr_..."
bash
curl -X POST https://api.infrai.cc/v1/captcha/verify \
  -H "Authorization: Bearer $INFRAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"widget_record_id": "cwid_...", "token": "<client-captcha-token>", "ip": "203.0.113.5"}'

2.2captcha.widget.create

POST /v1/captcha/widget/create

完全通过 Infrai 创建验证码组件并获取浏览器公开配置。

参数

名称类型必填说明
namestring
必填
便于识别的 widget 名称。
1–128 chars
domainsstring[]可选允许渲染该 widget 的主机名。
widget_mode"managed" | "non-interactive" | "invisible"可选供应商控件的渲染模式。
default: "managed"

返回

WidgetSetup { widget_record_id, vendor, sitekey, embed_url, embed_snippet, status }
名称类型说明
widget_record_idstringInfrai widget ID;用于 widget.get 和 captcha.verify。
namestringHuman label; doubles as verify-time sitekey_label hint.
vendorstring-
statusanyvendor widget 和服务端验证密钥均已成功创建。
sitekeystringPublic; embed in client HTML.
≥ 1 chars
widget_idstring | nullVendor-side widget identifier.
domainsstring[]-
embed_urlstringPublic vendor browser script URL.
format: uri
embed_snippetstringCopy-paste HTML snippet.
≥ 1 chars
dashboard_urlstring | nullVendor dashboard link for manual completion.
messagestring | nullHuman-readable note (e.g. manual setup instructions).
metadataobject | null-
errorstring | null-
created_atstring-
format: date-time

示例

一次性前置(每个范例都假定已完成):

bash
# No SDK to install — every call is a plain HTTPS request.
# Get a project key by signing in at https://infrai.cc/login (Google/GitHub gives
# you $2 free credit; email sign-in starts at $0). On 402 INSUFFICIENT_CREDIT, add
# funds at https://infrai.cc/billing (or POST /v1/account/topup and open the
# returned checkout_url).
export INFRAI_API_KEY="ifr_..."
bash
curl -X POST https://api.infrai.cc/v1/captcha/widget/create \
  -H "Authorization: Bearer $INFRAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"name": "signup", "domains": ["app.example.com"], "widget_mode": "managed"}'

2.3captcha.widget.list

GET /v1/captcha/widget/list

列出当前账户由 Infrai 托管的验证码组件。

返回

WidgetListResult { items: WidgetSetup[], total_count, next_cursor? }
名称类型说明
itemsobject[]-
items[].widget_record_idstringInfrai widget ID;用于 widget.get 和 captcha.verify。
items[].namestringHuman label; doubles as verify-time sitekey_label hint.
items[].vendorstring-
items[].statusanyvendor widget 和服务端验证密钥均已成功创建。
items[].sitekeystringPublic; embed in client HTML.
≥ 1 chars
items[].widget_idstring | nullVendor-side widget identifier.
items[].domainsstring[]-
items[].embed_urlstringPublic vendor browser script URL.
format: uri
items[].embed_snippetstringCopy-paste HTML snippet.
≥ 1 chars
items[].dashboard_urlstring | nullVendor dashboard link for manual completion.
items[].messagestring | nullHuman-readable note (e.g. manual setup instructions).
items[].metadataobject | null-
items[].errorstring | null-
items[].created_atstring-
format: date-time
total_countintegerOptional; may be omitted for high-cardinality lists.
≥ 0
next_cursorstring | null传入 list() 以获取下一页;null 表示结束。

示例

一次性前置(每个范例都假定已完成):

bash
# No SDK to install — every call is a plain HTTPS request.
# Get a project key by signing in at https://infrai.cc/login (Google/GitHub gives
# you $2 free credit; email sign-in starts at $0). On 402 INSUFFICIENT_CREDIT, add
# funds at https://infrai.cc/billing (or POST /v1/account/topup and open the
# returned checkout_url).
export INFRAI_API_KEY="ifr_..."
bash
curl -X GET https://api.infrai.cc/v1/captcha/widget/list \
  -H "Authorization: Bearer $INFRAI_API_KEY"

2.4captcha.widget.get

GET /v1/captcha/widget/get/{widget_record_id}

获取 Infrai 托管验证码组件的浏览器公开配置。

参数

名称类型必填说明
widget_record_idstring
必填
captcha.widget.create 返回的 Infrai widget ID。

返回

WidgetSetup { widget_record_id, vendor, sitekey, embed_url, embed_snippet, status }
名称类型说明
widget_record_idstringInfrai widget ID;用于 widget.get 和 captcha.verify。
namestringHuman label; doubles as verify-time sitekey_label hint.
vendorstring-
statusanyvendor widget 和服务端验证密钥均已成功创建。
sitekeystringPublic; embed in client HTML.
≥ 1 chars
widget_idstring | nullVendor-side widget identifier.
domainsstring[]-
embed_urlstringPublic vendor browser script URL.
format: uri
embed_snippetstringCopy-paste HTML snippet.
≥ 1 chars
dashboard_urlstring | nullVendor dashboard link for manual completion.
messagestring | nullHuman-readable note (e.g. manual setup instructions).
metadataobject | null-
errorstring | null-
created_atstring-
format: date-time

示例

一次性前置(每个范例都假定已完成):

bash
# No SDK to install — every call is a plain HTTPS request.
# Get a project key by signing in at https://infrai.cc/login (Google/GitHub gives
# you $2 free credit; email sign-in starts at $0). On 402 INSUFFICIENT_CREDIT, add
# funds at https://infrai.cc/billing (or POST /v1/account/topup and open the
# returned checkout_url).
export INFRAI_API_KEY="ifr_..."
bash
curl -X GET https://api.infrai.cc/v1/captcha/widget/get/WIDGET_RECORD_ID \
  -H "Authorization: Bearer $INFRAI_API_KEY"
高级:指定 vendor

默认情况下 infrai 会把每次调用智能路由到最佳可用供应商——无需自己挑选 vendor。作为高级逃生口,本能力支持可选的 vendor 入参以锁定某个供应商。本能力当前所有可用 vendor 可通过该能力 id 对应的 discovery 端点实时获取——参见 discovery API

GET /v1/discovery/{capability}

captcha.verify

captcha.widget.create

captcha.widget.list

3. 全部能力

本模块全部已路由能力——完整的对外 REST 契约。上方方法是带讲解的入门示例,此表是完整参考。

captcha.verifyPOST /v1/captcha/verify

Verify a client-submitted CAPTCHA token against the vendor and return the success result; idempotent.

参数 (10)
名称类型必填说明
widget_record_idstring必填Infrai widget ID returned by captcha.widget.create; selects the exact vendor and server-side secret.
≥ 1 chars
tokenstring必填One-time vendor response token from the client widget.
≥ 1 chars
vendorstring | null可选Optional consistency assertion; must match the widget vendor when provided.
ipstring | null可选End-user IP for vendor-side risk scoring. Alias: `remoteip`.
remoteipstring | null可选Alias of `ip` (module accepts both; `ip = ip or remoteip`).
actionstring | null可选Action name bound at challenge time; rejected if mismatched (anti cross-form replay).
expected_hostnamestring | null可选If set, token hostname must match.
score_thresholdnumber | null可选Minimum acceptable score [0,1]; below → fail with low-score reason.
0–1
mode"default_vendor" | "verified_account"可选Routing axis (CaptchaMode); orthogonal to widget_mode.
default: "default_vendor"
sitekey_labelstring可选Deprecated compatibility hint; widget_record_id selects the exact sitekey and secret.
default: "default"
captcha.widget.createPOST /v1/captcha/widget/create

Create and manage a CAPTCHA widget through Infrai; returns the public sitekey and browser embed snippet while keeping the vendor secret server-side.

参数 (5)
名称类型必填说明
namestring必填Human label; also the verify-time sitekey_label hint.
1–128 chars
domainsstring[]可选Allowed hostnames for this sitekey.
vendor"turnstile" | null可选Optional vendor pin. Widget provisioning currently supports Turnstile.
mode"default_vendor" | "verified_account"可选Routing axis (CaptchaMode).
default: "default_vendor"
widget_mode"managed" | "non-interactive" | "invisible"可选Vendor render axis (orthogonal to mode).
default: "managed"
captcha.widget.getGET /v1/captcha/widget/get/{widget_record_id}

Get this account's CAPTCHA widget and public browser configuration.

参数 (1)
名称类型必填说明
widget_record_idstring必填WidgetStore primary key.
captcha.widget.listGET /v1/captcha/widget/list

List this account's CAPTCHA widgets.

参数 (3)
名称类型必填说明
vendorstring | null可选Optional filter by vendor.
cursorstring | null可选Opaque cursor from a previous page; null = first page.
limitinteger可选
1–1000default: 50

4. 完整示例

本模块的生产级端到端范例:先一次性配置,再运行业务流程,尽量覆盖本模块的多数 API。

单文件可运行 Python 程序(仅标准库、无 SDK):拷贝后填入 INFRAI_API_KEY 运行,即可按真实业务流逐步体验本模块核心 API——每一步都真实调用并计费,后续步骤复用前一步返回的真实字段。12 行 helper 就是全部集成代码。

python
#!/usr/bin/env python3
"""Infrai · captcha — runnable real-app example (single file, zero deps).

Copy this file, set your key, run it: every step is a REAL call to
api.infrai.cc, billed at the real (tiny) per-call price, printing the
live JSON response. Get a key at https://infrai.cc/login (Google/
GitHub sign-in grants $2 free credit); add funds at
https://infrai.cc/billing. No SDK — the 12-line helper below is the
entire integration."""
import json
import os
from urllib import error, request

KEY = os.environ.get("INFRAI_API_KEY") or "ifr_..."  # <- your key
BASE = "https://api.infrai.cc"


# Same raw HTTPS POST/GET as every per-method example on this page —
# wrapped once for reuse. There is nothing else to it: no SDK.
def infrai(method, path, body=None):
    req = request.Request(
        BASE + path, method=method,
        data=json.dumps(body).encode() if body is not None else None,
        headers={"Authorization": f"Bearer {KEY}",
                 "Content-Type": "application/json"})
    try:
        with request.urlopen(req, timeout=60) as r:
            return json.loads(r.read())
    except error.HTTPError as e:
        return json.loads(e.read())


def show(label, resp):
    print(f"\n== {label} ==")
    print(json.dumps(resp, indent=2, ensure_ascii=False))
    return resp


# 1) captcha.widget.create — POST /v1/captcha/widget/create · Create and manage a CAPTCHA widget through Infrai; returns the public sitekey and browser embed snippet while keeping the vendor secret server-side.
r1 = show("captcha.widget.create", infrai("POST", "/v1/captcha/widget/create", {"name":"signup","domains":["app.example.com"],"widget_mode":"managed"}))

# 2) captcha.verify — POST /v1/captcha/verify · Verify a client-submitted CAPTCHA token against the vendor and return the success result; idempotent.
ask1 = input("Paste the one-time token returned by the browser widget: ").strip()
widget_record_id_2 = (r1.get("data") or {}).get("widget_record_id") or ""
r2 = show("captcha.verify", infrai("POST", "/v1/captcha/verify", {"widget_record_id":widget_record_id_2,"token":ask1,"ip":"203.0.113.5"}))

5. 开发者指南(英文)

从端点细节继续进入完整工作流、生产模式和基于实时 API 验证的故障排查指南。

验证码 开发者指南