You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<img src="https://user-images.githubusercontent.com/43156212/277095260-ef5d4496-8290-4b18-99b2-0c0b5500504e.png" alt="Blue Rocket with FastAPI Logo as its window. There is a word FAST written" width="35%" height="auto">
8
+
<img src="docs/assets/FastAPI-boilerplate.png" alt="Purple Rocket with FastAPI Logo as its window." width="35%" height="auto">
9
9
</a>
10
10
</p>
11
11
@@ -33,6 +33,16 @@
33
33
</a>
34
34
</p>
35
35
36
+
---
37
+
38
+
## 📖 Documentation
39
+
40
+
📚 **[Visit our comprehensive documentation at benavlabs.github.io/fastapi-boilerplate](https://benavlabs.github.io/fastapi-boilerplate/)**
41
+
42
+
This README provides a quick reference for LLMs and developers, but the full documentation contains detailed guides, examples, and best practices.
43
+
44
+
---
45
+
36
46
## 0. About
37
47
38
48
**FastAPI boilerplate** creates an extendable async API using FastAPI, Pydantic V2, SQLAlchemy 2.0 and PostgreSQL:
@@ -47,7 +57,7 @@
47
57
-[`NGINX`](https://nginx.org/en/) High-performance low resource consumption web server used for Reverse Proxy and Load Balancing.
48
58
49
59
> \[!TIP\]
50
-
> If you want the `SQLModel` version instead, head to[SQLModel-boilerplate](https://github.com/igorbenav/SQLModel-boilerplate).
60
+
> There's a `SQLModel` version as well, but it's no longer updated:[SQLModel-boilerplate](https://github.com/igorbenav/SQLModel-boilerplate).
51
61
52
62
## 1. Features
53
63
@@ -62,8 +72,6 @@
62
72
- ⎘ Out of the box offset and cursor pagination support with <ahref="https://github.com/igorbenav/fastcrud">fastcrud</a>
63
73
- 🛑 Rate Limiter dependency
64
74
- 👮 FastAPI docs behind authentication and hidden based on the environment
> 📖 **[See complete configuration guide in our docs](https://benavlabs.github.io/fastapi-boilerplate/getting-started/configuration/)**
158
+
147
159
Then create a `.env` file inside `src` directory:
148
160
149
161
```sh
@@ -302,6 +314,8 @@ pip install uv
302
314
303
315
## 4. Usage
304
316
317
+
> 📖 **[See complete first run guide in our docs](https://benavlabs.github.io/fastapi-boilerplate/getting-started/first-run/)**
318
+
305
319
### 4.1 Docker Compose
306
320
307
321
If you used docker compose, your setup is done. You just need to ensure that when you run (while in the base folder):
@@ -530,8 +544,12 @@ uv run alembic upgrade head
530
544
531
545
## 5. Extending
532
546
547
+
> 📖 **[See comprehensive development guide in our docs](https://benavlabs.github.io/fastapi-boilerplate/user-guide/development/)**
548
+
533
549
### 5.1 Project Structure
534
550
551
+
> 📖 **[See detailed project structure guide in our docs](https://benavlabs.github.io/fastapi-boilerplate/user-guide/project-structure/)**
552
+
535
553
First, you may want to take a look at the project structure and understand what each file is doing.
536
554
537
555
```sh
@@ -661,6 +679,8 @@ Note that this table is used to blacklist the `JWT` tokens (it's how you log a u
661
679
662
680
### 5.3 SQLAlchemy Models
663
681
682
+
> 📖 **[See database models guide in our docs](https://benavlabs.github.io/fastapi-boilerplate/user-guide/database/models/)**
683
+
664
684
Inside `app/models`, create a new `entity.py` for each new entity (replacing entity with the name) and define the attributes according to [SQLAlchemy 2.0 standards](https://docs.sqlalchemy.org/en/20/orm/mapping_styles.html#orm-mapping-styles):
665
685
666
686
> \[!WARNING\]
@@ -683,6 +703,8 @@ class Entity(Base):
683
703
684
704
### 5.4 Pydantic Schemas
685
705
706
+
> 📖 **[See database schemas guide in our docs](https://benavlabs.github.io/fastapi-boilerplate/user-guide/database/schemas/)**
707
+
686
708
Inside `app/schemas`, create a new `entity.py` for each new entity (replacing entity with the name) and create the schemas according to [Pydantic V2](https://docs.pydantic.dev/latest/#pydantic-examples) standards:
687
709
688
710
```python
@@ -731,6 +753,8 @@ class EntityDelete(BaseModel):
731
753
732
754
### 5.5 Alembic Migrations
733
755
756
+
> 📖 **[See database migrations guide in our docs](https://benavlabs.github.io/fastapi-boilerplate/user-guide/database/migrations/)**
757
+
734
758
> \[!WARNING\]
735
759
> To create the tables if you did not create the endpoints, ensure that you import the models in src/app/models/__init__.py. This step is crucial to create the new models.
736
760
@@ -748,6 +772,8 @@ uv run alembic upgrade head
748
772
749
773
### 5.6 CRUD
750
774
775
+
> 📖 **[See CRUD operations guide in our docs](https://benavlabs.github.io/fastapi-boilerplate/user-guide/database/crud/)**
776
+
751
777
Inside `app/crud`, create a new `crud_entities.py` inheriting from `FastCRUD` for each new entity:
> 📖 **[See API pagination guide in our docs](https://benavlabs.github.io/fastapi-boilerplate/user-guide/api/pagination/)**
1025
+
996
1026
With the `get_multi` method we get a python `dict` with full suport for pagination:
997
1027
998
1028
```javascript
@@ -1057,6 +1087,8 @@ async def read_entities(
1057
1087
1058
1088
#### 5.7.2 HTTP Exceptions
1059
1089
1090
+
> 📖 **[See API exceptions guide in our docs](https://benavlabs.github.io/fastapi-boilerplate/user-guide/api/exceptions/)**
1091
+
1060
1092
To add exceptions you may just import from `app/core/exceptions/http_exceptions` and optionally add a detail:
1061
1093
1062
1094
```python
@@ -1084,6 +1116,8 @@ if not post:
1084
1116
1085
1117
### 5.8 Caching
1086
1118
1119
+
> 📖 **[See comprehensive caching guide in our docs](https://benavlabs.github.io/fastapi-boilerplate/user-guide/caching/)**
1120
+
1087
1121
The `cache` decorator allows you to cache the results of FastAPI endpoint functions, enhancing response times and reducing the load on your application by storing and retrieving data in a cache.
1088
1122
1089
1123
Caching the response of an endpoint is really simple, just apply the `cache` decorator to the endpoint function.
@@ -1247,6 +1281,8 @@ For `client-side caching`, all you have to do is let the `Settings` class define
1247
1281
1248
1282
### 5.10 ARQ Job Queues
1249
1283
1284
+
> 📖 **[See background tasks guide in our docs](https://benavlabs.github.io/fastapi-boilerplate/user-guide/background-tasks/)**
1285
+
1250
1286
Depending on the problem your API is solving, you might want to implement a job queue. A job queue allows you to run tasks in the background, and is usually aimed at functions that require longer run times and don't directly impact user response in your frontend. As a rule of thumb, if a task takes more than 2 seconds to run, can be executed asynchronously, and its result is not needed for the next step of the user's interaction, then it is a good candidate for the job queue.
> 📖 **[See rate limiting guide in our docs](https://benavlabs.github.io/fastapi-boilerplate/user-guide/rate-limiting/)**
1382
+
1345
1383
To limit how many times a user can make a request in a certain interval of time (very useful to create subscription plans or just to protect your API against DDOS), you may just use the `rate_limiter_dependency` dependency:
1346
1384
1347
1385
```python
@@ -1456,6 +1494,8 @@ Note that for flexibility (since this is a boilerplate), it's not necessary to p
1456
1494
1457
1495
### 5.12 JWT Authentication
1458
1496
1497
+
> 📖 **[See authentication guide in our docs](https://benavlabs.github.io/fastapi-boilerplate/user-guide/authentication/)**
1498
+
1459
1499
#### 5.12.1 Details
1460
1500
1461
1501
The JWT in this boilerplate is created in the following way:
@@ -1641,6 +1681,8 @@ volumes:
1641
1681
1642
1682
## 6. Running in Production
1643
1683
1684
+
> 📖 **[See production deployment guide in our docs](https://benavlabs.github.io/fastapi-boilerplate/user-guide/production/)**
1685
+
1644
1686
### 6.1 Uvicorn Workers with Gunicorn
1645
1687
1646
1688
In production you may want to run using gunicorn to manage uvicorn workers:
@@ -1834,6 +1876,8 @@ And finally, on your browser: `http://localhost/docs`.
1834
1876
1835
1877
## 7. Testing
1836
1878
1879
+
> 📖 **[See comprehensive testing guide in our docs](https://benavlabs.github.io/fastapi-boilerplate/user-guide/testing/)**
1880
+
1837
1881
This project uses **fast unit tests** that don't require external services like databases or Redis. Tests are isolated using mocks and run in milliseconds.
ENVIRONMENT="local" # local, staging, or production
131
+
```
132
+
133
+
-**local**: API docs available at `/docs`, `/redoc`, and `/openapi.json`
134
+
-**staging**: API docs available to superusers only
135
+
-**production**: API docs completely disabled
136
+
137
+
## Docker Compose Settings
138
+
139
+
If using Docker Compose, use these values instead:
140
+
141
+
```env
142
+
# Docker Compose values
143
+
POSTGRES_SERVER="db"
144
+
REDIS_CACHE_HOST="redis"
145
+
REDIS_QUEUE_HOST="redis"
146
+
REDIS_RATE_LIMIT_HOST="redis"
147
+
```
148
+
149
+
## Optional Services
150
+
151
+
The boilerplate includes Redis for caching, job queues, and rate limiting. If running locally without Docker, either:
152
+
153
+
1.**Install Redis** and keep the default settings
154
+
2.**Disable Redis services** (see [User Guide - Configuration](../user-guide/configuration/index.md) for details)
155
+
156
+
## That's It!
157
+
158
+
With these basic settings configured, you can start the application:
159
+
160
+
-**Docker Compose**: `docker compose up`
161
+
-**Manual**: `uv run uvicorn src.app.main:app --reload`
162
+
163
+
For detailed configuration options, advanced settings, and production deployment, see the [User Guide - Configuration](../user-guide/configuration/index.md).
0 commit comments