HTMX + FastAPI

IntermediateWeb App

FastAPI routes with HTMX: server-driven interactivity in Python.

Published 27 September 2026

Core Tools
HTMX
HTMX
FastAPI
FastAPI
Python
Python
HTML
HTML
CSS
CSS
Database
PostgreSQL
MySQL
MariaDB
Hosting
Railway
Render
Hetzner
Fly.io
DigitalOcean

About HTMX + FastAPI

HTMX + FastAPI pairs the performance and type-safety of a modern Python API framework with the simplicity of server-driven UI. FastAPI handles routing, dependency injection, and automatic data validation via Pydantic. Jinja2 templates render HTML on the server. HTMX enriches those pages with partial updates, form submissions, and live search, all without adding a JavaScript build pipeline or a separate SPA layer.

The key difference from HTMX + Django is ergonomics: FastAPI's async-first design and dependency injection make it easier to compose complex request handlers, and its built-in OpenAPI docs let you test endpoints directly. If your app needs to serve both an HTMX-driven browser interface and a machine-readable API for a mobile app or third-party integration, FastAPI handles both from the same codebase: HTML responses for HTMX, JSON responses for API clients, distinguished by the Accept header or separate route prefixes.

PostgreSQL provides durable relational storage, accessed via SQLAlchemy (async or sync), and any container platform or VPS deploys the ASGI app with Uvicorn. This stack suits teams already comfortable with FastAPI who want to add interactive UIs without adopting a full JavaScript framework.

Key Features

  • ✓FastAPI's async handlers support high concurrent connections without blocking on I/O
  • ✓Pydantic models validate incoming form data and query parameters automatically
  • ✓Jinja2 templates render HTML server-side; HTMX swaps fragments without full page reloads
  • ✓Built-in OpenAPI documentation at /docs to test endpoints without a separate client
  • ✓Same routes can return HTML for HTMX or JSON for API clients via content negotiation
  • ✓Tailwind CSS for rapid, utility-based styling without a CSS architecture decision

When to Use HTMX + FastAPI

  • →Interactive dashboards and data exploration tools backed by Python data libraries
  • →Apps that need both a browser UI and a REST API from the same backend
  • →Internal tools where the team is Python-first but users expect web interactivity
  • →Prototypes that may later split into a separate frontend and API

Pros

  • FastAPI's type hints and Pydantic validation catch data errors before they hit the database
  • Async-native design handles many concurrent connections efficiently on a single process
  • HTMX adds interactivity without requiring the team to learn React, Vue, or a build tool
  • Single Python codebase serves both browser users and API consumers

Cons

  • Jinja2 template reuse is less ergonomic than React components for complex, stateful UIs
  • Network round-trips on every interaction are visible, which rules out real-time collaborative features
  • FastAPI + Jinja2 is an unconventional pairing; fewer tutorials and community examples than Django templates or React

Database Options for HTMX + FastAPI

PostgreSQL

HTMX + FastAPI with PostgreSQL

The standard choice: a full PostgreSQL instance on a managed host or your own server, connected from FastAPI via SQLAlchemy or asyncpg, with complete control over configuration and extensions.

MySQL

HTMX + FastAPI with MySQL

SQLAlchemy, the ORM most FastAPI apps already use for PostgreSQL, connects to MySQL just as easily via PyMySQL (sync) or aiomysql (async), so switching engines doesn't touch your model code. A common pick in mixed stacks running alongside existing PHP or legacy MySQL infrastructure.

MariaDB

HTMX + FastAPI with MariaDB

MariaDB connects through the same SQLAlchemy MySQL dialect and PyMySQL/aiomysql drivers as MySQL: a genuine drop-in, not a separate integration. Reach for this specifically when your infrastructure already standardizes on MariaDB over MySQL.

These are highlighted picks. To see all the tools, check the Databases category.

Hosting Options for HTMX + FastAPI

Railway

Deploy HTMX + FastAPI on Railway

A container-based platform where FastAPI deploys as a persistent service, a natural fit for an async process holding a live database connection pool rather than cold-starting per request. No free tier: the Hobby plan starts around $5/mo in usage credit, plus usage-based billing for CPU, memory, and network. Auto-deploys on push, and has no CDN of its own.

Render

Deploy HTMX + FastAPI on Render

The same persistent-service model as Railway, with a genuinely free tier for small APIs, built-in cron jobs, and its own CDN for any static assets. Free-tier services spin down after inactivity and take a few seconds to wake on the next request; predictable always-on pricing starts around $7/mo.

Hetzner

Deploy HTMX + FastAPI on Hetzner

A self-hosted VPS running the whole FastAPI app yourself, at the price of managing deployment and updates. Entry-level instances start around €4-5/mo, the cheapest option here, though there's no bundled CDN.

Fly.io

Deploy HTMX + FastAPI on Fly.io

Runs FastAPI as a container close to users across multiple regions, useful if API latency to a geographically spread user base matters more than raw cost. Billing is usage-based with only a small free allowance, and there's no dedicated static-asset CDN either.

DigitalOcean

Deploy HTMX + FastAPI on DigitalOcean

