- Initial Setup:
- Create a new directory for your project
- Create and activate a virtual environment
- Install FastAPI using pip: pip install fastapi
- Create a new file main.py in the project directory
- Add the following code to the main.py file to create a basic FastAPI application
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: str = None):
return {"item_id": item_id, "q": q}
Run the application by executing uvicorn main:app --reload
in the command line
Visit http://localhost:8000/ in your browser to see the Hello World
message
Define Models:
Install SQLAlchemy using pip: bash pip install sqlalchemy
Create a new file models.py in the project directory
Add the following code to define a model for the blog posts:
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class BlogPost(Base):
__tablename__ = "blog_posts"
id = Column(Integer, primary_key=True, index=True)
title = Column(String, index=True)
body = Column(String)
Create Endpoints: Add the following code to the main.py file to create endpoints for creating, reading, updating, and deleting blog posts:
from fastapi import FastAPI, HTTPException
from sqlalchemy.orm import Session
from .models import BlogPost, Base
from sqlalchemy import create_engine
app = FastAPI()
engine = create_engine("sqlite:///blog.db")
Base.metadata.create_all(bind=engine)
@app.post("/blog_posts")
def create_blog_post(post: BlogPost, db: Session = Depends(get_db)):
db.add(post)
db.commit()
db.refresh(post)
return post
@app.get("/blog_posts/{post_id}")
def read_blog_post(post_id: int, db: Session = Depends(get_db)):
post = db.query(BlogPost).filter(BlogPost.id == post_id).first()
if not post:
raise HTTPException(status_code=404, detail="Post not found")
return post
@app.put("/blog_posts/{post_id}")
def update_blog_post(post_id: int, post: BlogPost, db: Session = Depends(get_db)):
stored_post = db.query(BlogPost).filter(BlogPost.id == post_id).first()
if not stored_post:
raise HTTPException(status_code=404, detail="Post