DEV Community

Cover image for FastAPI - The Simplicity
rahul188
rahul188

Posted on

FastAPI - The Simplicity

While studying in College and having multiple projects ideas in my mind. i always thinking why there is no such simple way to build the apis & now it seems i missed one of the option.

When it comes to building APIs, Python has always been a popular choice due to its ease of use, readability, and the large number of libraries available. One of the newer frameworks in the Python ecosystem is FastAPI. In this blog, we'll take a look at what FastAPI is, why you might want to use it, and some of its key features.

What is FastAPI?

FastAPI is a modern, fast (hence the name), web framework for building APIs with Python 3.7+ based on standard Python type hints. It was created by Sebastián Ramírez and is an open-source project with a thriving community. FastAPI is built on top of Starlette for the web parts and Pydantic for the data parts.

Why use FastAPI?

One of the key benefits of using FastAPI is its speed. It is built on top of Starlette, which is an async web framework that is designed to be lightweight and fast. This means that FastAPI is able to handle a large number of requests quickly and efficiently, making it an excellent choice for building high-performance APIs.

FastAPI is also highly customizable, with a wide range of options for handling authentication, authorization, and other features. This means that it can be tailored to fit the needs of a wide range of projects and use cases.

Key Features of FastAPI

FastAPI has a number of key features that make it an attractive choice for building APIs:

  1. Fast
  2. Easy to use
  3. Automatic documentation generation
  4. Highly customizable
  5. WebSocket support
  6. Type checking

Let's Build One Project Using FastApis -

Language Translation | English To Hindi

Project Output

Below Piece of code will helps For Language Translation

from googletrans import Translator
translator = Translator()

translation = translator.translate("All The Best", dest='hi')
print(translation)
Enter fullscreen mode Exit fullscreen mode

How to create Endpoints in FastApis -

from fastapi import FastAPI

app = FastAPI()


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

As we can use jinja templating in Fast Apis will create normal HTML Page Which will send Api Request & get the converted response from backend.

So out project folder structure will look like below -

Project Structure

How to send Api request to Backend -

    <script>
      const box = document.getElementById("box");
      // const box1 = document.getElementById("value")

      uploadform.onsubmit = async (e) => {
        e.preventDefault();

        let res = await fetch("/english_text", {
          method: "POST",
          body: new FormData(uploadform),
        });

        if (res.ok) {
          let result = await res.text();

          box.innerHTML = result;
        } else {
          box.innerHTML = `Response error: ${res.status}`;
        };
      };
    </script>

Enter fullscreen mode Exit fullscreen mode

Now We are all set to boot up the FastApi Server.

So let's pull the code & follow below steps with python3.

git clone https://github.com/rahul188/Fast_API_Demo.git
python3 -m venv env
source env/bin/activate
pip install -r requirements.txt
uvicorn application:app --reload
Enter fullscreen mode Exit fullscreen mode

the most beautiful thing about FastApi is the documentation. as we can see in below it will generate Api document as soon as we write the code.

FastApi Docs

Conclusion

FastAPI is a modern, fast web framework for building APIs with Python 3.7+ that is built on top of Starlette and Pydantic. It is highly customizable, easy to use, and provides automatic API documentation generation. FastAPI's speed and flexibility make it an excellent choice for building high-performance APIs that can be tailored to fit the needs of a wide range of projects and use cases.

Top comments (4)

Collapse
 
codeofrelevancy profile image
Code of Relevancy

Look great, I will use FastAPI in my one of the project. Thank you for sharing..

Collapse
 
rahul188 profile image
rahul188

Sure, Feel Free To Reach Out To Me. If any help needed :)

Collapse
 
itskunal007 profile image
Kunal Sharma

Amazing demonstration with a simple project.

Collapse
 
rahul188 profile image
rahul188

Many Thanks!! 😊