Reusable hCaptcha integration for NiceGUI applications.
Features:
- Async token retrieval
- Server-side verification
- Hook-based extensibility
pip install nicegui-hcaptchaYou also need:
- hCaptcha site key
- hCaptcha secret key
Both are available in the hCaptcha dashboard.
from nicegui import ui
from nicegui_hcaptcha import HCaptcha
captcha = HCaptcha(site_key="your_site_key_here")
async def submit():
result = await captcha.verify(secret="your_secret_key_here")
if not result.success:
ui.notify(f"Captcha failed: {result.error_codes}")
return
ui.notify("Captcha verified")
ui.button("Submit", on_click=submit)That is sufficient for basic usage.
verify() returns:
HCaptchaResult(
success: bool,
error_codes: Sequence[HCaptchaErrorCode] | None,
token: str | None,
)You are responsible for handling UI behavior.
missing-input-secretinvalid-input-secretmissing-input-responseinvalid-input-responseexpired-input-responsealready-seen-responsebad-requestmissing-remoteipinvalid-remoteipnot-using-dummy-passcodesitekey-secret-mismatch
-
captcha-not-completedReturned ifverify()is called before user completion. -
verification-request-failedReturned if the verification request fails or response parsing fails.
You can register async callbacks:
async def on_captcha(result: HCaptchaResult) -> None:
if result.success:
print("Verified")
else:
print("Failed:", result.error_codes)
captcha.on_verified(on_captcha)Hooks are executed after each verification.
await captcha.reset()This forces the user to complete a new challenge.
Typical pattern:
result = await captcha.verify(secret)
if not result.success:
return
# continue form processing