Official Foil Python server SDK
Project description
Foil Python Library
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, sealed token verification, Gate, and Gate delivery/webhook helpers.
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, and event delivery history
- public, bearer-token, and secret-key auth modes for Gate flows
- Gate delivery/webhook helpers
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=... for core detect APIs. For public or bearer-auth Gate flows, the client can also be created without a 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")
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", "gate.session.approved"],
)
events = client.webhooks.list_events(
"org_123",
endpoint_id=endpoint.id,
type="session.result.persisted",
)
print(events.items[0].webhook_deliveries[0].status)
Gate APIs
from foil_server import Foil, create_delivery_key_pair
client = Foil()
services = client.gate.registry.list()
session = client.gate.sessions.create(
service_id="foil",
account_name="my-project",
delivery=create_delivery_key_pair().delivery,
)
print(services[0].id, session.consent_url)
Gate delivery and webhook helpers
from foil_server import (
create_delivery_key_pair,
create_gate_approved_webhook_response,
decrypt_gate_delivery_envelope,
parse_webhook_event,
verify_gate_webhook_signature,
)
key_pair = create_delivery_key_pair()
response = create_gate_approved_webhook_response(
{
"delivery": key_pair.delivery,
"outputs": {
"FOIL_PUBLISHABLE_KEY": "pk_live_...",
"FOIL_SECRET_KEY": "sk_live_...",
},
}
)
payload = decrypt_gate_delivery_envelope(key_pair.private_key, response.encrypted_delivery)
print(payload.outputs["FOIL_SECRET_KEY"])
raw_body = '{"id":"wevt_123","object":"webhook_event","type":"webhook.test","created":"2026-04-26T00:00:00.000Z","data":{}}'
print(
verify_gate_webhook_signature(
secret="whsec_test",
timestamp="1735776000",
raw_body=raw_body,
signature="…",
)
)
event = parse_webhook_event(raw_body)
print(event.type)
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file foil_server-0.2.3.tar.gz.
File metadata
- Download URL: foil_server-0.2.3.tar.gz
- Upload date:
- Size: 88.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fa4d58423f51feba92df982204e3d2705d4b5b76c51d14c733279d7ea8d70558
|
|
| MD5 |
c0a1e59207b9b2445845ff157e108d73
|
|
| BLAKE2b-256 |
276023840893f5290301bbbd2568306cff440d0a60b8072127500b08cee5baec
|
Provenance
The following attestation bundles were made for foil_server-0.2.3.tar.gz:
Publisher:
release.yml on abxy-labs/foil-server-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
foil_server-0.2.3.tar.gz -
Subject digest:
fa4d58423f51feba92df982204e3d2705d4b5b76c51d14c733279d7ea8d70558 - Sigstore transparency entry: 1607853166
- Sigstore integration time:
-
Permalink:
abxy-labs/foil-server-python@43924cd089d495bd78a9dc9d4941389cb219324d -
Branch / Tag:
refs/heads/main - Owner: https://github.com/abxy-labs
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@43924cd089d495bd78a9dc9d4941389cb219324d -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file foil_server-0.2.3-py3-none-any.whl.
File metadata
- Download URL: foil_server-0.2.3-py3-none-any.whl
- Upload date:
- Size: 20.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dcb1b6ef03a2237148a08fc3466c8e1b25bb3c4111e549c8b6a1673e1a480e70
|
|
| MD5 |
82bd5a62b35f7d197c4914b6ba9c75ba
|
|
| BLAKE2b-256 |
85a8e27de7548938312172e64afd99441000adb5b6774c62bb8ddfe81f81707c
|
Provenance
The following attestation bundles were made for foil_server-0.2.3-py3-none-any.whl:
Publisher:
release.yml on abxy-labs/foil-server-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
foil_server-0.2.3-py3-none-any.whl -
Subject digest:
dcb1b6ef03a2237148a08fc3466c8e1b25bb3c4111e549c8b6a1673e1a480e70 - Sigstore transparency entry: 1607853225
- Sigstore integration time:
-
Permalink:
abxy-labs/foil-server-python@43924cd089d495bd78a9dc9d4941389cb219324d -
Branch / Tag:
refs/heads/main - Owner: https://github.com/abxy-labs
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@43924cd089d495bd78a9dc9d4941389cb219324d -
Trigger Event:
workflow_dispatch
-
Statement type: