FastAPI and HTMX: Interactive UIs Without a JS Framework

How FastAPI and HTMX pair together to build dynamic, interactive web apps using server-rendered HTML instead of a JavaScript frontend framework

FastAPI is usually reached for as a JSON API backend paired with a separate React or Vue frontend. HTMX offers a genuinely different path: skip the separate frontend framework entirely, and let FastAPI return HTML fragments directly.

The core idea

HTMX lets HTML attributes trigger AJAX requests and swap the response directly into the page (WebSockets and server-sent events are available through extensions) — no client-side JavaScript framework, no separate build step, no API contract to maintain between a frontend and backend repo. FastAPI, instead of returning JSON, returns HTML fragments (via Jinja2Templates or similar) that HTMX swaps into the DOM.

from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates

app = FastAPI()
templates = Jinja2Templates(directory="templates")

WORDS = ["alpha", "alpine", "beta", "gamma"]

PAGE = """<!doctype html>
<script src="https://cdn.jsdelivr.net/npm/htmx.org@2.0.11/dist/htmx.min.js"></script>
<input type="text" name="q" placeholder="Search"
       hx-get="/search" hx-trigger="keyup changed delay:300ms" hx-target="#results">
<div id="results"></div>"""

@app.get("/", response_class=HTMLResponse)
async def index():
    return PAGE

@app.get("/search")
async def search(request: Request, q: str = ""):
    results = [w for w in WORDS if q and q in w]
    return templates.TemplateResponse(request, "results.html", {"results": results})

and templates/results.html:

<ul>
	{% for r in results %}
	<li>{{ r }}</li>
	{% endfor %}
</ul>

That’s a live search box with no client-side JavaScript written by you: HTMX handles the request and the DOM swap, and FastAPI renders the fragment. hx-trigger="keyup changed delay:300ms" fires on a key press only if the value changed, after a 300 millisecond pause (the changed and delay: modifiers are in HTMX’s docs).

This example was run with FastAPI 0.162.7, Starlette 1.10.0 and HTMX 2.0.11, and tested in a browser: typing alp sent one request to /search?q=alp and swapped in <ul><li>alpha</li><li>alpine</li></ul>. Note the order of the TemplateResponse arguments. Older tutorials pass the template name first and put the request in the context dictionary ("results.html", {"request": request, ...}), and that form raised TypeError: unhashable type: 'dict' on Starlette 1.10.0.

Which HTMX version

HTMX 2.x is the line its documentation calls current, and it is the latest tag on npm (2.0.11, published 22 September 2026), so the example above uses it. HTMX 4.0 was released on 28 August 2026 (4.0.1 on 1 November) and sits under npm’s next tag; its migration notes say attribute inheritance is explicit by default in 4.x, where 2.x inherits implicitly. Check the version in the script tag before copying older examples.

Why this combination specifically

FastAPI’s async support lets one process keep many small HTMX requests in flight at once (every keystroke, every partial page update), which suits this pattern. And FastAPI’s dependency injection and Pydantic validation apply just as well to HTML-returning routes as JSON ones — you’re not giving up FastAPI’s core strengths by not returning JSON.

When this approach makes sense

Internal tools, admin dashboards, and content-heavy apps where you want interactivity (live search, inline editing, infinite scroll) without committing to a full SPA build pipeline. It’s a poor fit for apps that genuinely need complex client-side state management, offline support, or a native mobile app sharing the same frontend logic — that’s still React/Vue territory.

Django works too, FastAPI isn’t required

HTMX is backend-agnostic — it pairs just as naturally with Django, and Django’s templating is arguably more mature for this pattern. Reach for FastAPI+HTMX specifically when you also want FastAPI’s async performance or are already using FastAPI for a JSON API and want to add a lightweight admin UI without adopting a second framework.

Frequently Asked Questions

Can you use FastAPI without a JavaScript frontend framework?

Yes — pairing FastAPI with HTMX lets you return HTML fragments directly instead of JSON, with HTMX handling the AJAX requests and DOM swaps. No client-side JavaScript framework, build step, or separate API contract to maintain.

Does HTMX require FastAPI specifically?

No — HTMX is backend-agnostic and pairs just as naturally with Django, whose templating is arguably more mature for this pattern. FastAPI+HTMX makes the most sense when you also want FastAPI’s async performance or are already using FastAPI for a JSON API.

When is FastAPI + HTMX a poor fit?

Apps that genuinely need complex client-side state management, offline support, or a native mobile app sharing the same frontend logic — that’s still React/Vue territory. HTMX fits internal tools, admin dashboards, and content-heavy apps that want interactivity without a full SPA build pipeline.

Why does TemplateResponse fail with unhashable type dict?

The request now comes first: templates.TemplateResponse(request, ‘results.html’, context). The older form, with the template name first and the request inside the context dictionary, raised TypeError: unhashable type: dict in a test with Starlette 1.10.0 and FastAPI 0.162.7.