Skip to main content
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:
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?

Add rate limiting

Protect your endpoints from abuse

Python SDK Reference

Complete Python SDK documentation

Authorization

Add roles and permissions

Cookbook

More Python recipes and examples
Last modified on June 2, 2026