Install FastAPI and run your first FastAPI server on Windows

Aboubacar Abdou Abarchi
3 min readApr 16, 2023

--

What is FastAPI?

Released in 2018, FastAPI is a Python web framework for building RESTful APIs. It quickly gained popularity due to its ease of use, speed and robustness. FastAPI uses Pydantic to define custom data types or extend validation with methods and allows to validate, serialize and deserialize data. It also automatically generates OpenAPI documentation for APIs built in two html format using Swagger UI and Redoc and OpenAPI data in json format[1].

First let’s install Python

Photo by Rubaitul Azad on Unsplash

Go to the official website https://www.python.org/downloads, download and install python, just follow the instruction to install it.

Note, don’t forget to check the Add python in environment variable box.

After this, when you run one of the command below, you will see the version of python you have installed.

python --version
py --version

As we have python installed, we will now install pip (is the package installer for Python)and pipenv (is a Python virtualenv management tool that supports a multitude of systems and nicely bridges the gaps between pip, pyenv and virtualenv.) which will help us launch our server. Recent versions of python come with pip installed by default. To install pipenv you just need the command pip install pipenv.

To do this properly, we will use python virtual environment
in order to isolate the libraries and scripts that we will use for our server from those installed in other virtual environments and from all the libraries installed by default.

After the configuration let’s create our server

Run these commands to create a folder and access it

mkdir fastapi_server
cd fastapi_server

If you are using VScode, we can open the project by running the command code . or open the folder with VScode.

At the root of the project folder, we will create our virtual environment and install the fastapi and packages.

pipenv shell

This command will create a virtual environment.

pipenv install fastapi uvicorn

This command will install fastapi and uvicorn (ASGI web server implementation for Python.) packages in our virtual environment.

Now let’s create the main.py file which looks like this

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
return {"message": "Hello World"}

And we run the command below to run the server:

uvicorn main:app --reload

Finally, we have just launched our first fastapi server. The logs will look like the figure below:

And when we open the browser at http://127.0.0.1:8000 we will see the JSON response as: {“message”: “Hello World”}

And as a bonus, fastapi provides at the same time the documentation accessible through these links http://127.0.0.1:8000/docs, http://127.0.0.1:8000/redoc and http://127.0.0.1:8000/openapi.json.

Note: Do not deploy the server in production with insecure documentation. See this article Protect FastAPI Docs behind Firebase Authentication to learn how to secure the documentation.

Thank you for reading this article. I hope this will help you.

References

[1]: https://fastapi.tiangolo.com

--

--