Skip to main content

Simple Flask decorator for requiring an API key

Project description

Flask API Key Decorator

A simple decorator for requiring an API key in order to access flask endpoints or methods.

It checks for an API key and adds the requirement to the swagger documentation.

Installation

pip install flask-api-key-decorator

Usage

Import the library

from require_api_key import require_api_key

Use it to decorate your Flask routes:

@app.route('/')
@require_api_key
def home():
    return "Hello, World!"

By default, the decorator looks for the API key in the 'x-api-key' header. If you want to use a different header, you can specify it using the header_name parameter:

@app.route('/')
@require_api_key(header_name='custom-header')
def home():
    return "Hello, World!"

Setting the API key

The API key can be set in two ways:

  1. As an environment variable named API_KEY
  2. As an argument to the decorator
@app.route('/')
@require_api_key(key='your-api-key')
def home():
    return "Hello, World!"

To set multiple API keys, separate them with a comma.

export API_KEY=your-api-key-1,your-api-key-2

License

MIT (see LICENSE.md

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask_api_key_decorator-0.1.3.tar.gz (2.8 kB view hashes)

Uploaded Source

Built Distribution

flask_api_key_decorator-0.1.3-py3-none-any.whl (3.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page