Setting Up Your FastAPI Project for More Than Just CRUD Operations – Part 1

Posted by


Welcome to the first part of our FastAPI Beyond CRUD tutorial series! In this series, we will explore how to build a more complex and fully-featured REST API using the FastAPI framework.

In this first part, we will start by introducing FastAPI and setting up our project. FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints. It is easy to use, extremely fast, and comes with features such as automatic API validation, OpenAPI and JSON Schema integration, dependency injection, and much more.

To get started, make sure you have Python 3.6 or higher installed on your machine. You can check your Python version by running the following command in your terminal:

python --version

If you don’t have Python installed, you can download it from the official Python website.

Now, let’s create a new directory for our project and navigate into it:

mkdir fastapi-beyond-crud
cd fastapi-beyond-crud

Next, we will create a new virtual environment to isolate our project dependencies:

python -m venv venv

Activate the virtual environment. On Windows:

venvScriptsactivate

On macOS and Linux:

source venv/bin/activate

Now that we have our virtual environment set up, we can install FastAPI and Uvicorn, which is a lightning-fast ASGI server:

pip install fastapi uvicorn

You can also install additional dependencies that we will use throughout the tutorial series:

pip install pydantic sqlalchemy alembic

Now, let’s create a new Python file to start building our FastAPI application. Create a new file named main.py in the project directory and open it in your favorite code editor:

touch main.py

Inside main.py, we will start by importing FastAPI and creating an instance of the FastAPI class:

from fastapi import FastAPI

app = FastAPI()

This creates a new FastAPI application instance that we can use to define the endpoints and their respective operations.

To run our FastAPI application, we will utilize Uvicorn. We can start our application by running the following command in the terminal:

uvicorn main:app --reload

This command tells Uvicorn to run our main.py file (which contains the FastAPI instance as app) with automatic reloading enabled. Now, if you navigate to http://localhost:8000 in your browser, you should see the FastAPI interactive API documentation (Swagger UI) where you can explore the API endpoints.

That’s it for the introduction and project setup in this part of the tutorial. In the next part, we will start defining endpoints and operations to create a more complex API. Stay tuned for more!

0 0 votes
Article Rating
9 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@SsaliJonathan
2 months ago

Hey everyone! The FastAPI course is out. And I want to request you click the like button. Also, leave a comment about what I can add to it. Thanks for watching.
All course content is found here.
https://jod35.github.io/fastapi-beyond-crud-docs/site/chapter15/

@EmreEkmekcioglu
2 months ago

Project website is down :((

@user-ip4bx8zs3n
2 months ago

nice tutorial

@geminiwork
2 months ago

sir im a beginner to python which framework should i learn django? flask? fast api?
plz give suggestion

@satabdidey2016
2 months ago

Please tell me tha vs code theme name that you have used in this course

@mahdilotfi4102
2 months ago

great content dude, tnx a lot
can you also cover the Dependency injection, using an optimized format of background tasks (in FastAPI), caching, and containerization to make it more like production-ready projects?

@ulugbeknurjonov5226
2 months ago

Hi bro
Can you share list of course?
What we will learn etc?

@leokim6270
2 months ago

Grateful for having found your channel…

@suen-tech
2 months ago

Thank you
– FASTAPI + Websockets
– FASTAPI + Deploiement on Deta Space (Vercel, Fly, …)
– Sync two DBs via FASTAPI (Local / Distante)
Good lack