This document provides a comprehensive introduction to the OpenFGA Python SDK, covering its purpose, architecture philosophy, and relationship to the OpenFGA authorization service. It explains the SDK's layered design, dual execution model (async/sync), and key components that applications interact with.
For installation instructions and quick start examples, see Getting Started. For detailed API operation documentation, see API Operations. For configuration options, see Configuration and Authentication.
The OpenFGA Python SDK is a client library that provides programmatic access to the OpenFGA authorization service. It is primarily auto-generated from the OpenFGA API specification using OpenAPI Generator, with hand-written high-level client wrappers and utilities added on top for developer convenience.
The SDK serves as a bridge between Python applications and OpenFGA servers, handling:
Key characteristics:
asyncio and aiohttp) and synchronous (using urllib3)openfga-sdkSources: README.md1-14 README.md59-64 pyproject.toml11-42
The SDK follows a layered architecture with clear separation of concerns. Each layer has a specific responsibility and delegates downward through the stack:
Layer responsibilities:
| Layer | Primary Classes | Responsibility |
|---|---|---|
| High-Level Client | OpenFgaClient, ClientConfiguration | Convenience methods, batch operations, streaming, client-side logic |
| Direct API | OpenFgaApi, Model classes | One-to-one mappings to OpenFGA API endpoints |
| HTTP Orchestration | ApiClient, Configuration, OAuth2Client | Request preparation, authentication, retry logic, telemetry |
| Transport | RESTClientObject | Raw HTTP communication using aiohttp (async) or urllib3 (sync) |
Sources: openfga_sdk/__init__.py1-6 README.md122-148
The SDK provides complete parallel implementations for asynchronous and synchronous execution. This allows applications to choose their concurrency model based on their runtime environment and performance requirements.
Key points:
async/await syntax, asyncio, and aiohttp for I/Ourllib3 for I/Oasync/await keywordsSources: README.md266-287 openfga_sdk/__init__.py1-6
The openfga_sdk package is organized into distinct modules based on functionality:
Module breakdown:
| Module | Purpose | Key Files |
|---|---|---|
client/ | High-level client wrapper | client.py, configuration.py |
api/ | Direct API endpoint methods | open_fga_api.py |
api_client.py | HTTP orchestration | Single file with ApiClient class |
configuration.py | Low-level configuration | Single file with Configuration, RetryParams |
rest.py | HTTP transport | Single file with RESTClientObject |
models/ | Auto-generated data models | 100+ individual model files |
credentials/ | Authentication | oauth2_client.py, credentials.py, config.py |
telemetry/ | Observability | metrics.py, attributes.py, configuration.py |
exceptions.py | Error hierarchy | ApiException, FgaValidationException, etc. |
validation.py | Input validation | ULID validation, parameter checks |
sync/ | Synchronous mirror | Complete parallel implementation |
Auto-generated vs. hand-written:
api/, models/, parts of api_client.py and rest.pyclient/, credentials/, telemetry/, validation.py, exceptions.pySources: openfga_sdk/__init__.py1-241 README.md12
The primary entry point for most applications. Located in openfga_sdk.client.client (async) and openfga_sdk.sync.client (sync).
Responsibilities:
client_batch_check, batch_check)streamed_list_objects)list_relations)ClientConfiguration (store ID, authorization model ID)OpenFgaApi for actual API callsUsage pattern:
Sources: README.md134-149 openfga_sdk/__init__.py3
The direct API interface with one-to-one endpoint mappings. Located in openfga_sdk.api.open_fga_api (async) and openfga_sdk.sync.api (sync).
Responsibilities:
OpenFgaClient internally, but can also be used directly for fine-grained controlAvailable methods: check, batch_check, expand, list_objects, read, write, create_store, delete_store, get_store, list_stores, read_authorization_models, read_authorization_model, write_authorization_model, read_assertions, write_assertions, read_changes, list_users, and streamed_list_objects.
Sources: README.md1314-1353 openfga_sdk/__init__.py1
The HTTP orchestration engine. Located in openfga_sdk.api_client (async) and openfga_sdk.sync.api_client (sync).
Responsibilities:
OAuth2Client for OAuth2 flows)TelemetryMetricsRESTClientObject for actual HTTP I/OSources: openfga_sdk/__init__.py2
Two configuration classes with different scopes:
Configuration (openfga_sdk.configuration):
host, access_token, api_key, api_key_prefix, username, password, discard_unknown_keys, disabled_client_side_validations, server_index, server_variables, server_operation_index, server_operation_variables, ssl_ca_cert, cert_file, key_file, verify_ssl, proxy, proxy_headers, safe_chars_for_path_param, retriesRetryParams (max_retry, min_wait_in_ms)TelemetryConfigurationClientConfiguration (openfga_sdk.client.configuration):
api_url, store_id, authorization_model_id, credentials, retry_params, telemetry, headersOpenFgaClientConfiguration internallySources: README.md139-148 README.md1272-1287 openfga_sdk/__init__.py4-5
Over 100 auto-generated model classes in openfga_sdk.models, representing API request and response structures.
Core models:
TupleKey: Represents a relationship tuple (user, relation, object)Store: A logical container for authorization dataAuthorizationModel: Schema defining types and relationsCheckRequest, CheckResponse: Authorization check operationWriteRequest: Tuple write/delete operationsReadRequest, ReadResponse: Tuple read operationsListObjectsRequest, ListObjectsResponse: List accessible objectsAll models support:
to_dict() and to_str() methodsSources: openfga_sdk/__init__.py15-124 README.md52
This diagram illustrates a typical check operation's path through the SDK layers, showing how application code interacts with OpenFGA:
Key observations:
OpenFgaClient adds convenience (auto-applies store_id, model_id)OpenFgaApi translates to raw API callsApiClient handles cross-cutting concerns (auth, retries, telemetry)Sources: README.md803-825 README.md1264-1311
The SDK has minimal required dependencies to reduce supply chain risk and simplify installation:
| Dependency | Version | Purpose |
|---|---|---|
aiohttp | >=3.9.3 | Async HTTP client |
urllib3 | >=1.26.19, <3 | Sync HTTP client |
python-dateutil | >=2.9.0 | Date/time parsing |
opentelemetry-api | >=1.25.0 | Telemetry integration |
Development dependencies: pytest, pytest-asyncio, pytest-cov, ruff, mypy, mock, griffe
Sources: pyproject.toml37-55
The SDK is a client library that communicates with an OpenFGA server instance over HTTP. It does not implement authorization logic itself—it delegates all authorization decisions to the OpenFGA service.
OpenFGA service responsibilities:
SDK responsibilities:
Sources: README.md59-64
The SDK provides a comprehensive feature set for integrating OpenFGA into Python applications:
| Feature | Description | Page Reference |
|---|---|---|
| Async/Sync Support | Complete dual implementation | Async vs Sync Implementations |
| Store Management | Create, read, update, delete stores | Store Management |
| Authorization Models | Versioned schema management | Authorization Models |
| Tuple Operations | Write, read, delete relationship tuples | Relationship Tuples |
| Check Operations | Single and batch authorization checks | Authorization Checks |
| Advanced Queries | Expand, list_objects, list_users, list_relations | Advanced Queries |
| Batch Operations | Client-side and server-side batching | Batch Operations |
| Streaming | Incremental result processing for large datasets | Streaming Operations |
| Contextual Tuples | Temporary relationships for checks | Contextual Tuples and Conditions |
| Conditions | Attribute-based access control | Contextual Tuples and Conditions |
| Authentication | API token and OAuth2 client credentials | Credentials and Authentication Methods |
| Automatic Retries | Exponential backoff for 429/5xx errors | Retry Logic and Error Handling |
| Telemetry | OpenTelemetry metrics integration | Telemetry and Observability |
| Conflict Options | Idempotent writes (OpenFGA v1.10.0+) | ConflictOptions for Write Operations |
Sources: README.md14-52
Current version: 0.9.9 (as of this documentation)
Python version requirements: 3.10, 3.11, 3.12
OpenFGA server compatibility: The SDK is designed to work with OpenFGA 1.x servers. Some features require specific server versions:
For detailed version compatibility information, see OpenFGA Server Version Compatibility.
Sources: pyproject.toml13 pyproject.toml23-25 pyproject.toml35 README.md830-831 README.md754
This overview establishes the foundation for understanding the OpenFGA Python SDK. For hands-on guidance, proceed to Getting Started. For deep dives into specific subsystems, consult the relevant sections in the table of contents.
Refresh this wiki
This wiki was recently refreshed. Please wait 1 day to refresh again.