Skip to main content

Python

Add API key authentication to your Python application with the Unkey Python SDK. Verify keys on each request to secure your API routes.
1 min read

This guide shows how to add API key verification to your Python applications using the official Unkey Python SDK (v2).

Prerequisites#

  • Python 3.9 or higher
  • An Unkey account (free at unkey.com)

1. Install the SDK#

2. Set up your Unkey credentials#

  1. Create a keyspace in the Unkey Dashboard
  2. Create a root key at Settings → Root Keys

Set your root key as an environment variable:

3. FastAPI Integration#

Here's how to protect your FastAPI endpoints using the v2 SDK:

Run your FastAPI app:

Test with a valid API key:

4. Flask Integration#

For Flask applications using the v2 SDK:

5. Django Integration#

For Django, create a custom middleware using the v2 SDK:

Then in your views:

Creating and Managing Keys#

Here's how to create API keys programmatically using the v2 SDK:

Warning

The full API key is only returned once at creation. Store it securely and never show it again.

Error Handling#

Always handle Unkey errors gracefully:

What's next?#