A managed VPS or App Platform deployment with predictable flat-rate pricing, starting around $4-6/mo for a basic Droplet or about $5/mo for App Platform's smallest tier. Neither bundles a CDN by default, though DigitalOcean sells one (Spaces CDN) separately if needed.

These are highlighted picks. To see all the tools, check the Hosting & Cloud category.

HTMX + FastAPI Add-ons

Each addition below extends this stack with a capability the base stack works fine without. None are required: include the ones your product actually needs when building this stack, and skip the rest.

Authentication Add-ons

Add authentication when the app needs user accounts — sign-up, login, and data scoped to a specific user instead of a fully open app.

Auth0

HTMX + FastAPI with Auth0

A hosted identity platform with an official Python SDK for verifying tokens in FastAPI route dependencies; it handles social logins, MFA, and session management without building an auth service yourself.

Supabase Auth

HTMX + FastAPI with Supabase Auth

Supabase's standalone auth service, usable from FastAPI even without adopting the rest of Supabase, a lighter-weight option if you just need email/password and OAuth logins backed by a hosted user table.

These are highlighted picks. To see all the tools, check the Authentication category.

CI/CD Add-ons

Add CI/CD when you want a dedicated pipeline for running tests, linting, or multi-stage builds before a deploy goes out. Many hosting platforms already redeploy automatically on every push on their own — a CI/CD tool adds the most value on top of that by gating the deploy on a passing test suite, and matters even more when the hosting choice does not auto-deploy at all, such as a self-hosted server.

GitHub Actions

HTMX + FastAPI with GitHub Actions

Runs the test suite and triggers deployments on every push, directly from the same GitHub repo the code already lives in, with no separate CI service to configure.

GitLab CI/CD

HTMX + FastAPI with GitLab CI/CD

GitLab's built-in CI/CD system, configured via .gitlab-ci.yml in the repository, the natural pick if the project's code lives on GitLab (self-hosted or gitlab.com) rather than GitHub, with the same YAML-pipeline model as GitHub Actions.

These are highlighted picks. To see all the tools, check the CI/CD Pipelines category.

Containerization Add-ons

Add containerization when you want the app packaged the same way across local development, staging, and production, or need to deploy somewhere that isn't a managed serverless platform.

Docker

HTMX + FastAPI with Docker

Packages the whole FastAPI + HTMX app into a container image for consistent local development and deployment, independent of the hosting platform chosen above.

These are highlighted picks. To see all the tools, check the Containerization category.

Observability Add-ons

Add observability when you want to catch errors and performance regressions in production before users have to report them.

Sentry

HTMX + FastAPI with Sentry

Error tracking and performance monitoring with Sentry's dedicated FastAPI integration, covering both route handlers and template rendering. Open-source with a self-hosted path, alongside its own managed cloud.

Datadog

HTMX + FastAPI with Datadog

A broader observability platform covering APM, infrastructure metrics, and log management in one dashboard, useful once a team is already running other services worth monitoring alongside this app. Managed-only, with pricing that scales by host and usage rather than Sentry's per-error volume.

These are highlighted picks. To see all the tools, check the Infrastructure & APM category.

Email Add-ons

Add email when the app needs to send account verification, password reset, or notification messages.

Resend

HTMX + FastAPI with Resend

A transactional email API for account verification, password resets, and notification emails, sent via Resend's official Python SDK from a FastAPI route handler, with a free tier for getting started.

SendGrid

HTMX + FastAPI with SendGrid

The established high-volume choice, with dedicated IP addresses and deliverability tooling for apps sending at serious scale. No permanent free plan (a 60-day trial, then paid tiers), so it earns its slot when volume and deliverability matter more than upfront cost.

Brevo

HTMX + FastAPI with Brevo

An all-in-one platform combining the transactional email API with marketing campaigns, a built-in CRM, and SMS from the same dashboard. A permanent free tier of 300 emails a day with no card required makes it an easy starting pick for an early-stage app.

Payments Add-ons

Add payments when the product is ready to charge for subscriptions or one-time purchases.

Stripe

HTMX + FastAPI with Stripe

Adds subscription billing, one-time checkout, and invoicing via Stripe's hosted Checkout or embedded Elements, with FastAPI handling webhook verification and API calls server-side. The stack works fully without it; add this once the product is ready to charge for something.

Styling Add-ons

Add styling when you want a component or utility-class system to build the UI faster than hand-writing CSS from scratch.

Tailwind CSS

HTMX + FastAPI with Tailwind CSS

A utility-class CSS framework: styles are composed directly in markup via class names instead of writing separate stylesheet files. The default styling choice bundled by most modern framework CLIs (create-next-app, create-vue, the SvelteKit and Astro starters all offer it), and the base layer shadcn/ui components are built on when that's loaded into the catalog in the future.

These are highlighted picks. To see all the tools, check the CSS Frameworks category.

Interactivity Add-ons

Add client-side interactivity when you want local UI state — toggles, modals, dropdowns, tabs — without a full JS framework or an extra server round trip.

Alpine.js

HTMX + FastAPI with Alpine.js

