This is a small sample project demonstrating how to build an API with MongoDB and FastAPI.
Build with us
Running the Example
To begin, you should clone the example code from GitHub.
git clone git@github.com:mongodb-developer/mongodb-with-fastapi.git
You will need to install a few dependencies: FastAPI, Motor, etc. I always recommend that you install all Python dependencies in a virtualenv for the project. Before running pip, ensure your virtualenv is active.
cd mongodb-with-fastapi
pip install -r requirements.txt
It may take a few moments to download and install your dependencies. This is normal, especially if you have not installed a particular package before.
Once you have installed the dependencies, you need to create an environment variable for your MongoDB connection string.
export MONGODB_URL="<Your Atlas Connection String>"
Remember, anytime you start a new terminal session, you will need to set this environment variable again. I use direnv to make this process easier.
The final step is to start your FastAPI server.
uvicorn app:app --reload
Once the application has started, you can view it in your browser at http://127.0.0.1:8000/docs.
Once you have had a chance to try the example, walk through the code.