Auth0 Python SDK - Management and Authentication APIs
Project description
Auth0 Python Library
The Auth0 Python library provides convenient access to the Auth0 APIs from Python.
Table of Contents
- Installation
- Reference
- Authentication API
- Management API
- Async Client
- Exception Handling
- Pagination
- Advanced
- Feedback
Installation
pip install auth0-python==5.0.0
Requirements:
- Python ≥3.8 (Python 3.7 support has been dropped)
Reference
A full reference for this library is available here.
Authentication API
The Authentication API is used for authentication flows such as obtaining tokens via client credentials, authorization codes, or resource owner password grants:
from auth0.authentication import GetToken
token_client = GetToken(
domain="your-tenant.auth0.com",
client_id="YOUR_CLIENT_ID",
client_secret="YOUR_CLIENT_SECRET",
)
# Get an access token using client credentials
token_response = token_client.client_credentials(
audience="https://your-tenant.auth0.com/api/v2/"
)
access_token = token_response["access_token"]
Management API
Recommended: Using ManagementClient
The ManagementClient is the recommended way to interact with the Auth0 Management API. It provides a simpler interface using just your Auth0 domain, and supports automatic token management with client credentials:
from auth0.management import ManagementClient
# With an existing token
client = ManagementClient(
domain="your-tenant.auth0.com",
token="YOUR_TOKEN",
)
# Or with client credentials (automatic token acquisition and refresh)
client = ManagementClient(
domain="your-tenant.auth0.com",
client_id="YOUR_CLIENT_ID",
client_secret="YOUR_CLIENT_SECRET",
)
For async usage:
import asyncio
from auth0.management import AsyncManagementClient
client = AsyncManagementClient(
domain="your-tenant.auth0.com",
token="YOUR_TOKEN",
)
async def main() -> None:
users = await client.users.list()
print(users)
asyncio.run(main())
Using a Token from the Authentication API
You can obtain a token using the Authentication API and use it with the Management API client:
from auth0.authentication import GetToken
from auth0.management import Auth0
domain = "your-tenant.auth0.com"
# Get a token using the Authentication API
token_client = GetToken(
domain=domain,
client_id="YOUR_CLIENT_ID",
client_secret="YOUR_CLIENT_SECRET",
)
token_response = token_client.client_credentials(
audience=f"https://{domain}/api/v2/"
)
access_token = token_response["access_token"]
# Use the token with the Management API client
client = Auth0(
base_url=f"https://{domain}/api/v2",
token=access_token,
)
Using the Base Client
Alternatively, you can use the Auth0 client directly with a full base URL:
from auth0.management import ActionTrigger, Auth0
client = Auth0(
base_url="https://YOUR_TENANT.auth0.com/api/v2",
token="YOUR_TOKEN",
)
client.actions.create(
name="name",
supported_triggers=[
ActionTrigger(
id="id",
)
],
)
Async Client
The SDK also exports an async client so that you can make non-blocking calls to our API. Note that if you are constructing an Async httpx client class to pass into this client, use httpx.AsyncClient() instead of httpx.Client() (e.g. for the httpx_client parameter of this client).
import asyncio
from auth0.management import ActionTrigger, AsyncAuth0
client = AsyncAuth0(
base_url="https://YOUR_TENANT.auth0.com/api/v2",
token="YOUR_TOKEN",
)
async def main() -> None:
await client.actions.create(
name="name",
supported_triggers=[
ActionTrigger(
id="id",
)
],
)
asyncio.run(main())
Exception Handling
When the API returns a non-success status code (4xx or 5xx response), a subclass of the following error will be thrown.
from auth0.management.core.api_error import ApiError
try:
client.actions.create(...)
except ApiError as e:
print(e.status_code)
print(e.body)
Pagination
Paginated requests will return a SyncPager or AsyncPager, which can be used as generators for the underlying object.
from auth0.management import Auth0
client = Auth0(
base_url="https://YOUR_TENANT.auth0.com/api/v2",
token="YOUR_TOKEN",
)
response = client.actions.list(
trigger_id="post-login",
action_name="actionName",
deployed=True,
page=1,
per_page=1,
installed=True,
)
for item in response:
print(item)
# alternatively, you can paginate page-by-page
for page in response.iter_pages():
print(page)
# You can also iterate through pages and access the typed response per page
pager = client.actions.list(...)
for page in pager.iter_pages():
print(page.response) # access the typed response for each page
for item in page:
print(item)
Advanced
Access Raw Response Data
The SDK provides access to raw response data, including headers, through the .with_raw_response property.
The .with_raw_response property returns a "raw" client that can be used to access the .headers and .data attributes.
from auth0.management import Auth0
client = Auth0(
base_url="https://YOUR_TENANT.auth0.com/api/v2",
token="YOUR_TOKEN",
)
response = client.actions.with_raw_response.create(...)
print(response.headers) # access the response headers
print(response.data) # access the underlying object
pager = client.actions.list(...)
print(pager.response) # access the typed response for the first page
for item in pager:
print(item) # access the underlying object(s)
for page in pager.iter_pages():
print(page.response) # access the typed response for each page
for item in page:
print(item) # access the underlying object(s)
Retries
The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long as the request is deemed retryable and the number of retry attempts has not grown larger than the configured retry limit (default: 2).
A request is deemed retryable when any of the following HTTP status codes is returned:
Use the max_retries request option to configure this behavior.
client.actions.create(..., request_options={
"max_retries": 1
})
Timeouts
The SDK defaults to a 60 second timeout. You can configure this with a timeout option at the client or request level.
from auth0.management import Auth0
client = Auth0(
base_url="https://YOUR_TENANT.auth0.com/api/v2",
token="YOUR_TOKEN",
timeout=20.0,
)
# Override timeout for a specific method
client.actions.create(..., request_options={
"timeout_in_seconds": 1
})
Custom Client
You can override the httpx client to customize it for your use-case. Some common use-cases include support for proxies
and transports.
import httpx
from auth0.management import Auth0
client = Auth0(
base_url="https://YOUR_TENANT.auth0.com/api/v2",
token="YOUR_TOKEN",
httpx_client=httpx.Client(
proxy="http://my.test.proxy.example.com",
transport=httpx.HTTPTransport(local_address="0.0.0.0"),
),
)
Feedback
Contributing
We appreciate feedback and contribution to this repo! Before you get started, please see the following:
Raise an issue
To provide feedback or report a bug, please raise an issue on our issue tracker.
Vulnerability Reporting
Please do not report security vulnerabilities on the public GitHub issue tracker. The Responsible Disclosure Program details the procedure for disclosing security issues.
Auth0 is an easy to implement, adaptable authentication and authorization platform. To learn more checkout Why Auth0
This project is licensed under the MIT license. See the LICENSE file for more info
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file auth0_python-5.0.0.tar.gz.
File metadata
- Download URL: auth0_python-5.0.0.tar.gz
- Upload date:
- Size: 659.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
deebf16e045b2cc2e85a011069cc8eabf072485dd75a1ab8f5a5bfdb024a3fbd
|
|
| MD5 |
07770f7f7fa8355618236b886065eaca
|
|
| BLAKE2b-256 |
7548a799b0dcdbfc1e8654b8a4bb4c7d8c61d9d3909c3cb1601f35e7f10a347b
|
Provenance
The following attestation bundles were made for auth0_python-5.0.0.tar.gz:
Publisher:
publish.yml on auth0/auth0-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
auth0_python-5.0.0.tar.gz -
Subject digest:
deebf16e045b2cc2e85a011069cc8eabf072485dd75a1ab8f5a5bfdb024a3fbd - Sigstore transparency entry: 910298653
- Sigstore integration time:
-
Permalink:
auth0/auth0-python@ae22a65eb94b1c5a376c825757d43f3fbec78996 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/auth0
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@ae22a65eb94b1c5a376c825757d43f3fbec78996 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file auth0_python-5.0.0-py3-none-any.whl.
File metadata
- Download URL: auth0_python-5.0.0-py3-none-any.whl
- Upload date:
- Size: 2.0 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
64662edd7767ba85cb20f3fbd95998f30713bfdd3c9251193cc9b75690cc79b7
|
|
| MD5 |
ed65fc9b2867e66a209cde5fc7e89166
|
|
| BLAKE2b-256 |
f0d5132d4cdf6c10f0250ab9eaffa2ba860eba12903a28273495d57e2d236a82
|
Provenance
The following attestation bundles were made for auth0_python-5.0.0-py3-none-any.whl:
Publisher:
publish.yml on auth0/auth0-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
auth0_python-5.0.0-py3-none-any.whl -
Subject digest:
64662edd7767ba85cb20f3fbd95998f30713bfdd3c9251193cc9b75690cc79b7 - Sigstore transparency entry: 910298655
- Sigstore integration time:
-
Permalink:
auth0/auth0-python@ae22a65eb94b1c5a376c825757d43f3fbec78996 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/auth0
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@ae22a65eb94b1c5a376c825757d43f3fbec78996 -
Trigger Event:
workflow_dispatch
-
Statement type: