Skip to content

Python SDK - FastAPI & Flask

FastAPI and Flask. Requires Python 3.9+.

Install

pip install kohi-python python-dotenv

Add to .env:

KOHI_PROJECT_KEY=pk_your_project_key
KOHI_SECRET_KEY=your_secret_key

FastAPI

Monitor() goes right after app = FastAPI() and must be before any @app.get/@app.post route definitions.

import os
from dotenv import load_dotenv
from fastapi import FastAPI
from kohi import Monitor
load_dotenv()
app = FastAPI()
monitor = Monitor(
app,
project_key=os.getenv("KOHI_PROJECT_KEY"),
secret_key=os.getenv("KOHI_SECRET_KEY"),
)

Flask

Monitor() goes right after app = Flask(__name__) and must be before any @app.route definitions.

import os
from dotenv import load_dotenv
from flask import Flask
from kohi import Monitor
load_dotenv()
app = Flask(__name__)
monitor = Monitor(
app,
project_key=os.getenv("KOHI_PROJECT_KEY"),
secret_key=os.getenv("KOHI_SECRET_KEY"),
)

Error tracking

Uncaught exceptions and 5xx responses are captured automatically in both FastAPI and Flask. Thread-level exceptions are also caught via threading.excepthook.

To capture errors manually (store the Monitor() return value as shown above):

try:
risky_operation()
except Exception as exc:
monitor.capture_exception(exc)

capture_exception(exc) records the exception type, message, and full traceback. 5xx responses and unhandled exceptions are captured automatically.

See configuration reference for all available options, security & privacy for redacted fields, or troubleshooting if something isn’t working.