apilytics-python
Installation
-
Sign up and get your API key from https://apilytics.io - we offer a completely free trial with no credit card required!
-
Install this package:
pip install apilytics
- Enable the middleware and set your API key:
A good practice is to securely store the API key as an environment variable.
You can leave the env variable unset in e.g. development and test environments, the middleware will be automatically disabled if the key isNone
.
Django:
settings.py
:
import os
APILYTICS_API_KEY = os.getenv("APILYTICS_API_KEY")
MIDDLEWARE = [
"apilytics.django.ApilyticsMiddleware",
]
FastAPI:
main.py
:
import os
from apilytics.fastapi import ApilyticsMiddleware
from fastapi import FastAPI
app = FastAPI()
app.add_middleware(ApilyticsMiddleware, api_key=os.getenv("APILYTICS_API_KEY"))
Other Python Frameworks:
You can easily build your own middleware which measures the execution time and sends the metrics:
my_apilytics_middleware.py
:
import os
from apilytics.core import ApilyticsSender
def my_apilytics_middleware(request, get_response):
with ApilyticsSender(
api_key=os.getenv("APILYTICS_API_KEY"),
path=request.path,
method=request.method,
) as sender:
response = get_response(request)
sender.set_response_info(status_code=response.status_code)
return response
Frequently Asked Questions
Does the middleware slow down my backend?
- No. The middleware does all of its requests to the Apilytics API in a background thread pool, so it will not slow down your normal request handling.
apilytics
have?
What 3rd party dependencies does - None besides the frameworks that you use it in.
What Python versions does the package work with?
apilytics
is tested to work on all the currently supported versions of Python: 3.7, 3.8, 3.9, and 3.10.