Star us on GitHub
Star
Menu

Using highlight.io with Python FastAPI

Learn how to set up highlight.io on your Python FastAPI backend API.

1

Configure client-side Highlight. (optional)

If you're using Highlight on the frontend for your application, make sure you've initialized it correctly and followed the fullstack mapping guide.

2

Install the highlight-io python package.

Download the package from pypi and save it to your requirements. If you use a zip or s3 file upload to publish your function, you will want to make sure highlight-io is part of the build.

poetry add highlight-io
Copy
# or with pip pip install highlight-io
Copy
3

Initialize the Highlight SDK.

Setup the SDK to with the FastAPI integration.

from fastapi import FastAPI, Request import highlight_io from highlight_io.integrations.fastapi import FastAPIMiddleware # `instrument_logging=True` sets up logging instrumentation. # if you do not want to send logs or are using `loguru`, pass `instrument_logging=False` H = highlight_io.H( "<YOUR_PROJECT_ID>", instrument_logging=True, service_name="my-app", service_version="git-sha", environment="production", ) app = FastAPI() app.add_middleware(FastAPIMiddleware)
Copy
4

Verify your installation.

Check that your installation is valid by throwing an error. Add the following code to your FastAPI app and start the FastAPI server. Visit http://127.0.0.1:5000/hello in your browser. You should see a DivideByZero error in the Highlight errors page within a few moments.

from fastapi import FastAPI, Request import highlight_io from highlight_io.integrations.fastapi import FastAPIMiddleware # `instrument_logging=True` sets up logging instrumentation. # if you do not want to send logs or are using `loguru`, pass `instrument_logging=False` H = highlight_io.H( "<YOUR_PROJECT_ID>", instrument_logging=True, service_name="my-app", service_version="git-sha", environment="production", ) app = FastAPI() app.add_middleware(FastAPIMiddleware) @app.get("/") async def root(request: Request): return {"message": f"This might not be a great idea {5 / 0}"}
Copy
5

Verify your backend logs are being recorded.

Visit the highlight logs portal and check that backend logs are coming in.

6

Verify your backend traces are being recorded.

Visit the highlight traces portal and check that backend traces are coming in.