Building an API with FastAPI: A Simple Guide for Indian Devs
Namaste fellow devs! Today, I’m super excited to share with you my experience of building a RESTful API using FastAPI, a modern Python web framework. As a developer from India, I know how challenging it can be to get started with new tech, but trust me, this one’s a game-changer.
I’ve been working on a personal project where I needed to build a simple API to fetch data from a database. I wanted to learn how to do it using FastAPI, and I must say, it was a breeze! In this post, I’ll walk you through the process of building an API with FastAPI, and I’ll also share some practical tips and tricks that I learned along the way.
Why FastAPI?
FastAPI is a modern Python web framework that’s specifically designed for building APIs. It’s super fast, scalable, and easy to use. With FastAPI, you can build a RESTful API in just a few lines of code. It’s also super lightweight, which means it won’t hog your system resources.
One of the things that I love about FastAPI is its automatic API documentation. With just a few lines of code, you can generate beautiful documentation that includes all the endpoints, methods, and parameters. It’s a huge time-saver, and it makes your API look super professional.
Building an API with FastAPI
Let’s get started with building an API! First, you’ll need to install FastAPI and its dependencies. You can do this using pip:
pip install fastapi uvicorn
Next, create a new Python file for your API, and import FastAPI:
from fastapi import FastAPI
app = FastAPI()
This is the basic structure of your API. Now, let’s add some endpoints!
Here’s an example of how you can add a simple endpoint to fetch data from a database:
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
price: float
@app.get("/items/")
async def read_items():
return [{"item": "Apple", "price": 100.0}, {"item": "Banana", "price": 50.0}]
In this example, we’ve defined a simple Item model using Pydantic, and we’ve added a single endpoint /items/ that returns a list of items.
Deploying on Heroku
One of the things that I love about FastAPI is its ease of deployment. You can deploy your API on Heroku in just a few clicks!
Here’s how you can do it:
- Create a new Heroku app:
heroku create - Install FastAPI and its dependencies:
pip install fastapi uvicorn - Create a new file for your API, and import FastAPI:
from fastapi import FastAPI - Run your API using
uvicorn:uvicorn main:app --host 0.0.0.0 --port 8000 - Deploy your API on Heroku:
git push heroku main
That’s it! Your API is now live on Heroku.
Conclusion
Building an API with FastAPI is a breeze, especially for Indian devs who are looking to get started with new tech. With its ease of use, scalability, and automatic API documentation, FastAPI is a game-changer for any developer looking to build a RESTful API.
So, what are you waiting for? Give FastAPI a try today, and see how easy it is to build a RESTful API!
Has anyone else out there used FastAPI for their projects? Share your experiences in the comments below!
Share this post
Written by Bhairav
Building AI products for Indian developers and small businesses. Founder of DigiAI India. Bootstrapped, profitable, and obsessed with solving real problems.
More from Bhairav