You have a FastAPI app running, the API docs look great, but the browser tab still shows a blank square or the default globe icon. This is a common issue: FastAPI does not automatically serve a favicon, and unlike Flask or Django, it has no built-in convention for one. The fix is straightforward once you know where to put the file and how to expose it.

In this guide, we will walk through the exact steps to serve a fastapi favicon using StaticFiles, a dedicated route, or a template tag. We will also cover the HTML markup that makes browsers pick up the icon reliably, plus the pitfalls that cause 404s and stale icons.

Prerequisites

Before you start, make sure you have:

Step 1: Place the favicon file in a static directory

Create a folder named static at the root of your project (or anywhere you prefer). Put your favicon file inside it. For maximum compatibility, name it favicon.ico and also include a PNG or SVG version if you have one.

myproject/
├── main.py
├── static/
│   └── favicon.ico
└── templates/
    └── index.html

If you only have a PNG, that works too—just reference it correctly in the HTML. The ICO format is still requested by browsers when no link tag is present, so having /favicon.ico available avoids a 404 in the console.

Step 2: Mount StaticFiles in your FastAPI app

FastAPI uses Starlette's StaticFiles to serve static assets. Add this to your main.py:

from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles

app = FastAPI()

app.mount('/static', StaticFiles(directory='static'), name='static')

This makes any file in static/ available at /static/filename. So your favicon will be at /static/favicon.ico.

Step 3: Add the link tag in your HTML

If you serve HTML templates (using Jinja2Templates), add the favicon link inside the <head> of your base template. The rel attribute must be icon according to the HTML specification.

<link rel='icon' type='image/x-icon' href='/static/favicon.ico'>

If you also have an SVG or PNG, include them with appropriate type attributes. Browsers will pick the best supported format. For a deeper dive into link tag order, see our favicon link tag guide.

Step 4: Serve /favicon.ico directly (optional but recommended)

Browsers and some tools still request /favicon.ico at the root even if you have a link tag. To avoid a 404, you can add a dedicated route that returns the file:

from fastapi import FastAPI
from fastapi.responses import FileResponse

app = FastAPI()

@app.get('/favicon.ico', include_in_schema=False)
async def favicon():
    return FileResponse('static/favicon.ico')

The include_in_schema=False keeps it out of the auto-generated docs. This route is especially useful for API-only apps that do not serve HTML but still want a clean network tab.

Step 5: Verify the setup

Start your server and open the app in a browser. Check the network tab: the favicon request should return 200. If you see a 404, confirm the file path and that the mount point matches the URL in your link tag. You can also use a favicon checker tool to audit the icon across browsers.

Common pitfalls

Conclusion

Serving a favicon in FastAPI is a matter of placing the file, mounting a static directory, and adding the correct link tag. For API-only apps, a simple route for /favicon.ico keeps logs clean. Once set up, your app will show a proper icon in browser tabs, bookmarks, and history—no more blank squares.