A baseline implementation using Python/FastAPI to handle CRUD operations against a Mongo database.
One key challenge is that Mongo's default primary key fields are named _id but FastAPI/Pydantic does not permit field names in a model to begin with underscores. There was much discussion of this problem in online forums, and several partially-described solutions; thankfully the official Mongo documentation was recently updated to describe a proper solution in full.
This demonstration puts it all together to create a base repository that can be used to quickly spin up APIs in the future.
- Perform Create, Read, Update and Delete on a model stored in a MongoDB collection.
- Validate fields using standard Pydantic model definitions.
- Connect to a Mongo database defined in
.env. - Authenticate all endpoints against a single secret Bearer Token defined in
.env. - Internally map the Mongo
_idfield toidin code and API calls. - Cleanly separate code for each endpoint's router and model to enable easy future expansion.
- Automatically generate complete endpoint documentation via SwaggerUI.
This application has been tested with the following stack pre-installed:
- Ubuntu 20
- Python 3.6
- Mongo 4.4.15
- python3-pip
- Clone this repo and
cdinto thefastapi-mongo-demofolder. - Run
pip install -r requirements.txtto install the required Python modules.
- Copy or rename the file
.env-samplein the application base folder to.env. - Edit the file
.envin the application base folder.- Change the
MONGO_URIvalue to be the connection string for a running Mongo database. - Change the
MONGO_DBNAMEvalue to be the desired name of your Mongo database. - Change the
API_KEYvalue to be a strong random token string known only to you. - Save the file changes.
- Change the
-
From the application base folder, run the Uvicorn server:
python3 -m uvicorn main:app --reload --host="0.0.0.0" --port=8000 -
In a browser visit
http://<server_ip_address>/docswhere<server_ip_address>is the IP address of the server where this application is installed. -
Confirm you see documentation describing the avaiable API endpoints.
-
This demo provides CRUD endpoints for an endpoint called
/postsfor theoretical posts in a blog, which has the following model definition:id: ObjectID uri: str title: str excerpt: str body: str -
To create an additional API endpoint, copy and modify
/models/pydantic/post.pyand/routers/post.pyand use these as a base to define a model and routes specific to your new endpoint.
Optionally, you may run a test script called located in the root folder of the application by executing this on the command line: python3 tests.py
This script runs a sequence of GET, POST, PUT and DELETE calls against the /posts endpoint and ensures the expected values are returned for each.