Alpine.js adds lightweight, declarative client-side interactivity on top of this stack's HTMX-driven FastAPI pages: toggling a mobile menu, showing a modal, or switching an active tab, all without triggering a server round trip HTMX would otherwise need. It is included via a script tag, with behavior written as HTML attributes (x-data, x-show, x-on) rather than a build step, keeping the stack's zero-bundler approach intact.

These are highlighted picks. To see all the tools, check the HTML-first Frameworks category.

Analytics Add-ons

Add analytics when you want to measure traffic, track visitor behavior, or understand how people actually use the product.

PostHog

HTMX + FastAPI with PostHog

An all-in-one product analytics platform: event tracking, session replay, feature flags, and A/B testing in one SDK. The richest feature set if you want more than pageviews.

Google Analytics

HTMX + FastAPI with Google Analytics

The industry-standard, free analytics platform, with native Google Ads and Search Console integration. Requires a cookie consent banner in most jurisdictions (GDPR/CCPA), and all data lives on Google's infrastructure, the tradeoff the other three picks were built to avoid.

Plausible Analytics

HTMX + FastAPI with Plausible Analytics

A lightweight, privacy-first analytics tool with no cookie banner required. A good fit for simple traffic metrics without PostHog's broader feature surface.

Umami

HTMX + FastAPI with Umami

Open-source and self-hostable, keeping analytics data on infrastructure you control. Pairs naturally with the self-hosted Hetzner hosting option above if you want to avoid a third-party analytics vendor entirely.

These are highlighted picks. To see all the tools, check the Web & Product Analytics category.

Frequently Asked Questions about HTMX + FastAPI

How does this compare to FastAPI Backend (without HTMX or templates)?

FastAPI Backend is a pure API with no rendered pages at all; a mobile app or separate frontend would call it. This stack renders actual HTML pages from FastAPI, with HTMX handling partial-page updates for interactivity. Pick this one if the app itself needs to serve pages to a browser; pick the bare API if something else is doing the rendering.

How does this compare to HTMX + Django or HTMX + Flask?

All three pair HTMX with a different Python backend. FastAPI brings async performance and automatic API docs; Django brings a built-in ORM, admin panel, and auth system; Flask brings a minimal, unopinionated core. Pick based on which backend framework fits the rest of the project, since HTMX itself works the same way on all three.

Should I use Auth0 or Supabase Auth for authentication?

Auth0 is the better default if you want a mature, general-purpose identity platform with social logins and MFA built in, verified from FastAPI route dependencies via its official Python SDK. Supabase Auth is the lighter option if you just need email/password and OAuth logins backed by a hosted user table, without adopting the rest of Supabase's platform.

What's the cheapest way to run this stack?

Self-hosted PostgreSQL and skipping the paid addition tools cost nothing beyond hosting. Every hosting option here (Railway, Render, Fly.io, DigitalOcean) charges from day one for an always-on API process, so expect at least about $5/mo once you're past local development.

Scores

Popularity3/5

FastAPI is well-established, but pairing it with HTMX specifically is a smaller, if fast-growing, pattern compared to a JS framework frontend — genuinely capable for the "server-rendered but interactive" niche, not yet a mainstream default.

Learning Curve2/5

FastAPI's type-hint-driven design is approachable, and HTMX adds only a handful of HTML attributes rather than a new templating language or build step. One of the faster paths to interactive UI without learning a JS framework.

Flexibility4/5

FastAPI is unopinionated about how you structure templates, auth, or data access, and HTMX itself just swaps HTML fragments without imposing a state-management pattern. Combine with whatever templating engine (Jinja2) and database layer fits the project.

Performance4/5

FastAPI's async handlers process concurrent requests efficiently, and HTMX's partial-page swaps avoid a full reload for most interactions. Not a 5 only because every interaction is still a server round-trip — no client-side state the way a JS framework would provide.

Portability3/5

FastAPI and HTMX are both open-source with no vendor lock-in, portable to any Python-capable host. HTMX itself adds very little coupling since it's just HTML attributes; the main migration cost is whatever templating and data-access code you've written around FastAPI.

Tools in the HTMX + FastAPI Stack

Frontend Frameworks

Backend Frameworks

Programming Languages

Database (choose one)

Hosting (choose one)

Add-ons (optional — add any, or none)

Authentication

CI/CD

Containerization

Observability

Email

Payments

Styling

Interactivity

Analytics

HTMX + FastAPI Pricing

From ~$5/mo Free to start

FastAPI, HTMX, Tailwind CSS, Python and PostgreSQL are all open-source and free. HTMX handles interactivity server-side, so there's no separate frontend to host, just one app server and a database, around $5–20/mo on Railway, Render, Fly.io or Hetzner, plus $6–15/mo for managed Postgres if you don't self-host.

Core toolsFree (open source)

FastAPI, HTMX, Tailwind CSS, Python and PostgreSQL are free.

Hosting$5–20/mo

A single app server on Railway, Render, Fly.io, Hetzner or DigitalOcean; free tiers for testing.

Database (PostgreSQL)Free–$15/mo

Self-host for free, or use managed Postgres from about $6–15/mo.