---
source: https://iinventedlight.com/library/notes/fastapi-templates.md
title: "FastAPI templates: Jinja2Templates and TemplateResponse"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT (FastAPI docs); MIT (FastAPI); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: FastAPI docs, Templates; FastAPI, templates.md. MIT (https://raw.githubusercontent.com/fastapi/fastapi/master/LICENSE). Copyright FastAPI contributors."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-web: fastapi templates"
tags: [fastapi, templates, jinja2, html, response]
client: Librarian
sources:
  - id: S1
    title: "Templates"
    publisher: FastAPI docs
    url: https://fastapi.tiangolo.com/advanced/templates/
    accessed: 2026-11-18
    license: "MIT"
  - id: S2
    title: "templates.md"
    publisher: FastAPI
    url: https://raw.githubusercontent.com/fastapi/fastapi/master/docs/en/docs/advanced/templates.md
    accessed: 2026-11-18
    license: "MIT"
---
# FastAPI templates: Jinja2Templates and TemplateResponse
**Answer:** FastAPI can render HTML with Jinja2Templates. Import Jinja2Templates, point it at a templates directory, and return a TemplateResponse with the template name, the request, and a context.

## Examples

```python
from fastapi import FastAPI, Request
from fastapi.templating import Jinja2Templates

templates = Jinja2Templates(directory="templates")
```

## Notes

You can use any template engine, but the tutorial uses Jinja2Templates. 

## Reference list

- `The` page is about templates in FastAPI.
- `Use` Jinja2Templates to render templates.
- `Return` a TemplateResponse with the template name.

## See also

- [fastapi-response-model](https://iinventedlight.com/library/notes/fastapi-response-model/)
- [fastapi-request-body](https://iinventedlight.com/library/notes/fastapi-request-body/)
- [fastapi-dependencies](https://iinventedlight.com/library/notes/fastapi-dependencies/)

## Claims

- id: C1
  claim: The page is about templates in FastAPI.
  source: S2
  evidence: "Templates"

- id: C2
  claim: Use Jinja2Templates to render templates.
  source: S1
  evidence: "Jinja2Templates"

- id: C3
  claim: Return a TemplateResponse with the template name.
  source: S2
  evidence: "return a `TemplateResponse`"

## Sources

- [S1] Templates. FastAPI docs. https://fastapi.tiangolo.com/advanced/templates/ . License: MIT. Accessed 2026-11-18.

- [S2] templates.md. FastAPI. https://raw.githubusercontent.com/fastapi/fastapi/master/docs/en/docs/advanced/templates.md . License: MIT. Accessed 2026-11-18.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: FastAPI docs, Templates; FastAPI, templates.md. MIT (https://raw.githubusercontent.com/fastapi/fastapi/master/LICENSE). Copyright FastAPI contributors. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
