Python SDK
The Authgate Python SDK is a single-file client library that you can drop directly into your Python application.
Installation
- Download
authgate_api.pyfrom your Authgate purchase - Copy it into your project directory
- Import it in your code
Requirements:
- Python 3.9 or higher
cryptographypackage (for file decryption)
pip install cryptographyQuick Start
from authgate_api import AuthgateAPI, TokenAuthStrategy
# Initialize the client
client = AuthgateAPI(
base_url="https://your-authgate.com/api/integration",
api_key="your-api-key",
api_secret="your-api-secret",
files_encryption_key="your-base64-encryption-key",
request_signing_key="your-base64-signing-key",
request_signing_enabled=True,
response_signing_key="your-base64-response-signing-key",
response_signing_enabled=True
)
# Login with username and password
auth = TokenAuthStrategy(username="user", password="pass")
client.login(auth)
# Get app context
app_context = client.get_app_context()
print(f"Connected to: {app_context.application.name}")
# Check if user has active membership
if app_context.user and app_context.user.membership.is_active:
print("User has active membership!")Authentication
The SDK supports two authentication strategies:
TokenAuthStrategy (Recommended)
Gets a session token on login and automatically refreshes it when needed.
# Login with username/password
auth = TokenAuthStrategy(username="user", password="pass")
client.login(auth)
# Login with license code
auth = TokenAuthStrategy(license_code="LICENSE-CODE")
client.login(auth)
# Login with device authentication (if enabled)
auth = TokenAuthStrategy(
username="user",
password="pass",
hardware_id="device-hardware-id"
)
client.login(auth)LegacyAuthStrategy
Sends credentials with every request. Less efficient but simpler.
from authgate_api import LegacyAuthStrategy
auth = LegacyAuthStrategy(username="user", password="pass")
client.login(auth)Common Operations
Check Membership Status
app_context = client.get_app_context()
if app_context.user and app_context.user.membership.is_active:
# User has access
pass
else:
# Show upgrade prompt
passActivate a License
try:
adjustment = client.activate_license(
code="LICENSE-CODE",
username="user",
password="pass"
)
if adjustment.grants_lifetime_access:
print("Lifetime license activated!")
elif adjustment.minutes:
print(f"Added {adjustment.minutes} minutes")
except ValidationError as e:
print("Invalid license code")Download Files
# Download and decrypt a file
file_data = client.download_file(
file_id="file-uuid",
decrypt=True,
output_path="config.json" # Optional: save to disk
)Access Server Variables
app_context = client.get_app_context()
for variable in app_context.application.variables:
print(f"{variable.name}: {variable.value}")Error Handling
The SDK uses specific exceptions for different error types:
from authgate_api import (
AuthgateApiError,
AuthenticationError,
AuthorizationError,
ValidationError,
NetworkError
)
try:
client.get_app_context()
except AuthenticationError as e:
print("Authentication failed - wrong credentials or not logged in")
except AuthorizationError as e:
print("Access denied - inactive membership")
except ValidationError as e:
# Get field-specific errors
for error in e.get_validation_errors():
print(f"{error['field']}: {error['message']}")
except NetworkError as e:
print("Connection failed")
except AuthgateApiError as e:
print(f"API error: {e}")Device Authentication
If device authentication is enabled on your application, you must provide a hardware_id when logging in or signing up:
During Login:
# Generate a unique hardware ID for this device
import uuid
hardware_id = str(uuid.getnode()) # MAC address
auth = TokenAuthStrategy(
username="user",
password="pass",
hardware_id=hardware_id
)
client.login(auth)During Sign-Up:
# Sign up with username/password and register device
hardware_id = str(uuid.getnode())
user = client.sign_up("username", "password", hardware_id=hardware_id)
# Sign up with license code and register device
user = client.sign_up_with_license_code("LICENSE-CODE", hardware_id=hardware_id)API Reference
AuthgateAPI Class
Constructor:
AuthgateAPI(
base_url: str, # Your Authgate API URL
api_key: str, # Application API key
api_secret: str, # Application API secret
files_encryption_key: str, # Base64 encryption key
request_signing_key: str, # Base64 signing key
request_signing_enabled: bool,
response_signing_key: str, # Base64 response signing key
response_signing_enabled: bool,
verify_ssl: bool = True # Set False for self-signed certs
)Methods:
login(auth_strategy)- Authenticate userlogout()- Clear authenticationget_app_context()- Get application and user infosign_up(username, password, hardware_id=None)- Create new user accountsign_up_with_license_code(code, hardware_id=None)- Create account with licenseactivate_license(code, username, password)- Activate a licensedownload_file(file_id, decrypt=True, output_path=None)- Download a file
Complete Example
from authgate_api import (
AuthgateAPI,
TokenAuthStrategy,
AuthenticationError,
AuthorizationError
)
def main():
# Initialize client
client = AuthgateAPI(
base_url="https://auth.myapp.com/api/integration",
api_key="...",
api_secret="...",
files_encryption_key="...",
request_signing_key="...",
request_signing_enabled=True,
response_signing_key="...",
response_signing_enabled=True
)
try:
# Login
auth = TokenAuthStrategy(username="user", password="pass")
client.login(auth)
# Check access
context = client.get_app_context()
if context.user.membership.is_active:
print("Welcome to the app!")
# Download config file
client.download_file(
file_id=context.application.files[0].id,
output_path="config.json"
)
else:
print("Please purchase a license")
except AuthenticationError:
print("Login failed - wrong credentials")
except AuthorizationError:
print("Access denied - inactive membership")
if __name__ == "__main__":
main()Last updated on