{"id":5204,"library":"edgegrid-python","title":"Akamai EdgeGrid Authentication for Python","description":"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.","status":"active","version":"2.0.5","language":"en","source_language":"en","source_url":"https://github.com/akamai/AkamaiOPEN-edgegrid-python","tags":["akamai","edgegrid","authentication","requests","api"],"install":[{"cmd":"pip install edgegrid-python","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"While decoupled as a direct dependency in v1.3.0+, this library's primary use case is to provide authentication for `requests.Session` objects.","package":"requests","optional":false}],"imports":[{"symbol":"EdgeGridAuth","correct":"from akamai.edgegrid import EdgeGridAuth"},{"note":"The `EdgeGridAuthHeaders` class is directly importable from the top-level `akamai.edgegrid` package since v1.0.0.","wrong":"from akamai.edgegrid.auth import EdgeGridAuthHeaders","symbol":"EdgeGridAuthHeaders","correct":"from akamai.edgegrid import EdgeGridAuthHeaders"}],"quickstart":{"code":"import requests\nimport os\nfrom akamai.edgegrid import EdgeGridAuth\n\n# For simplicity, using environment variables. \n# In production, use a .edgerc file (e.g., ~/.edgerc) and specify a section.\n# Example .edgerc section:\n# [default]\n# host = your_api_host.akadns.net\n# client_token = YOUR_CLIENT_TOKEN\n# client_secret = YOUR_CLIENT_SECRET\n# access_token = YOUR_ACCESS_TOKEN\n\n# Configure from environment variables (less secure for production)\nclient_token = os.environ.get('AKAMAI_CLIENT_TOKEN', 'YOUR_CLIENT_TOKEN_HERE')\nclient_secret = os.environ.get('AKAMAI_CLIENT_SECRET', 'YOUR_CLIENT_SECRET_HERE')\naccess_token = os.environ.get('AKAMAI_ACCESS_TOKEN', 'YOUR_ACCESS_TOKEN_HERE')\nhost = os.environ.get('AKAMAI_HOST', 'https://your-api-host.akadns.net')\n\nif client_token == 'YOUR_CLIENT_TOKEN_HERE' or \\\n   client_secret == 'YOUR_CLIENT_SECRET_HERE' or \\\n   access_token == 'YOUR_ACCESS_TOKEN_HERE' or \\\n   host == 'https://your-api-host.akadns.net':\n    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.\")\n    print(\"Or configure a .edgerc file and initialize EdgeGridAuth with 'client_token=None' and 'section='your_section_name'\")\nelse:\n    # Initialize EdgeGridAuth with credentials (from env vars in this case)\n    # For .edgerc file, use: auth = EdgeGridAuth(client_token=None, section='default')\n    auth = EdgeGridAuth(\n        client_token=client_token,\n        client_secret=client_secret,\n        access_token=access_token,\n        base_url=host\n    )\n\n    # Create a requests session and apply EdgeGridAuth\n    session = requests.Session()\n    session.auth = auth\n\n    # Example API call (replace with an actual Akamai API endpoint)\n    try:\n        response = session.get(f'{host}/diagnostic-tools/v2/locations') # Example path\n        response.raise_for_status() # Raise an exception for HTTP errors\n        print(f\"Successfully authenticated and fetched data: {response.status_code}\")\n        # print(response.json()) # Uncomment to see the response body\n    except requests.exceptions.HTTPError as e:\n        print(f\"HTTP Error: {e} - {e.response.text}\")\n    except requests.exceptions.RequestException as e:\n        print(f\"Request Error: {e}\")\n","lang":"python","description":"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."},"warnings":[{"fix":"Upgrade your Python environment to 3.9 or newer before upgrading edgegrid-python to 2.0.0 or later.","message":"Version 2.0.0 discontinued support for Python 2.7. The minimum supported Python version became 3.9.","severity":"breaking","affected_versions":"<2.0.0"},{"fix":"Ensure your Python environment is 3.10 or newer when using edgegrid-python 2.0.3 and subsequent versions.","message":"Version 2.0.3 dropped support for Python 3.9. The minimum supported Python version is now 3.10.","severity":"breaking","affected_versions":">=2.0.3"},{"fix":"If you were passing `headers_to_sign` or `max_body` positionally, update your calls to `EdgeGridAuth(..., headers_to_sign=['X-Akamai-Match'], max_body=2048)`.","message":"As of v2.0.0, the `__init__` function of `EdgeGridAuth` and `EdgeGridAuthHeaders` now accepts `headers_to_sign` and `max_body` parameters exclusively as keyword-only arguments. Direct positional arguments for these will raise errors.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Ensure your `.edgerc` file is correctly formatted, accessible, and in the expected location (`~/.edgerc`), or explicitly specify its path and section in the `EdgeGridAuth` constructor (e.g., `EdgeGridAuth(section='my_section', config_file='/path/to/my_edgerc')`). Alternatively, use environment variables like `EG_CLIENT_TOKEN`.","message":"Incorrect configuration of `.edgerc` file path or missing/incorrect environment variables are common causes of authentication failures. The library searches for `~/.edgerc` by default or can be pointed to a custom path.","severity":"gotcha","affected_versions":"All"},{"fix":"For requests with larger bodies, increase `max_body` when initializing `EdgeGridAuth` (e.g., `EdgeGridAuth(..., max_body=16384)`). Be aware that extremely large `max_body` values can impact performance due to increased hash computation.","message":"The `max_body` parameter (default 2048 bytes) truncates request bodies for signature generation if the body exceeds this size. For large POST/PUT requests, especially those with binary data, this can lead to signature mismatches if the API expects the full body to be signed.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}