Fast API crash course

Fast API crash course

Welcome to the crash course on Fast API

Fast API is a comparatively new framework but it is impressing me a lot. I found it so much simpler than the Django REST framework. The built-in docs system is just so cool that I am very sure, it's going to save so much of my time

Fast API is going to bring new standards about what to expect from an API framework

Here is the video for the indepth guide with code walk through:

PipFile generated in this video

[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
fastapi = "*"
uvicorn = "*"

[requires]
python_version = "3.8"

Final code file for this Fast API crash course

from fastapi import FastAPI
from pydantic import BaseModel
from typing import Optional

app = FastAPI()

# temp database
fakedb = []

# course model to store courses
class Course(BaseModel):
    id: int
    name: str
    price: float
    is_early_bird: Optional[bool] = None

# Home/welcome route
@app.get("/")
def read_root():
    return {"greetings": "Welcome to LearnCodeOnline.in"}

# Get all courses
@app.get("/courses")
def get_courses():
    return fakedb

# get single course
@app.get("/courses/{course_id}")
def get_a_course(course_id: int):
    course = course_id - 1
    return fakedb[course]

# add a new course
@app.post("/courses")
def add_course(course: Course):
    fakedb.append(course.dict())
    return fakedb[-1]

# delete a course
@app.delete("/courses/{course_id}")
def delete_course(course_id: int):
    fakedb.pop(course_id-1)
    return {"task": "deletion successful"}

Make sure to keep an eye on our blog and youtube channel. We are putting a lot of content to serve the programming community

Did you find this article valuable?

Support Learn Code Online by becoming a sponsor. Any amount is appreciated!