Duo Security Python Client
The `duo-client` library provides a reference Python client for interacting with Duo Security's Auth, Admin, and Accounts APIs. It is actively maintained with frequent releases, offering programmatic access to manage users, policies, and authentication events. The current version is 5.6.1, and it supports Python 3.7 and higher.
Warnings
- breaking Version 5.0.0 dropped support for Python versions below 3.7. Attempting to use `duo-client` 5.0.0 or later on older Python environments will result in errors.
- breaking As of version 5.2.0, the client enforces the documented API usage requiring the child account hostname when using Admin API in conjunction with the Accounts API in multi-account setups. While the client may attempt to look up the hostname if not provided, explicitly defining it is the correct and reliable approach.
- deprecated Version 5.6.0 removed deprecated mobile restore parameters from settings. Code attempting to use these parameters will fail.
- breaking Duo Security will no longer trust the DigiCert G1 root certificate after April 15, 2026. This is an external CA bundle expiry that will affect *all* Duo integrations, potentially causing connection failures if the underlying client (duo-client) and operating system do not support and trust the replacement DigiCert G5 root and use modern TLS (1.2/1.3).
- gotcha Duo API Integration Keys (IKEYs), Secret Keys (SKEYs), and API Hostnames (HOSTs) are highly sensitive credentials. Hardcoding them directly into source code is a major security risk.
Install
-
pip install duo-client
Imports
- AdminApi
from duo_client.admin import AdminApi
- AuthApi
from duo_client.auth import AuthApi
- AccountsApi
from duo_client.accounts import AccountsApi
Quickstart
import os
from duo_client.auth import AuthApi
# It's crucial to load credentials from environment variables or a secure secret store.
# DO NOT hardcode IKEY, SKEY, or HOST in your application code.
IKEY = os.environ.get('DUO_IKEY', 'DIXXXXXXXXXXXXXXXXXX') # Replace with your actual Integration Key
SKEY = os.environ.get('DUO_SKEY', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') # Replace with your actual Secret Key
HOST = os.environ.get('DUO_HOST', 'api-xxxxxxxx.duosecurity.com') # Replace with your actual API Hostname
if IKEY == 'DIXXXXXXXXXXXXXXXXXX' or SKEY == 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' or HOST == 'api-xxxxxxxx.duosecurity.com':
print("Warning: Please set DUO_IKEY, DUO_SKEY, and DUO_HOST environment variables or replace placeholder values.")
print("Skipping quickstart execution due to placeholder credentials.")
else:
try:
# Initialize the Auth API client
auth_api = AuthApi(
ikey=IKEY,
skey=SKEY,
host=HOST,
)
# Make a simple API call to check service status
response = auth_api.check()
print("Duo Auth API Check successful:")
print(response)
except Exception as e:
print(f"Error checking Duo Auth API: {e}")