HomeDocs

Example: Python

Verify a token server-side with the standard library urllib or requests.

Using requests

import os

import requests

def verify_token(token: str, secret: str) -> dict:
    resp = requests.post(
        "https://verify.nexlolabs.net/api/challenge/verify",
        json={"token": token, "secret": secret},
        timeout=10,
    )
    data = resp.json()
    if resp.status_code != 200:
        raise RuntimeError(data["error"]["message"])
    return data


# ---- in your web framework handler (Flask/Django/FastAPI) ----
def handle_form(request):
    token = request.form.get("verificationToken", "")

    if not token:
        return "Missing verification token", 400

    try:
        result = verify_token(token, os.environ["NLV_SITE_SECRET"])
    except (RuntimeError, requests.RequestException) as exc:
        return str(exc), 400

    if result["success"] is not True or result["score"] < 0.5:
        return "Human verification failed - please try again", 400

    # Safe to process the form…
    return "OK"

Using the standard library

import json
import os
import urllib.request

def verify_token(token: str, secret: str) -> dict:
    body = json.dumps({"token": token, "secret": secret}).encode("utf-8")
    request = urllib.request.Request(
        "https://verify.nexlolabs.net/api/challenge/verify",
        data=body,
        headers={"Content-Type": "application/json"},
        method="POST",
    )
    with urllib.request.urlopen(request, timeout=10) as response:
        return json.load(response)

Django view example

from django.http import JsonResponse

def submit_view(request):
    token = request.POST.get("verificationToken", "")
    if not token:
        return JsonResponse({"error": "Missing verification token"}, status=400)

    result = verify_token(token, os.environ["NLV_SITE_SECRET"])

    if result["success"] is not True or result["score"] < 0.5:
        return JsonResponse({"error": "Verification failed"}, status=400)

    # … save the comment, order, message …
    return JsonResponse({"ok": True})