API
What Are APIs?
API stands for Application Programming Interface. APIs allow communication between different software systems to interact with each other, example between the client and API server. They define a set of rules and protocols that allow applications to interact with each other. APIs allow a program to request data from a server without the need for a user interface e.g. a browser but uses code instead.
What Are REST APIs?
REST stands for Representational State Transfer. REST is an architectural style for designing networked applications i.e it provides a set of guidelines and rules for how APIs should be designed, implemented and function.
When a program needs something from a RESTful API it needs to send a request to a specific address on the server, called endpoint which is usually a URI - Uniform Resource Identifier.
They use HTTP methods like GET, POST, PUT, DELETE to perform operations on resources. RESTful APIs are widely used in modern web development.
Example Usage
An example where an API is commonly used is data retrieval, e.g., a frontend app requesting data from a backend.
Below is an example of an API endpoint built using FastAPI:
# backend/routes/web/book.py
from fastapi import APIRouter, Depends, Request, status
from sqlalchemy.orm import Session
from core.database import get_db
from schemas.book import BookCreate
from service.book import BookService
router = APIRouter(prefix="/books", tags=["Website - Books"])
@router.post("/new")
async def create_book(
request: Request,
title: str,
description: str,
published_year: int,
db: Session = Depends(get_db)
):
book_data = BookCreate(title=title, description=description, published_year=published_year)
return BookService.create_book(book_data, db=db)