How to Install FastAPI

Learn to install and execute the FastAPI applications on your local system.

Installation

Run the command below to install FastAPI. You can also create a virtual environment and then install the package.

pip install fastapi

You will also need an ASGI server for production such as uvicorn. An ASGI (Asynchronous Server Gateway Interface) is a spiritual successor to WSGI, intended to provide a standard interface between async-capable Python web servers, frameworks, and applications.

pip install uvicorn

Now we’re ready to run our first FastAPI code.

Running the server on the local machine

To start the server, you need to run the following command:

uvicorn main:app --reload

Here, main refers to the file name and app refers to the object of FastAPI created inside the main.py file. The --reload parameter makes the server restart after code changes. It should only be used for development purposes.

If you are working on your local computer, once the server is started, you can visit the URL given below to see the response of the API:

http://localhost:8000/

Running the FastAPI app: Path parameters

This section is in reference to the Path Parameters in FastAPI lesson. Below is the code snippet that has been discussed in the Path Parameters in FastAPI lesson.

Get hands-on with 1200+ tech skills courses.