Python Intermediate - FastAPI

An Understanding of Python Frameworks

mrcloudexplorer
4 min readJun 16, 2024

Understanding Fast API

FastAPI is a modern, fast (high-performance) web framework for building APIs with Python 3.7+ based on standard Python-type hints.

The key features that make FastAPI stand out are

  • Speed It’s one of the fastest Python frameworks available.
  • Ease of use It’s designed to be easy to use and learn.
  • Automatic docs It generates interactive API documentation (Swagger UI and ReDoc) automatically.
  • Dependency Injection FastAPI has an easy and powerful dependency injection system.

Wait First What is API 🤔

An API, or Application Programming Interface, acts as a middleman between different software programs. It defines clear instructions for how applications can communicate and share data. This allows developers to easily integrate features and functionality from other programs into their own, without needing to understand the inner workings of those programs. Think of it like a set of rules that lets programs talk to each other and exchange information.

This is what powers many of the features and connections we use every day in technology.

  • GET: Retrieve information.
  • POST: Add information.
  • PUT: Update information.
  • DELETE: Remove information.
  • PATCH: Edit part of the information.

Fast API File Structure

When setting up a FastAPI project, a well-organized file structure is crucial for maintaining and scaling your application. reference each project has various needs

FastAPI File structure

Building Our First API with Fast API

Let’s start by creating a simple API. First, ensure you have FastAPI and Uvicorn installed

pip install fastapi uvicorn

As a big fan of Kung Fu Panda, let’s create an API for those characters.

Run in localhost

Now, let’s test our Kung Fu Panda API locally

following these links

If everything works well, we should be able to fetch information about characters in the Kung Fu Panda movies.

GitHub code

Deployment with Koyeb

Now that our Fast API is ready, let’s deploy it using Koyeb, a cloud application hosting platform for developers. Follow these steps

  1. Sign up on Koyeb.

2. Use GitHub authentication to continue.

3. Create a new web service.

4. Select the method of deployment and use GitHub.

5. Choose GitHub as your repository and connect it.

6. Select build pack to configure the run command

7. Select override on run command is needed other build and work directory is optional as we use

uvicorn app.main:app --host 0.0.0.0 --port 8000 # Our API use

For some use cases, you might need

uvicorn main:app --host 0.0.0.0 --port 8000 # In some other use case

8. Name your API service and app.

9. Click the deploy button to start the deployment process.

Now, our API is building, automatically creating the environment, cloning our repository and requirement files. Once completed, the server will be up and running.

Our API run time is completed and the server is up streaming

Accessing the Deployed API

Kung-Fu-Panda API

Congratulations! You’ve successfully built and deployed your first FastAPI.

In the next blog post, we’ll explore another framework and create a simple app, continuing our journey into the exciting world of Python development. Stay tuned!

Happy coding!

Reach me 👨‍💻

--

--