Skip to main content

Foil Python Library

Preview Python 3.10+ License: MIT

The Foil Python library provides convenient access to the Foil API from applications written in Python. It includes a synchronous client for Sessions, Fingerprints, Organizations, Organization API key management, webhook endpoints, and sealed token verification.

The library also provides:

  • a fast configuration path using FOIL_SECRET_KEY
  • iterator helpers for cursor-based pagination
  • structured API errors and built-in sealed token verification
  • webhook endpoint management, test sends, event delivery history, and webhook signature verification

Documentation

See the Foil docs and API reference.

Installation

You don't need this source code unless you want to modify the package. If you just want to use the package, run:

pip install foil-server

Requirements

  • Python 3.10+

Usage

Use FOIL_SECRET_KEY or secret_key=...:

from foil_server import Foil

client = Foil(secret_key="sk_live_...")

page = client.sessions.list(verdict="bot", limit=25)
session = client.sessions.get("sid_123")
client.sessions.attach_client_user("sid_123", "user_123")
client.sessions.clear_client_user("sid_123")

Sealed token verification

from foil_server import safe_verify_foil_token

result = safe_verify_foil_token(
    sealed_token,
    "sk_live_...",
)

if result.ok:
    print(result.data.verdict, result.data.score)
else:
    print(result.error)

Pagination

for session in client.sessions.iter(search="signup"):
    print(session.id, session.latest_decision.verdict)

Fingerprints

page = client.fingerprints.list(sort="seen_count")
fingerprint = client.fingerprints.get("vid_123")

Organizations

organization = client.organizations.get("org_123")
updated = client.organizations.update("org_123", name="New Name")

Organization API keys

created = client.organizations.api_keys.create(
    "org_123",
    name="Production",
    type="secret",
    scopes=["sessions:list", "sessions:read"],
)

client.organizations.api_keys.revoke("org_123", created.id)

Webhooks

endpoint = client.webhooks.create_endpoint(
    "org_123",
    name="Production alerts",
    url="https://example.com/foil/webhook",
    event_types=["session.result.persisted"],
)

events = client.webhooks.list_events(
    "org_123",
    endpoint_id=endpoint.id,
    type="session.result.persisted",
)

print(events.items[0].webhook_deliveries[0].status)

Verifying webhook deliveries

Every webhook delivery is signed with your endpoint's signing secret. Verify the X-Foil-Timestamp and X-Foil-Signature headers against the raw request body before trusting the payload:

import os

from foil_server import parse_webhook_event, verify_and_parse_webhook_event, verify_webhook_signature

valid = verify_webhook_signature(
    secret=os.environ["FOIL_WEBHOOK_SECRET"],
    timestamp=request.headers["X-Foil-Timestamp"],
    raw_body=raw_body,
    signature=request.headers["X-Foil-Signature"],
)

# Verify and parse in one step. Raises ValueError if the signature is invalid or expired.
event = verify_and_parse_webhook_event(
    secret=os.environ["FOIL_WEBHOOK_SECRET"],
    timestamp=request.headers["X-Foil-Timestamp"],
    raw_body=raw_body,
    signature=request.headers["X-Foil-Signature"],
)

if event.type == "session.result.persisted":
    print(event.data)

# Parse a payload you have already verified.
parsed = parse_webhook_event(raw_body)

Signatures older than five minutes are rejected by default. Pass max_age_seconds to change the tolerance.

Error handling

from foil_server import FoilApiError

try:
    client.sessions.list(limit=999)
except FoilApiError as error:
    print(error.status, error.code, error.message)

Support

If you need help integrating Foil, start with usefoil.com/docs.

Metadata

Release files for foil-server 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for foil-server 0.3.0
File Size Uploaded Provenance Actions
foil_server-0.3.0.tar.gz 74.2 kB With provenance Details for foil_server-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for foil-server 0.3.0
File Interpreter ABI Platform Actions
foil_server-0.3.0-py3-none-any.whl Python 3 none any Details for foil_server-0.3.0-py3-none-any.whl

Total release size: 88.8 kB

Release files / foil_server-0.3.0.tar.gz

Download URL foil_server-0.3.0.tar.gz
Size 74.2 kB
Tags Source
SHA-256 checksum
How to use checksums
87ac4ae311f98a83672729e168b9f9d4c52036fede3a511044f8f604db977be5
BLAKE2b-256 checksum
How to use checksums
93fc4042d3f5b1a826a7ef88b6d91ad026a9e233b5585952dd2ab584fe6c3749
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 5, 2026.

Transparency log

Release files / foil_server-0.3.0-py3-none-any.whl

Download URL foil_server-0.3.0-py3-none-any.whl
Size 14.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1d86172eee0a3ea329897d41b921b2d0f6eb4faf0e671ed90f297b3e8d37784f
BLAKE2b-256 checksum
How to use checksums
96cbdda553d464614964061adc025678e249cb0b7f3d336ad0d0232ea1b8b66a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 5, 2026.

Transparency log

Security for foil-server 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for foil_server-0.3.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 5, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page