Authzed Python Client
The `authzed` library is the official Python client for Authzed's SpiceDB, a permissions database and service. It enables developers to define authorization schemas, manage relationships between objects, and perform efficient permission checks within their applications. The library supports both the v1 Core SpiceDB API and the materialize/v0 API for building materialized permission views. It maintains an active development status with regular updates.
Warnings
- gotcha When developing locally or with self-signed certificates, standard `bearer_token_credentials` might fail due to TLS verification issues. Use `insecure_bearer_token_credentials()` for non-TLS connections or explicitly provide `certChain` for custom certificates.
- gotcha The 'Dual-Write Problem' is a common architectural challenge when integrating Authzed/SpiceDB with an application database. Ensuring consistency between both systems (e.g., when creating a file and its permissions) requires careful handling.
- gotcha The `InsecureClient` provided by `authzed-py` uses `grpc.insecure_channel`, which is not inherently compatible with `asyncio`. Attempting to use it with asynchronous operations, especially methods like `LookupResources` that return `UnaryStreamCall`, may lead to authentication errors or unexpected behavior.
- gotcha Periodically, specific minor releases may encounter packaging issues that prevent correct installation or module imports, as was observed with `authzed-py v1.22.0`.
- gotcha Avoid creating cycles in your SpiceDB schema definitions. While recursive schemas can be powerful, incorrect usage or accidental cycles can lead to significant performance issues and unexpected behavior in permission evaluations.
- gotcha When making permission checks, prefer checking permissions directly rather than relations. If the logic for a check needs to change, modifying a permission definition is significantly easier and safer than changing a relation definition, which often requires a data migration.
Install
-
pip install authzed
Imports
- Client
from authzed.api.v1 import Client
- bearer_token_credentials
from grpcutil import bearer_token_credentials
- insecure_bearer_token_credentials
from grpcutil import insecure_bearer_token_credentials
Quickstart
import os
from authzed.api.v1 import Client, CheckPermissionRequest, ObjectReference, SubjectReference
from grpcutil import bearer_token_credentials
# Replace with your SpiceDB endpoint and API token from environment variables
SPICEDB_ENDPOINT = os.environ.get('SPICEDB_ENDPOINT', 'grpc.authzed.com:443')
SPICEDB_API_TOKEN = os.environ.get('SPICEDB_API_TOKEN', 't_your_token_here_1234567deadbeef')
if not SPICEDB_API_TOKEN:
raise ValueError("SPICEDB_API_TOKEN environment variable not set or is empty.")
# Initialize the client with bearer token credentials
client = Client(
SPICEDB_ENDPOINT,
bearer_token_credentials(SPICEDB_API_TOKEN),
)
try:
# Example: Check if a user 'emilia' can 'view' a document 'first_doc'
request = CheckPermissionRequest(
resource=ObjectReference(object_type="document", object_id="first_doc"),
permission="view",
subject=SubjectReference(object=ObjectReference(object_type="user", object_id="emilia"))
)
response = client.CheckPermission(request)
print(f"Permission check result: {response.permissionship}")
except Exception as e:
print(f"An error occurred: {e}")