Akamai EdgeGrid Authentication for Python

2.0.5 · active · verified Mon Apr 13

edgegrid-python provides the client authentication protocol for accessing Akamai's APIs, designed to integrate seamlessly with the popular `requests` library. It handles the cryptographic signing of requests required by the Akamai EdgeGrid specification. The current version is 2.0.5, with frequent releases primarily focused on security updates and dependency management, and less frequent but significant feature or breaking changes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to authenticate `requests` sessions using `EdgeGridAuth` with credentials typically loaded from environment variables or an `.edgerc` file. It constructs a `requests.Session` object, applies the `EdgeGridAuth` instance, and then makes an example GET request to an Akamai API endpoint, handling potential errors.

import requests
import os
from akamai.edgegrid import EdgeGridAuth

# For simplicity, using environment variables. 
# In production, use a .edgerc file (e.g., ~/.edgerc) and specify a section.
# Example .edgerc section:
# [default]
# host = your_api_host.akadns.net
# client_token = YOUR_CLIENT_TOKEN
# client_secret = YOUR_CLIENT_SECRET
# access_token = YOUR_ACCESS_TOKEN

# Configure from environment variables (less secure for production)
client_token = os.environ.get('AKAMAI_CLIENT_TOKEN', 'YOUR_CLIENT_TOKEN_HERE')
client_secret = os.environ.get('AKAMAI_CLIENT_SECRET', 'YOUR_CLIENT_SECRET_HERE')
access_token = os.environ.get('AKAMAI_ACCESS_TOKEN', 'YOUR_ACCESS_TOKEN_HERE')
host = os.environ.get('AKAMAI_HOST', 'https://your-api-host.akadns.net')

if client_token == 'YOUR_CLIENT_TOKEN_HERE' or \
   client_secret == 'YOUR_CLIENT_SECRET_HERE' or \
   access_token == 'YOUR_ACCESS_TOKEN_HERE' or \
   host == 'https://your-api-host.akadns.net':
    print("Warning: Please set AKAMAI_CLIENT_TOKEN, AKAMAI_CLIENT_SECRET, AKAMAI_ACCESS_TOKEN, and AKAMAI_HOST environment variables or update the example with actual credentials.")
    print("Or configure a .edgerc file and initialize EdgeGridAuth with 'client_token=None' and 'section='your_section_name'")
else:
    # Initialize EdgeGridAuth with credentials (from env vars in this case)
    # For .edgerc file, use: auth = EdgeGridAuth(client_token=None, section='default')
    auth = EdgeGridAuth(
        client_token=client_token,
        client_secret=client_secret,
        access_token=access_token,
        base_url=host
    )

    # Create a requests session and apply EdgeGridAuth
    session = requests.Session()
    session.auth = auth

    # Example API call (replace with an actual Akamai API endpoint)
    try:
        response = session.get(f'{host}/diagnostic-tools/v2/locations') # Example path
        response.raise_for_status() # Raise an exception for HTTP errors
        print(f"Successfully authenticated and fetched data: {response.status_code}")
        # print(response.json()) # Uncomment to see the response body
    except requests.exceptions.HTTPError as e:
        print(f"HTTP Error: {e} - {e.response.text}")
    except requests.exceptions.RequestException as e:
        print(f"Request Error: {e}")

view raw JSON →