# clerk/fastapi-example architecture

> Architecture diagram of the GitHub repository clerk/fastapi-example, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/clerk/fastapi-example
- Repository: https://github.com/clerk/fastapi-example

## Overview

This is a small FastAPI example that authenticates Clerk JWTs and exposes an identity endpoint plus protected sample data. The workflow is frontend request → FastAPI application and router → endpoint authentication dependency → Clerk verification → endpoint response. The user-ID response is modeled separately. The sampled source shows application configuration, routing, authentication, and response shaping; external request and Clerk verification links are supported by the README and source excerpts.

Main entry points: [`app/api/clerk/clerk.py`](https://github.com/clerk/fastapi-example/blob/main/app/api/clerk/clerk.py) (Clerk endpoints), [`app/security/security_config.py`](https://github.com/clerk/fastapi-example/blob/main/app/security/security_config.py) (JWT authentication), [`app/main.py`](https://github.com/clerk/fastapi-example/blob/main/app/main.py) (FastAPI app), [`app/router.py`](https://github.com/clerk/fastapi-example/blob/main/app/router.py) (API router)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_application["Application setup"]
  node_app["FastAPI app<br/>[main.py]"]
end

subgraph group_api["API routes"]
  node_router["API router<br/>[router.py]"]
  node_handlers["Clerk endpoints<br/>[clerk.py]"]
end

subgraph group_identity["Authentication and response"]
  node_auth["JWT authentication<br/>[security_config.py]"]
  node_user_response["User response model<br/>[user.py]"]
end

node_frontend(("Clerk frontend"))
node_clerk_service["Clerk backend"]

node_frontend -->|"sends requests"| node_app
node_app -->|"mounts router"| node_router
node_router -->|"registers routes"| node_handlers
node_handlers -->|"checks identity"| node_auth
node_auth -->|"reads settings"| node_app
node_auth -->|"verifies JWT"| node_clerk_service
node_handlers -->|"shapes identity"| node_user_response
node_handlers -->|"returns endpoint data"| node_frontend

click node_app "https://github.com/clerk/fastapi-example/blob/main/app/main.py"
click node_router "https://github.com/clerk/fastapi-example/blob/main/app/router.py"
click node_handlers "https://github.com/clerk/fastapi-example/blob/main/app/api/clerk/clerk.py"
click node_auth "https://github.com/clerk/fastapi-example/blob/main/app/security/security_config.py"
click node_user_response "https://github.com/clerk/fastapi-example/blob/main/app/schemas/user.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_app,node_frontend toneBlue
class node_router,node_handlers toneAmber
class node_auth,node_user_response toneMint
class node_clerk_service toneIndigo
```

## Components

### Application setup

- [FastAPI app](https://github.com/clerk/fastapi-example/blob/main/app/main.py): `app/main.py`

### API routes

- [API router](https://github.com/clerk/fastapi-example/blob/main/app/router.py): `app/router.py`
- [Clerk endpoints](https://github.com/clerk/fastapi-example/blob/main/app/api/clerk/clerk.py): `app/api/clerk/clerk.py`

### Authentication and response

- [JWT authentication](https://github.com/clerk/fastapi-example/blob/main/app/security/security_config.py): `app/security/security_config.py`
- [User response model](https://github.com/clerk/fastapi-example/blob/main/app/schemas/user.py): `app/schemas/user.py`

### Other components

- **Clerk frontend**
- **Clerk backend**

## Connections

- Clerk frontend → FastAPI app: sends requests (evidence: `README.md`)
- FastAPI app → API router: mounts router (evidence: `app/main.py`)
- API router → Clerk endpoints: registers routes (evidence: `app/router.py`)
- Clerk endpoints → JWT authentication: checks identity (evidence: `app/api/clerk/clerk.py`)
- JWT authentication → FastAPI app: reads settings (evidence: `app/security/security_config.py`)
- JWT authentication → Clerk backend: verifies JWT (evidence: `app/security/security_config.py`)
- Clerk endpoints → User response model: shapes identity (evidence: `app/api/clerk/clerk.py`)
- Clerk endpoints → Clerk frontend: returns endpoint data (evidence: `README.md`)
