Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/tutosrive/fastapi-CRUD-MongoDB/llms.txt

Use this file to discover all available pages before exploring further.

This project exposes three main areas of configuration: the MongoDB connection (where and how the app connects to your database), the FastAPI app metadata (title, version, and Swagger UI groupings), and the uvicorn server startup options (port, hot-reload, and host binding). All three are handled through a small number of files that are straightforward to adjust before deploying to a new environment.

MongoDB Connection

The entire database connection is defined in a single line inside app/config/db.py:
from pymongo import MongoClient

mong_conn = MongoClient()
Calling MongoClient() with no arguments is equivalent to MongoClient("mongodb://localhost:27017/"). This default is fine for local development where MongoDB is installed and running on the same machine. The application accesses two collections through this connection object:
Collection referenceDatabaseCollection
mong_conn.local.userlocaluser
mong_conn.local.tasklocaltask

Connecting to a Remote Host

To point the app at a remote MongoDB server, replace the no-argument call with a full connection URI:
from pymongo import MongoClient

# Remote host on default port
mong_conn = MongoClient("mongodb://host:27017/")

# With authentication
mong_conn = MongoClient("mongodb://username:password@host:27017/")

# MongoDB Atlas (cloud)
mong_conn = MongoClient("mongodb+srv://username:password@cluster.mongodb.net/")
After changing the MongoClient call, also update the database name in every controller that references mong_conn.local.* — for example, change mong_conn.local.user to mong_conn.myapp.user.
MongoDB’s local database is a special system database. It is never replicated across nodes in a replica set and is not backed up by most managed database services. Using it for application data means your records will not survive a replica failover or a standard mongodump backup. For any environment beyond a single-developer local machine, create a dedicated database (e.g., myapp_db) and update the collection references in app/controllers/user.py and app/controllers/task.py accordingly.

FastAPI App Metadata

The FastAPI() constructor in main.py accepts metadata that appears in the auto-generated OpenAPI schema and Swagger UI:
from fastapi import FastAPI
from app.config.docs import api_tags

app = FastAPI(
    title='FASTAPI + MongoDB',
    description='Learning FastAPI with MongoDB ...',
    version='2025.0.1.0',
    openapi_tags=api_tags
)
ParameterCurrent valuePurpose
title'FASTAPI + MongoDB'Displayed as the API name in Swagger UI and ReDoc
description'Learning FastAPI with MongoDB ...'Shown below the title in the API docs
version'2025.0.1.0'API version string in the OpenAPI spec
openapi_tagsapi_tagsControls endpoint grouping in Swagger UI
The api_tags list is imported from app/config/docs.py:
api_tags = [
    {
        'name': 'User',
        'description': 'The user CRUD'
    },
    {
        'name': 'Task',
        'description': 'The tasks CRUD'
    }
]
Each entry in api_tags corresponds to a tags value set on an APIRouter. When a router declares tags=['User'], every endpoint registered on that router is grouped under the User section in Swagger UI with the description text from this list. Add a new entry here whenever you introduce a new resource router.

Running the Server

The application is served by uvicorn, an ASGI server. Run it from the project root directory (the folder containing main.py). Basic start (default port 8000):
uvicorn main:app
Development mode with auto-reload on port 5000:
uvicorn main:app --reload --port 5000
FlagEffect
--reloadWatches source files for changes and automatically restarts the server. Use only during development — it adds overhead.
--port 5000Binds the server to port 5000 instead of the default 8000.
--host 0.0.0.0Binds to all network interfaces, making the server accessible from other machines on the same network.
A successful startup prints output similar to:
INFO:     Uvicorn running on http://127.0.0.1:5000 (Press CTRL+C to quit)
INFO:     Started reloader process [12345] using WatchFiles
INFO:     Started server process [12346]
INFO:     Waiting for application startup.
INFO:     Application startup complete.

Interactive API Docs

FastAPI automatically generates two interactive documentation interfaces at no extra configuration cost:

Swagger UI

Available at /docs. Provides a full interactive interface where you can inspect every endpoint, view request/response schemas, and execute live requests directly from the browser.

ReDoc

Available at /redoc. Renders a clean, read-only reference view of the OpenAPI spec — useful for sharing API documentation with consumers who do not need to run requests.
Use /docs during development. The Try it out button on each endpoint lets you send real requests to your running server and inspect raw responses without needing a separate tool like Postman or curl. The api_tags groupings from app/config/docs.py keep the User and Task endpoints neatly separated so the interface stays easy to navigate as the API grows.

Build docs developers (and LLMs) love