Open In App

Introduction to FastAPI And Installation

Last Updated : 13 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Introduction to FastAPI

FastAPI is a modern, fast (as the name suggests), and highly performant Python web framework used for building APIs. It is built on top of standard Python-type hints and is powered by asynchronous programming using Python’s “asyncio”. FastAPI is known for its speed, simplicity, and ease of use, making it a popular choice for developing APIs.

(Visit the FastAPI website from here.)

Features of FastAPI

Key features are:

  • Fast: As the name suggests, FastAPI is fast and provides high performance, on par with the Nodejs. It is the fastest Python web framework out there. It is built on the starlette (for the routing) and pydantic (for data validation and serialization).
  • Fewer Bugs: FastAPI reduces the error caused by humans (developers) about 40%.
  • Fast to Code: The development is really fast. It does not have much boilerplate code and requires minimal setup.
  • Async support: FastAPI is designed to fully support asynchronous programming through Python’s asyncio. It can handle asynchronous request handlers, enabling the server to handle concurrent requests efficiently.
  • Short and easy: FastAPI is designed to be easy to use and easy to learn. We need minimum code setup, which results to fewer bugs.
  • Security and Authentication: FastAPI provides built-in support for handling security and authentication, including OAuth2, JWT tokens, and HTTP authentication. It enables secure access to APIs by enforcing authentication and authorization mechanisms, ensuring data protection and user privacy.
  • Extensible and Modular: FastAPI is designed to be modular and extensible. It allows developers to extend its functionality through plugins and middleware. The framework supports integration with various third-party libraries and tools, enabling the development of complex, feature-rich applications.
  • Easy Integration: FastAPI is designed to be compatible with existing Python libraries and frameworks. It can be easily integrated with other tools and frameworks, making it versatile and adaptable to a wide range of development scenarios.
  • Automatic Docs: FastAPI automatically generate comprehensive API interactive documentation, making it easier for developers to understand and use the API.

Installation for FastAPI

The setup of FastAPI is actually quite simple and straightforward. I have deduced the installation in the following steps: –

Step 1: Set a project directory. Open the terminal and create a virtual environment inside that directory. Creating a virtual environment is necessary, so that we can isolate the FastAPI inside the single folder. (Check out how to set up a python virtual environment here.)

Step 2: Activate your virtual environment.

Step 3: Now, install the FastAPI. Run the command given below!

pip install FastAPI
Screenshot-(233)

On pip install fastapi

Step 4: You will also need an ASGI server for the production as well such as uvicorn or hypercorn. We are using uvicorn here. So, go run the following command in terminal.

pip install "uvicorn[standard]" 

Screenshot-(239)

On pip install ‘uvicorn [standard]”

Step 5: Now, finally its coding time. Open your editor and navigate to your project directory.

Step 6: Create a file name “main.py”. Give it any name you want.

Step 7: Code it!!

Screenshot-(236)

A small FastAPI Application

Let’s see the code:

Line 2: Import FastAPI from the fastapi package.

line 5: Create a variable named “app” to store FastAPI instance. Note that this “app” will be used to start the server.

line 8: This decorator declares the routes for us. Here, it is pointing towards “https://localhost:8000/“. You will use it define endpoints of the app. Since, get method is used, we need to use “@app.get(“/”)”.

line 9: Ok, what will happen on the route is defined by the function. FastAPI provides async to make our code asynchronous.

line 10: Finally, we return a dictionary. FastAPI will serialize the dictionary to JSON like format by itself. And this is how our FastAPI works.

Step 8: Run it using the following command in your terminal. Make sure you are into virtual environment and right directory.

" uvicorn main:app --reload "

  • Make sure, the main, here is the name of our python file. If you have chosen another name, replace it.
  • The app, here is our FastAPI instance and should be changed, accordingly.
  • The –reload, is here for the handling reloading of the server whenever we make changes, so we don’t have to restart server manually.

Screenshot-(237)

Step 9: Head over to the browser and type “localhost:8000”. You will see your fastapi is installed and working fine:

Screenshot-(238)

localhost:8000

And that’s it! See how much easy and straightforward it is to make API in FastAPI.

Conclusion

This is the simplest intro to the fastest python web framework. We learned what is FastAPI, it’s features and its installation. Go through the FastAPI docs for more advance use.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads