{"library":"openfga-sdk","title":"OpenFGA Python SDK","description":"The OpenFGA Python SDK provides a high-performance and flexible authorization/permission engine client, inspired by Google Zanzibar. It allows developers to integrate OpenFGA for fine-grained access control in their Python applications. The library is actively developed with frequent releases, typically every few weeks.","language":"python","status":"active","last_verified":"Sat May 16","install":{"commands":["pip install openfga-sdk"],"cli":null},"imports":["from openfga_sdk import OpenFgaClient","from openfga_sdk import Credentials","from openfga_sdk.models import WriteRequest, TupleKey, User, Relation, Object, CheckRequest"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import os\nfrom openfga_sdk import OpenFgaClient, Credentials\nfrom openfga_sdk.models import WriteRequest, TupleKey, User, Relation, Object, CheckRequest\n\n# Configure OpenFGA client using environment variables for sensitive data.\n# Required: FGA_API_URL, FGA_STORE_ID\n# Optional (for auth): FGA_API_TOKEN or FGA_CLIENT_ID/FGA_CLIENT_SECRET/FGA_TOKEN_URL/FGA_AUDIENCE\n\nfga_api_url = os.environ.get(\"FGA_API_URL\", \"http://localhost:8080\")\nfga_store_id = os.environ.get(\"FGA_STORE_ID\", \"01H4F8G5K4S8K7J2G8R1T0V9M0\") # Replace with your actual Store ID\n\ncredentials = None\napi_token = os.environ.get(\"FGA_API_TOKEN\")\nif api_token:\n    credentials = Credentials(api_token=api_token)\nelif os.environ.get(\"FGA_CLIENT_ID\") and os.environ.get(\"FGA_CLIENT_SECRET\"):\n    # Example for Client Credentials flow with OAuth2 (replace with your IdP details)\n    credentials = Credentials(\n        client_id=os.environ.get(\"FGA_CLIENT_ID\", \"\"),\n        client_secret=os.environ.get(\"FGA_CLIENT_SECRET\", \"\"),\n        token_url=os.environ.get(\"FGA_TOKEN_URL\", \"https://auth.fga.example.com/oauth/token\"),\n        audience=os.environ.get(\"FGA_AUDIENCE\", fga_api_url) # Audience often matches API URL\n    )\n\nif not fga_store_id:\n    raise ValueError(\"FGA_STORE_ID environment variable is required.\")\n\nclient = OpenFgaClient(\n    api_url=fga_api_url,\n    store_id=fga_store_id,\n    credentials=credentials,\n)\n\ntry:\n    # 1. Write a relationship: \"user:anne can view document:roadmap\"\n    write_response = client.write(\n        body=WriteRequest(\n            writes=[\n                TupleKey(\n                    user=User(id=\"anne\"),\n                    relation=\"viewer\",\n                    object=Object(type=\"document\", id=\"roadmap\")\n                )\n            ]\n        )\n    )\n    print(f\"Wrote relationship: user:anne is viewer of document:roadmap\")\n\n    # 2. Check if \"user:anne can view document:roadmap\"\n    check_response = client.check(\n        body=CheckRequest(\n            user=User(id=\"anne\"),\n            relation=\"viewer\",\n            object=Object(type=\"document\", id=\"roadmap\")\n        )\n    )\n    print(f\"Check result (anne can view roadmap): {check_response.allowed}\") # Expected: True\n\n    # 3. Check if \"user:bob can view document:roadmap\" (assuming bob has no relation)\n    check_response_bob = client.check(\n        body=CheckRequest(\n            user=User(id=\"bob\"),\n            relation=\"viewer\",\n            object=Object(type=\"document\", id=\"roadmap\")\n        )\n    )\n    print(f\"Check result (bob can view roadmap): {check_response_bob.allowed}\") # Expected: False\n\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")\n\n# The client uses an httpx.Client which is typically managed internally.\n# No explicit close() is necessary for OpenFgaClient as of v0.10.0 in most cases.","lang":"python","description":"Initialize the OpenFGA client, write an authorization model tuple, and perform a check. This example demonstrates basic 'Write' and 'Check' operations. Ensure `FGA_API_URL` and `FGA_STORE_ID` (and authentication variables like `FGA_API_TOKEN`) are set in your environment. The provided `fga_store_id` is an example and must be replaced with a real one from your OpenFGA setup.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":{"tag":null,"tag_description":null,"last_tested":"2026-05-16","installed_version":"0.1.1","pypi_latest":"0.10.3","is_stale":true,"summary":{"python_range":"3.10–3.9","success_rate":100,"avg_install_s":4.4,"avg_import_s":1.08,"wheel_type":"wheel"},"results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"openfga-sdk","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.94,"mem_mb":17.7,"disk_size":"32.7M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"openfga-sdk","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":5.2,"import_time_s":0.67,"mem_mb":17.7,"disk_size":"35M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"openfga-sdk","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":1.12,"mem_mb":19.3,"disk_size":"36.1M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"openfga-sdk","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":4.2,"import_time_s":0.99,"mem_mb":19.2,"disk_size":"38M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"openfga-sdk","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":1.29,"mem_mb":19.6,"disk_size":"27.7M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"openfga-sdk","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":3.4,"import_time_s":1.21,"mem_mb":19.6,"disk_size":"30M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"openfga-sdk","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":1.27,"mem_mb":20.6,"disk_size":"27.4M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"openfga-sdk","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":3.6,"import_time_s":1.19,"mem_mb":20.6,"disk_size":"30M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"openfga-sdk","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"30.3M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"openfga-sdk","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":5.5,"import_time_s":null,"mem_mb":null,"disk_size":"33M"}]}}