Python Client Library

On this page

The Python client library integrates FusionAuth with your Python application. The source code is in the Python client GitHub repository.

Install#

Install the fusionauth-client package from PyPI:

$ pip install fusionauth-client

Create and Retrieve a User#

The following examples assume FusionAuth runs at http://localhost:9011. Each example contains an API key. Replace the API key with your own. When FusionAuth doesn't run locally, replace the host URL too.

The following example uses the create_user method to create a User. Then the example uses the retrieve_user_by_email method to retrieve the User by email address:

from fusionauth.fusionauth_client import FusionAuthClient

# supply your own API key and URL
client = FusionAuthClient('6b87a398-39f2-4692-927b-13188a81a9a3', 'http://localhost:9011')

user_request = {
    'sendSetPasswordEmail': False,
    'skipVerification': True,
    'user': {
        'email': 'art@vandaleyindustries.com',
        'password': 'password'
    }
}

client_response = client.create_user(user_request)

# Create a User
if client_response.was_successful():
    print(client_response.success_response)
else:
    print(client_response.error_response)

# Retrieve a user by email address
client_response = client.retrieve_user_by_email('art@vandaleyindustries.com')
if client_response.was_successful():
    print(client_response.success_response)
else:
    print(client_response.error_response)

Usage Suggestions#

Choose a Use Case#

Client libraries wrap the REST API. You can use a client library for two tasks.

The main task is calling the FusionAuth APIs from code. You get auto-completion and typed objects, which helps when you script configuration, automate common tasks, or copy existing Applications and Groups. The API documentation lists the JSON structure of every request and which fields each call requires. When you need the details, read the API documentation and the library source code.

The secondary task is exchanging an authorization code for tokens in the Authorization Code grant. For this task, use an OAuth library for your language instead. For a list of libraries, see the community curated list.

Client libraries don't provide higher level functionality such as token management. To request that functionality, comment on or upvote the open issue.

When a client library doesn't fit your needs, call the REST API directly.

Requests and Responses#

Each library maps a request object to the JSON that the matching API method requires. Depending on the API call, the request object is a set of string parameters or a complex object. When you use a language without static typing, read the API documentation for the operation to learn the expected fields.

Each library returns a response object with these fields:

  • A status that matches the HTTP status code from the API. The status is -1 when no HTTP request succeeded.
  • A JSON success object when the call succeeds.
  • A JSON error object with a message when the status is 4xx or 5xx.
  • An exception object when the library sent no request or the server returned no usable response.

Use Separate Clients for API Key Scopes#

When a script calls endpoints with different API key scopes, create one client instance per scope. For example, Entities accept a tenant-scoped API key, but Entity Types and Lambdas require a global API key.

Follow these rules when your library lets you set a Tenant Id on the client instance:

  • Set the Tenant Id on the tenant-scoped client only.
  • Don't change the Tenant Id on a shared client between calls.
  • Don't send a tenant header to a global-only endpoint. The endpoint fails with a 401.

Example Apps#

  • Anonymous user example - Create a shadow or anonymous user account before full registration
  • AWS Agentcore example - Example of using the client credentials grant to secure your AWS Agentcore agents
  • Flask profile portal - A user profile portal built with Flask
  • Mocking code example - Example of mocking code in python
  • Python Django and Google sign-in - a Django web application with user registration and authentication via FusionAuth and Google
  • Python Django quickstart - Python Django quickstart tutorial showing how to integrate FusionAuth with a Python Django application
  • Python Flask quickstart - Python Flask quickstart tutorial showing how to integrate FusionAuth with a Python Flask application
  • python JWT - JWT creation and decoding examples with the python python-jwt library
  • QR Code Example - An example Python/Flask application that uses an OAuth Device Code Grant workflow to log a user in and get them access and refresh tokens across devices.
  • Why Mocking Sucks - Firebase - Example repo for the Why mocking sucks post illustrating testing Firebase services with mocking vs with the Firebase emulator.
  • Why Mocking Sucks - FusionAuth - Example repo for the Why mocking sucks post illustrating testing FusionAuth with mocking vs with the development server.
  • Wifi Hotspot - Controlling a hotspot via FusionAuth

Update Policy#

FusionAuth releases new versions of the SDKs and client libraries to track the FusionAuth API. SDKs and client libraries may also receive bug fixes, security patches, tests, code samples, and documentation changes.

These releases may update dependencies, language engines, and operating systems, because FusionAuth follows the deprecation and sunsetting policies of the underlying technologies. When the maintainer deprecates a language, framework, or operating system, FusionAuth deprecates the SDKs and client libraries that depend on the technology. FusionAuth then updates the SDKs and client libraries to use a newer version.

For more information, see Update a Client Library.