fastapi-sitemap
Zero‑config sitemap generator & route for FastAPI / Starlette.
Basic Usage
from fastapi import FastAPI
from fastapi_sitemap import SiteMap, URLInfo
app = FastAPI()
sitemap = SiteMap(
app=app,
base_url="https://example.com",
exclude_patterns=["^/api/", "^/docs/"], # optional: exclude patterns
gzip=True, # optional: make a gz version too
)
sitemap.attach() # now GET /sitemap.xml is live
Adding Custom URLs
Use the @source decorator to add custom URLs to your sitemap:
@sitemap.source
def extra_urls():
# Simple URL
yield URLInfo("https://example.com/about")
# URL with metadata
yield URLInfo(
"https://example.com/priority",
changefreq="daily",
priority=0.8,
lastmod="2024-01-01"
)
Command Line Usage
Generate sitemap files at build time:
# Using a config file
python -m fastapi_sitemap.cli generate -c sitemap_config.py -o ./public
# Or directly with app reference
python -m fastapi_sitemap.cli generate -a myapp.main:app -u https://example.com -o ./public
Configuration Options
app: Your FastAPI application (required)base_url: Your site's canonical URL (required)static_dirs: List of directories containing static HTML filesexclude_patterns: List of regex patterns to exclude from sitemapexclude_deps: Set of dependency names to exclude from sitemapinclude_dynamic: Whether to include dynamic routes (default: False)changefreq: Default change frequency for URLs (default: "weekly")priority_map: Dictionary mapping paths to priority valuesgzip: Whether to enable gzip compression (default: False)
Example Configuration
# sitemap_config.py
from fastapi import FastAPI
from fastapi_sitemap import SiteMap, URLInfo
app = FastAPI()
sitemap = SiteMap(
app=app,
base_url="https://example.com",
static_dirs=["static", "docs"],
exclude_patterns=["^/api/", "^/admin/"],
include_dynamic=True,
changefreq="daily",
priority_map={
"/": 1.0,
"/about": 0.8,
},
gzip=True,
)
@sitemap.source
def extra_urls():
yield URLInfo("https://example.com/custom")
LICENSE (MIT)
MIT License
Copyright (c) 2025 Erik Aronesty
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction... (standard MIT text)
Metadata
Release files for fastapi-sitemap 1.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| fastapi_sitemap-1.0.4.tar.gz | 5.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fastapi_sitemap-1.0.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.5 kB
Release files / fastapi_sitemap-1.0.4.tar.gz
| Download URL | fastapi_sitemap-1.0.4.tar.gz |
|---|---|
| Size | 5.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
941e1fba717d17b73dd58aa9737d294b3d9b8d19bcc2805369b8a38b7a092e58
|
|
BLAKE2b-256 checksum How to use checksums |
72e6ee0c222bfcaa58f8bc6b0b870e77dd6900bae99e3054b99e9cc5efeb3fe5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.2 CPython/3.11.12 Linux/6.11.0-1012-azure
|
Release files / fastapi_sitemap-1.0.4-py3-none-any.whl
| Download URL | fastapi_sitemap-1.0.4-py3-none-any.whl |
|---|---|
| Size | 6.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
979d11308f8b0b8986f16fe83566103306ce1375859e4dfefbfe03af74c489ec
|
|
BLAKE2b-256 checksum How to use checksums |
d15726eb4e704b48a57e0d9583e776165b31fce9f8559bbea8dd7e74fb1b805a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.2 CPython/3.11.12 Linux/6.11.0-1012-azure
|