[Tutorial] FastAPI, Beanie and MongoDB Fully Async

Ahmed Nafies
4 min readApr 16, 2023

FastAPI and Beanie: A Simple Guide to Building RESTful APIs with MongoDB fully async

Photo by Iamninoslav on Unsplash

Introduction

FastAPI is a modern web framework for building APIs with Python, which is fast, easy to use, and comes with automatic validation and documentation. Beanie is an asynchronous Python object-document mapper (ODM) for MongoDB that makes it simple to work with your database using Python data models.

In this tutorial, we will build a simple RESTful API using FastAPI and Beanie to interact with a MongoDB database. We’ll cover installation, configuration, and basic CRUD operations. By the end, you’ll have a working API connected to MongoDB and ready for further expansion.

Prerequisites

  • Python 3.7 or higher
  • Poetry
  • Basic knowledge of FastAPI and MongoDB

Let’s get started!

1. Setting up the environment

First, install and run MongoDB:
https://www.mongodb.com/docs/manual/installation/

# linux
sudo systemctl start mongod

# osx
brew services start mongodb-community

--

--