Setting up a FastAPI Project: Installing Dependencies and Developing an API in Python (Part 1)

Posted by

Fast API | Project Setup | Installing Dependencies | API development in Python | part -1

Fast API | Project Setup | Installing Dependencies | API development in Python | part -1

Fast API is a modern, fast (high-performance), web framework for building APIs with Python 3.7+ based on standard Python type hints. In this article, we will discuss the project setup, installing dependencies, and API development using Fast API.

Project Setup

To start with Fast API, you need to have Python 3.7 or above installed on your machine. You can create a new project directory and set up a virtual environment using the following commands:

    
        $ mkdir fastapi_project
        $ cd fastapi_project
        $ python3 -m venv venv
        $ source venv/bin/activate
    
    

Once the virtual environment is activated, you can install Fast API using pip:

    
        $ pip install fastapi
    
    

Installing Dependencies

Fast API uses Pydantic for data validation and serialization. You can install Pydantic using the following command:

    
        $ pip install pydantic
    
    

For integrating with databases, you can use databases and sqlalchemy libraries. Install them using the following commands:

    
        $ pip install databases
        $ pip install sqlalchemy
    
    

API development in Python

Now, you are all set to start developing your API using Fast API. You can create a new Python file and start defining your API endpoints using Fast API’s decorators and request/response models from Pydantic.

Here is an example of a simple API endpoint:

    
        from fastapi import FastAPI

        app = FastAPI()

        @app.get("/")
        def read_root():
            return {"Hello": "World"}
    
    

You can run the above code using the uvicorn server:

    
        $ uvicorn app:app --reload
    
    

Now, you can access your API at http://localhost:8000 and see the response from the read_root endpoint.