Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/iwpnd/fastapi-key-auth
FastAPI API key authorizer
https://github.com/iwpnd/fastapi-key-auth
fastapi fastapi-middleware fastapi-security middleware
Last synced: about 7 hours ago
JSON representation
FastAPI API key authorizer
- Host: GitHub
- URL: https://github.com/iwpnd/fastapi-key-auth
- Owner: iwpnd
- License: mit
- Created: 2021-04-21T16:48:39.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2025-01-27T04:04:41.000Z (10 days ago)
- Last Synced: 2025-01-28T21:12:35.004Z (8 days ago)
- Topics: fastapi, fastapi-middleware, fastapi-security, middleware
- Language: Python
- Homepage:
- Size: 1020 KB
- Stars: 38
- Watchers: 4
- Forks: 9
- Open Issues: 5
-
Metadata Files:
- Readme: readme.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
FastAPI-key-auth
Secure your FastAPI endpoints using API keys.
Report Bug
·
Request Feature
Table of Contents
## About The Project
On deployment inject API keys authorized to use your service. Every call to a private
endpoint of your service has to include a `header['x-api-key']` attribute that is
validated against the API keys in your environment.
If it is present, a request is authorized. If it is not FastAPI return `401 Unauthorized`.
Use this either as a middleware, or as Dependency.### Built With
- [starlette](https://github.com/encode/starlette)
- [fastapi](https://github.com/tiangolo/fastapi)## Getting Started
### Installation
1. Clone and install
```sh
git clone https://github.com/iwpnd/fastapi-key-auth.git
poetry install
```
2. Install with pip
```sh
pip install fastapi-key-auth
```
3. Install with poetry
```sh
poetry add fastapi-key-auth
```## Usage
As Middleware:
```python
from fastapi import FastAPI
from fastapi_key_auth import AuthorizerMiddlewareapp = FastAPI()
app.add_middleware(AuthorizerMiddleware, public_paths=["/ping"], key_pattern="API_KEY_")
# optional use regex startswith
app.add_middleware(AuthorizerMiddleware, public_paths=["/ping", "^/users"])
```As Dependency
```python
from fastapi import FastAPI, Depends
from fastapi_key_auth import AuthorizerDependencyauthorizer = AuthorizerDependency(key_pattern="API_KEY_")
# either globally or in a router
app = FastAPI(dependencies=[Depends(authorizer)])
```## License
Distributed under the MIT License. See `LICENSE` for more information.
## Contact
Benjamin Ramser - [@imwithpanda](https://twitter.com/imwithpanda) - [email protected]
Project Link: [https://github.com/iwpnd/fastapi-key-auth](https://github.com/iwpnd/fastapi-key-auth)