Initialize the uv package manager to manage dependencies efficiently.
uv initAdd ruff, a fast Python linter, to ensure clean and optimized code.
uv add ruffActivate your virtual environment on Windows to manage dependencies separately.
.venv\Scripts\activateInstall FastAPI along with its standard dependencies to build APIs quickly.
uv add fastapi[standard]Start the FastAPI development server to test and interact with your API.
fastapi dev fileName.pyThe following code sets up a basic FastAPI application with a simple GET endpoint that returns JSON data.
# FastAPI se import kar rahe hain, taaki hum API bana sakein
from fastapi import FastAPI
# FastAPI ka ek instance bana rahe hain
app = FastAPI()
# Yeh ek GET request ka endpoint define kar raha hai jo root ("/") pe chalega
@app.get("/")
def get_function(): # Ek function define kiya jo execute hoga jab yeh endpoint hit hoga
# logic
# logic
# logic
return {"name": "Taha"} # Yeh function ek JSON response return karegaThe following code adds a POST endpoint that accepts JSON data and returns a response message.
from fastapi import FastAPI
from pydantic import BaseModel # request validation ke liye
app = FastAPI()
class Data(BaseModel):
name: str
age: int
@app.post("/data") # URL
def add_data(data: Data): # URL par jane se ye function chal jaye ga.
# logic
# logic
# logic
return {
"message" : f"Data received: Name: {data.name}, Age: {data.age}"
} # Json ObjectDefine a DELETE endpoint to remove an item using its ID.
@app.delete("/data/{item_id}") # Item ko delete karne ka endpoint
def delete_data(item_id: int):
# logic
# logic
# logic
return {"message": f"Item with ID {item_id} deleted successfully"}Define a PUT endpoint to modify an existing record based on its ID.
@app.put("/data/{item_id}")
def update_data(item_id: int, data: Data):
# logic
# logic
# logic
return {
"message": f"Item with ID {item_id} updated to Name: {data.name}, Age: {data.age}"
}FastAPI provides a built-in interactive documentation interface using Swagger UI. After running the FastAPI server, open the following link in your browser:
http://127.0.0.1:8000/docs
Besides Swagger UI, you can also test your API using Thunder Client (VS Code Extension) or Postman.
- Open Thunder Client/Postman.
- Enter the endpoint URL (
http://127.0.0.1:8000/for GET,/datafor POST, etc.). - Select the appropriate HTTP method (
GET,POST,DELETE,PUT). - Send the request and verify the response.
- The
uvtool is used to manage dependencies efficiently. - The
FastAPIframework simplifies API creation with built-in support for request validation usingPydantic. - The
DELETEendpoint removes an item using anID, while thePUTendpoint updates an existing record. - Use Swagger UI, Thunder Client, or Postman to test API requests effectively.
Make a requirements.txt file in your project.
Write/Mention all of your library names in your requirements.txt file.
Make a vercel.json file in your project. Use the code below and modify according to your needs.
{
"version": 2,
"builds": [
{
"src": "your_file_name.py",
"use": "@vercel/python"
}
],
"routes": [
{
"src": "/(.*)",
"dest": "/your_file_name.py"
}
]
}Push your project to GitHub.
Deploy your project on Vercel.