Developer Hub

Django Integration

Use client.django_middleware() to gate every request with a CertiLayer HCS check — no per-view code required.

Install

bash
pip install certilayer django

Setup

settings.py
import os
from certilayer import CertiLayerClient

certilayer_client = CertiLayerClient(api_key=os.environ["CERTILAYER_SECRET_KEY"])

MIDDLEWARE = [
    # ... your other middleware
    certilayer_client.django_middleware(min_score=0.30),
]
⚠️
django_middleware() reads the session ID from the HTTP_X_CERTILAYER_SESSION META key (Django's auto-uppercased form of the X-CertiLayer-Session header) and calls the async client via asyncio.run() internally — it's built for Django's classic synchronous request cycle, not ASGI async views.

Manual verification in a view

views.py
import asyncio
from django.http import JsonResponse
from certilayer import CertiLayerClient, HCSVerdict

client = CertiLayerClient(api_key=os.environ["CERTILAYER_SECRET_KEY"])

def checkout_view(request):
    session_id = request.META.get("HTTP_X_CERTILAYER_SESSION")
    result = asyncio.run(client.verify_session(session_id, critical=True))

    if result.verdict == HCSVerdict.SYNTHETIC:
        return JsonResponse({"error": "bot_detected"}, status=403)

    return process_checkout(request)
ℹ️
On a step-up response, verify_session() raises CertiLayerError with code == "STEP_UP_REQUIRED" rather than returning normally — wrap the call in a try/except when passing critical=True.