
FastAPI
API endpoints are the specific URLs where your API responds to requests. Each endpoint handles a specific operation.
An endpoint is a URL path combined with an HTTP method that performs a specific action:
from fastapi import FastAPI
app = FastAPI()
@app.get("/items") # ← This is an endpoint
async def list_items():
return {"items": []}
@app.post("/items") # ← This is a different endpoint
async def create_item(item):
return item| Part | Example | Purpose |
|---|---|---|
| HTTP Method | GET, POST, PUT | What operation |
| URL Path | /users/123 | Where to access |
| Function | get_user() | What to execute |
| Response | JSON data | What to return |
# GET endpoint - retrieve data
@app.get("/users")
async def list_users():
return {"users": []}
# POST endpoint - create data
@app.post("/users")
async def create_user(user: dict):
return {"id": 1, **user}
# GET endpoint with path parameter
@app.get("/users/{user_id}")
async def get_user(user_id: int):
return {"id": user_id}
# DELETE endpoint - remove data
@app.delete("/users/{user_id}")
async def delete_user(user_id: int):
return {"deleted": True}Paths define the URL structure:
# Simple path
@app.get("/items")
# Path with parameter
@app.get("/items/{item_id}")
# Path with multiple parameters
@app.get("/users/{user_id}/posts/{post_id}")
# Path with version
@app.get("/api/v1/products")Visit these URLs to test:
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep • Build Real • Verify Skills • Launch Forward