Skip to main content

Zero‑config sitemap generator & route for FastAPI / Starlette apps

Project description

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 files
  • exclude_patterns: List of regex patterns to exclude from sitemap
  • exclude_deps: Set of dependency names to exclude from sitemap
  • include_dynamic: Whether to include dynamic routes (default: False)
  • changefreq: Default change frequency for URLs (default: "weekly")
  • priority_map: Dictionary mapping paths to priority values
  • gzip: 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)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_sitemap-1.0.4.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_sitemap-1.0.4-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_sitemap-1.0.4.tar.gz.

File metadata

  • Download URL: fastapi_sitemap-1.0.4.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.11.12 Linux/6.11.0-1012-azure

File hashes

Hashes for fastapi_sitemap-1.0.4.tar.gz
Algorithm Hash digest
SHA256 941e1fba717d17b73dd58aa9737d294b3d9b8d19bcc2805369b8a38b7a092e58
MD5 67a6f2ea952df5b3f6ed72192c74b3de
BLAKE2b-256 72e6ee0c222bfcaa58f8bc6b0b870e77dd6900bae99e3054b99e9cc5efeb3fe5

See more details on using hashes here.

File details

Details for the file fastapi_sitemap-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: fastapi_sitemap-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.11.12 Linux/6.11.0-1012-azure

File hashes

Hashes for fastapi_sitemap-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 979d11308f8b0b8986f16fe83566103306ce1375859e4dfefbfe03af74c489ec
MD5 34a6ad6ff094e3c5a8b8e52b52ef423f
BLAKE2b-256 d15726eb4e704b48a57e0d9583e776165b31fce9f8559bbea8dd7e74fb1b805a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page