This document provides a high-level introduction to the OpenFGA Python SDK, its architecture, and capabilities. The SDK is a wrapper around the OpenFGA API that provides both synchronous and asynchronous clients for integrating fine-grained authorization into Python applications.
For detailed information about OpenFGA concepts and terminology, see What is OpenFGA. For comprehensive feature documentation, see SDK Features and Capabilities. For details on how code is organized, see Package Structure.
The OpenFGA Python SDK is an autogenerated client library that wraps the OpenFGA API, providing a convenient interface for managing authorization in Python applications. OpenFGA is an open-source Fine-Grained Authorization solution inspired by Google's Zanzibar paper, designed to make it easy for application builders to model their permission layer and integrate authorization checks.
The SDK provides:
OpenFgaClient class for common operationsOpenFgaApi class for advanced use casesSources: README.md1-122 README.md59-64 pyproject.toml12-14
The SDK follows a layered architecture with clear separation of concerns, from high-level convenience methods down to HTTP transport:
Layer Responsibilities:
| Layer | Components | Responsibility |
|---|---|---|
| High-Level Client | OpenFgaClient, ClientConfiguration | User-facing convenience methods, parameter transformation, batch operations |
| Generated API | OpenFgaApi, model classes | Direct 1:1 mapping to OpenFGA API endpoints from OpenAPI specification |
| Core Infrastructure | ApiClient, Configuration, OAuth2Client, Credentials | HTTP orchestration, authentication, retry logic, configuration management |
| Transport | RESTClientObject, telemetry | Low-level HTTP operations, observability |
Sources: openfga_sdk/__init__.py1-14 README.md122-148
The OpenFgaClient class provides the primary interface for SDK users. It wraps the lower-level OpenFgaApi and adds convenience features:
batch_check, client_batch_check with parallel executionstreamed_list_objects for handling large result setsAvailable in two variants:
openfga_sdk.client.OpenFgaClient with async/await methodsopenfga_sdk.sync.client.OpenFgaClient with blocking methodsSources: README.md122-148 openfga_sdk/__init__.py3-4
The OpenFgaApi class is autogenerated from the OpenAPI specification and provides direct access to all API endpoints. Each API operation has a corresponding method:
check(), batch_check() - Authorization checkslist_objects(), streamed_list_objects() - List accessible objectsread(), write() - Relationship tuple operationswrite_authorization_model(), read_authorization_model() - Model managementcreate_store(), list_stores(), get_store(), delete_store() - Store operationsSources: README.md1314-1339 openfga_sdk/__init__.py1
The ApiClient class orchestrates HTTP requests and implements cross-cutting concerns:
OAuth2Client for credential handlingSources: README.md1264-1287 openfga_sdk/__init__.py2
Two configuration classes manage SDK settings at different levels:
ClientConfiguration (openfga_sdk.client.configuration):
api_url - OpenFGA server URLstore_id - Default store identifierauthorization_model_id - Default authorization modelcredentials - Authentication configurationheaders - Default request headersConfiguration (openfga_sdk.configuration):
api_url, host, scheme - Server connection settingsssl_ca_cert - SSL certificate pathtimeout - Request timeout in millisecondsretry_params - Retry behavior configurationtelemetry - Observability settingsSources: README.md134-230 openfga_sdk/__init__.py4-5
The SDK provides complete parallel implementations for asynchronous and synchronous execution:
Key Differences:
| Aspect | Async (openfga_sdk.*) | Sync (openfga_sdk.sync.*) |
|---|---|---|
| Method Signature | async def method_name() | def method_name() |
| Usage | await client.check(...) | client.check(...) |
| HTTP Library | aiohttp | urllib3 |
| Concurrency | Non-blocking I/O with asyncio | Blocking I/O |
| Best For | High-concurrency applications, web frameworks (FastAPI, aiohttp) | Traditional applications, scripts, Django |
Both implementations share the same data models, configuration classes, and telemetry infrastructure, ensuring API consistency.
Sources: README.md266-287 pyproject.toml37-42
The following diagram illustrates how a typical authorization check flows through the SDK layers:
Sources: README.md796-826 README.md1264-1287
The SDK is available via PyPI and requires Python 3.10 or higher:
Dependencies:
aiohttp>=3.9.3 - Async HTTP clienturllib3>=1.26.19,<3 - Sync HTTP clientpython-dateutil>=2.9.0 - Date/time utilitiesopentelemetry-api>=1.25.0 - Telemetry integrationSources: README.md75-120 pyproject.toml35-42
Async Client:
Sync Client:
Sources: README.md122-204 README.md266-287
The SDK architecture separates generated code from custom enhancements:
Generated Code provides:
Custom Code adds:
OpenFgaClient)The .openapi-generator-ignore file ensures generated code is excluded from version control and regenerated during builds.
Sources: README.md12 openfga_sdk/__init__.py1-14
The SDK provides methods for all OpenFGA operations, organized into functional categories:
| Category | Operations | Description |
|---|---|---|
| Store Management | create_store, get_store, list_stores, delete_store | Manage authorization stores (containers for models and tuples) |
| Authorization Models | write_authorization_model, read_authorization_model, read_latest_authorization_model, read_authorization_models | Define and retrieve permission schemas |
| Relationship Tuples | write, read, read_changes, write_tuples, delete_tuples | Manage relationship data (who has what relation to which object) |
| Authorization Queries | check, batch_check, expand, list_objects, list_relations, list_users | Perform authorization checks and queries |
| Streaming | streamed_list_objects | Handle large result sets efficiently |
| Assertions | read_assertions, write_assertions | Test authorization models |
For detailed documentation on each operation, see API Operations.
Sources: README.md296-1262 openfga_sdk/__init__.py15-131
The SDK automatically retries failed requests with 429 (rate limit) or 5xx (server error) status codes:
RetryParams allows customization up to 15 retriesRetry-After headersSources: README.md1264-1287
Built-in observability with OpenTelemetry metrics:
For details, see Telemetry and Observability.
Sources: pyproject.toml40 openfga_sdk/__init__.py125-131
Efficient bulk authorization checks:
batch_check: Server-side batch checking (OpenFGA 1.8.0+)client_batch_check: Client-side parallel checking for older serversFor examples, see Batch Operations.
Sources: README.md828-1035
Supports multiple authentication approaches:
For configuration details, see Credentials and Authentication Methods.
Sources: README.md132-204
To get started with the SDK:
For specific use cases:
Sources: README.md14-57
Refresh this wiki
This wiki was recently refreshed. Please wait 7 days to refresh again.