DEV Community

Phil IPS
Phil IPS

Posted on

FastAPI Basics: Building a Simple REST API

FastAPI Basics: Building a Simple REST API

FastAPI is a modern, high-performance web framework for building APIs with Python 3.6+.

Installation

pip install fastapi uvicorn
Enter fullscreen mode Exit fullscreen mode

Basic Example

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"Hello": "World"}

@app.get("/items/{item_id}")
def read_item(item_id: int, q: float = None):
    return {"item_id": item_id, "q": q}
Enter fullscreen mode Exit fullscreen mode

Run the server:

uvicorn main:app --reload
Enter fullscreen mode Exit fullscreen mode

Visit http://127.0.0.1:8000/docs for interactive Swagger UI.

Conclusion

FastAPI provides automatic documentation and high performance, making it ideal for building APIs quickly.

Top comments (0)