Python SDK for OpenFGA
This is an autogenerated python SDK for OpenFGA. It provides a wrapper around the OpenFGA API definition.
Table of Contents
About
OpenFGA is an open source Fine-Grained Authorization solution inspired by Google's Zanzibar paper. It was created by the FGA team at Auth0 based on Auth0 Fine-Grained Authorization (FGA), available under a permissive license (Apache-2) and welcomes community contributions.
OpenFGA is designed to make it easy for application builders to model their permission layer, and to add and integrate fine-grained authorization into their applications. OpenFGA’s design is optimized for reliability and low latency at a high scale.
Resources
Installation
pip install
PyPI
The openfga_sdk is available to be downloaded via PyPI, you can install directly using:
pip3 install openfga_sdk
(you may need to run pip with root permission: sudo pip3 install openfga_sdk)
Then import the package:
import openfga_sdk
GitHub
The openfga_sdk is also hosted in GitHub, you can install directly using:
pip3 install https://github.com/openfga/python-sdk.git
(you may need to run pip with root permission: sudo pip3 install https://github.com/openfga/python-sdk.git)
Then import the package:
import openfga_sdk
Setuptools
Install via Setuptools.
python setup.py install --user
(or sudo python setup.py install to install the package for all users)
Then import the package:
import openfga_sdk
Getting Started
Initializing the API Client
Learn how to initialize your SDK
We strongly recommend you initialize the OpenFgaClient only once and then re-use it throughout your app, otherwise you will incur the cost of having to re-initialize multiple times or at every request, the cost of reduced connection pooling and re-use, and would be particularly costly in the client credentials flow, as that flow will be preformed on every request.
The OpenFgaClient will by default retry API requests up to 3 times on 429 and 5xx errors.
No Credentials
from openfga_sdk import ClientConfiguration, OpenFgaClient
async def main():
configuration = ClientConfiguration(
api_url=FGA_API_URL, # required
store_id=FGA_STORE_ID, # optional, not needed when calling `CreateStore` or `ListStores`
authorization_model_id=FGA_MODEL_ID, # Optional, can be overridden per request
)
# Enter a context with an instance of the OpenFgaClient
async with OpenFgaClient(configuration) as fga_client:
api_response = await fga_client.read_authorization_models()
return api_response
API Token
from openfga_sdk import ClientConfiguration, OpenFgaClient
from openfga_sdk.credentials import CredentialConfiguration, Credentials
async def main():
configuration = ClientConfiguration(
api_url=FGA_API_URL, # required
store_id=FGA_STORE_ID, # optional, not needed when calling `CreateStore` or `ListStores`
authorization_model_id=FGA_MODEL_ID, # Optional, can be overridden per request
credentials=Credentials(
method='api_token',
configuration=CredentialConfiguration(
api_token=FGA_API_TOKEN,
)
)
)
# Enter a context with an instance of the OpenFgaClient
async with OpenFgaClient(configuration) as fga_client:
api_response = await fga_client.read_authorization_models()
return api_response
Client Credentials
from openfga_sdk import ClientConfiguration, OpenFgaClient
from openfga_sdk.credentials import Credentials, CredentialConfiguration
async def main():
configuration = ClientConfiguration(
api_url=FGA_API_URL, # required
store_id=FGA_STORE_ID, # optional, not needed when calling `CreateStore` or `ListStores`
authorization_model_id=FGA_MODEL_ID, # Optional, can be overridden per request
credentials=Credentials(
method='client_credentials',
configuration=CredentialConfiguration(
api_issuer=FGA_API_TOKEN_ISSUER,
api_audience=FGA_API_AUDIENCE, # optional, required for Auth0; omit for standard OAuth2
client_id=FGA_CLIENT_ID,
client_secret=FGA_CLIENT_SECRET,
# scopes="read write", # optional, space-separated OAuth2 scopes
)
)
)
# Enter a context with an instance of the OpenFgaClient
async with OpenFgaClient(configuration) as fga_client:
api_response = await fga_client.read_authorization_models()
return api_response
Note: api_issuer accepts either a hostname (e.g., issuer.fga.example, which defaults to https://<hostname>/oauth/token) or a full token endpoint URL (e.g., https://oauth.fga.example/token). Use the full URL when your OAuth2 provider uses a non-standard token endpoint path.
OAuth2 Client Credentials (Standard OAuth2)
For OAuth2 providers that use scope instead of audience:
from openfga_sdk import ClientConfiguration, OpenFgaClient
from openfga_sdk.credentials import Credentials, CredentialConfiguration
async def main():
configuration = ClientConfiguration(
api_url=FGA_API_URL, # required
store_id=FGA_STORE_ID, # optional
authorization_model_id=FGA_MODEL_ID, # optional
credentials=Credentials(
method='client_credentials',
configuration=CredentialConfiguration(
api_issuer="https://oauth.fga.example/token", # full token endpoint URL
client_id=FGA_CLIENT_ID,
client_secret=FGA_CLIENT_SECRET,
scopes="email profile", # space-separated OAuth2 scopes
)
)
)
async with OpenFgaClient(configuration) as fga_client:
api_response = await fga_client.read_authorization_models()
return api_response
Custom Headers
Default Headers
You can configure default headers that will be sent with every request by passing them to the ClientConfiguration:
from openfga_sdk import ClientConfiguration, OpenFgaClient
async def main():
configuration = ClientConfiguration(
api_url=FGA_API_URL,
store_id=FGA_STORE_ID,
authorization_model_id=FGA_MODEL_ID,
headers={
"X-Custom-Header": "default-value",
"X-Request-Source": "my-app",
},
)
async with OpenFgaClient(configuration) as fga_client:
api_response = await fga_client.read_authorization_models()
return api_response
Per-Request Headers
You can also send custom headers on a per-request basis by using the options parameter. Per-request headers will override any default headers with the same name.
from openfga_sdk import ClientConfiguration, OpenFgaClient
from openfga_sdk.client.models import ClientCheckRequest
async def main():
configuration = ClientConfiguration(
api_url=FGA_API_URL,
store_id=FGA_STORE_ID,
authorization_model_id=FGA_MODEL_ID,
)
async with OpenFgaClient(configuration) as fga_client:
# Add custom headers to a specific request
result = await fga_client.check(
body=ClientCheckRequest(
user="user:anne",
relation="viewer",
object="document:roadmap",
),
options={
"headers": {
"X-Request-ID": "123e4567-e89b-12d3-a456-426614174000",
"X-Custom-Header": "custom-value", # Overrides default header value
}
}
)
return result
Synchronous Client
To run outside of an async context, the project exports a synchronous client
from openfga_sdk.sync that supports all the credential types and calls,
without requiring async/await.
from openfga_sdk.client import ClientConfiguration
from openfga_sdk.sync import OpenFgaClient
def main():
configuration = ClientConfiguration(
api_url=FGA_API_URL, # required
store_id=FGA_STORE_ID, # optional, not needed when calling `CreateStore` or `ListStores`
authorization_model_id=FGA_MODEL_ID, # optional, can be overridden per request
)
# Enter a context with an instance of the OpenFgaClient
with OpenFgaClient(configuration) as fga_client:
api_response = fga_client.read_authorization_models()
return api_response
Get your Store ID
You need your store id to call the OpenFGA API (unless it is to call the CreateStore or ListStores methods).
If your server is configured with authentication enabled, you also need to have your credentials ready.
Calling the API
Stores
List Stores
Get a paginated list of stores.
API Documentation
# from openfga_sdk.sync import OpenFgaClient
# Initialize the fga_client
# fga_client = OpenFgaClient(configuration)
options = {"page_size": 25, "continuation_token": "eyJwayI6IkxBVEVTVF9OU0NPTkZJR19hdXRoMHN0b3JlIiwic2siOiIxem1qbXF3MWZLZExTcUoyN01MdTdqTjh0cWgifQ=="}
response = await fga_client.list_stores(options)
# response = ListStoresResponse(...)
# response.stores = [Store({"id": "01FQH7V8BEG3GPQW93KTRFR8JB", "name": "FGA Demo Store", "created_at": "2022-01-01T00:00:00.000Z", "updated_at": "2022-01-01T00:00:00.000Z"})]
Create Store
Create and initialize a store.
API Documentation
# from openfga_sdk import CreateStoreRequest, OpenFgaClient
# Initialize the fga_client
# fga_client = OpenFgaClient(configuration)
body = CreateStoreRequest(
name = "FGA Demo Store",
)
response = await fga_client.create_store(body)
# response.id = "01FQH7V8BEG3GPQW93KTRFR8JB"
Get Store
Get information about the current store.