Authzed Python Client

1.24.4 · active · verified Sun Apr 12

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Authzed client using an API token and perform a basic permission check. Ensure `SPICEDB_ENDPOINT` and `SPICEDB_API_TOKEN` environment variables are set or replaced with your actual SpiceDB connection details.

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}")

view raw JSON →