Sign your crawler's requests in Python (Web Bot Auth)

Last checked November 22, 2026.

To sign a request with Web Bot Auth, your bot adds three headers: Signature-Agent, Signature-Input and Signature. The signature is an Ed25519 signature over the site's host name and your Signature-Agent value. The code below does it in about 60 lines of Python. We tested it by verifying its output with Cloudflare's own web-bot-auth library.

Cloudflare, Akamai and DataDome all check these signatures (Cloudflare, Akamai, DataDome).

What you need

Step 1: make a key pair

openssl genpkey -algorithm ed25519 -out private-key.pem
openssl pkey -in private-key.pem -pubout -out public-key.pem

Keep private-key.pem secret. It never leaves your bot's servers. Only the public key gets published.

Step 2: the signing code

Save this as sign_request.py.

import base64
import hashlib
import json
import os
import time
from urllib.parse import urlsplit

from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey


def b64url(data: bytes) -> str:
    return base64.urlsafe_b64encode(data).rstrip(b"=").decode()


def load_private_key(path: str) -> Ed25519PrivateKey:
    with open(path, "rb") as f:
        return serialization.load_pem_private_key(f.read(), password=None)


def public_jwk(private_key: Ed25519PrivateKey) -> dict:
    raw = private_key.public_key().public_bytes(
        serialization.Encoding.Raw, serialization.PublicFormat.Raw
    )
    return {"kty": "OKP", "crv": "Ed25519", "x": b64url(raw)}


def jwk_thumbprint(jwk: dict) -> str:
    # RFC 7638 / RFC 8037: required members only, sorted, no whitespace.
    canonical = json.dumps(
        {"crv": jwk["crv"], "kty": jwk["kty"], "x": jwk["x"]},
        separators=(",", ":"), sort_keys=True,
    )
    return b64url(hashlib.sha256(canonical.encode()).digest())


def sign_headers(url: str, private_key: Ed25519PrivateKey, signature_agent: str,
                 lifetime: int = 60) -> dict:
    """Return the three headers to add to a request for `url`."""
    authority = urlsplit(url).netloc.lower()
    agent_value = f'"{signature_agent}"'  # must be a quoted string
    created = int(time.time())
    expires = created + lifetime
    keyid = jwk_thumbprint(public_jwk(private_key))
    nonce = base64.b64encode(os.urandom(32)).decode()

    params = (
        '("@authority" "signature-agent")'
        f";created={created};keyid=\"{keyid}\";alg=\"ed25519\""
        f";expires={expires};nonce=\"{nonce}\";tag=\"web-bot-auth\""
    )
    signature_base = (
        f'"@authority": {authority}\n'
        f'"signature-agent": {agent_value}\n'
        f'"@signature-params": {params}'
    )
    signature = base64.b64encode(private_key.sign(signature_base.encode())).decode()
    return {
        "Signature-Agent": agent_value,
        "Signature-Input": f"sig1={params}",
        "Signature": f"sig1=:{signature}:",
    }

Step 3: use it

import requests
from sign_request import load_private_key, sign_headers

key = load_private_key("private-key.pem")
url = "https://crawltest.com/cdn-cgi/web-bot-auth"

headers = sign_headers(url, key, "https://bot.example.com")
headers["User-Agent"] = "ExampleBot/1.0 (+https://bot.example.com)"

r = requests.get(url, headers=headers, timeout=10)
print(r.status_code)

Replace https://bot.example.com with the HTTPS address where you publish your key directory (step 4). Sign each request fresh, because the signature expires after 60 seconds.

Step 4: publish your public key

Sites find your public key through the Signature-Agent address. Serve your key set at /.well-known/http-message-signatures-directory on that domain, over HTTPS, with the content type application/http-message-signatures-directory+json (Cloudflare).

Print the file contents with:

import json
from sign_request import load_private_key, public_jwk

print(json.dumps({"keys": [public_jwk(load_private_key("private-key.pem"))]}, indent=2))

It includes only kty, crv and x. Never publish the d value or anything from private-key.pem.

Cloudflare also expects the directory's own response to be signed, with tag="http-message-signatures-directory". Cloudflare's guide covers that step, and its http-signature-directory tool checks your setup (Cloudflare).

Step 5: test it

Cloudflare runs a test endpoint at https://crawltest.com/cdn-cgi/web-bot-auth (Cloudflare):

ResponseMeaning
200Verified
401The key is unknown, or a known key failed
400The headers are malformed

You'll get 401 until Cloudflare has registered your key through the BotBase form.

Why requests fail

These are the common causes Cloudflare lists (Cloudflare), and the code above avoids each one:

How we tested this code

We signed requests with this code, then verified them with Cloudflare's open-source web-bot-auth npm package (version 0.1.2), and they passed. A request with a changed host name failed, as it should. The keyid function matches the test vector in RFC 8037, Appendix A.3.


Rather not run this yourself? PacketPaper is building a service that hosts your key directory, sets up dedicated addresses with reverse DNS, and files your verification applications. You keep your private key. It's planned, not live yet. Join the waitlist.