Python API Development with FastAPI

FastAPI Basics

Crud Basics Best Practices

In this lesson, we explore what a CRUD application is while outlining the standard API conventions for building a CRUD-based application. CRUD stands for Create, Read, Update, and Delete—the four primary operations required by most applications. For example, in a social media app, you might need to:

  • Create posts
  • Read (retrieve) posts
  • Update posts
  • Delete posts

These operations form the backbone of any CRUD-based system.

Pro Tip

Following established API conventions not only improves code readability but also aids in maintaining consistency across your application.

API Endpoint Conventions

The following best practices are recommended when designing your API endpoints:

  1. Use plural nouns for resource paths

    • For posts, use /posts instead of /post.
    • For users, use /users instead of /user.
  2. Create Operation
    Use HTTP POST to create a new resource. For example, in FastAPI, define the endpoint for creating a post as follows:

    @app.post("/posts")
    
  3. Read Operation
    There are two scenarios for reading data:

    • To retrieve multiple posts (or all posts), use a GET request to /posts:

      @app.get("/posts")
      
    • To retrieve a specific post, use a GET request to /posts/{id}. Here, {id} represents the unique identifier generated by the database:

      @app.get("/posts/{id}")
      
  4. Update Operation
    The update operation allows you to modify an existing post (e.g., change the title or content). There are two HTTP methods available:

    • PUT: Requires all resource fields—even if some remain unchanged.
    • PATCH: Requires only the fields that need to be updated.

    In this guide, we use the PUT method. The endpoint for updating a post is:

    @app.put("/posts/{id}")
    
  5. Delete Operation
    To remove a post based on its unique ID, use the DELETE request:

    @app.delete("/posts/{id}")
    

Quick Summary

The above endpoints ensure that your API remains consistent and easy to understand. Once you establish a CRUD API for one resource, implementing another typically involves replicating and modifying these routes.

Consolidated CRUD Example

Below is a complete example of CRUD endpoints for the posts resource:

@app.post("/posts")
@app.get("/posts")
@app.get("/posts/{id}")
@app.put("/posts/{id}")
@app.delete("/posts/{id}")

This example illustrates the standard route naming and HTTP methods for a CRUD API. When you design additional resources, simply replicate this structure and adjust the endpoints accordingly.

In the next lesson, we will refine our API endpoints further by ensuring they adhere to these best practices. For example, if your current implementation uses a non-standard endpoint such as /createpost instead of /posts, we will update it to match the conventions outlined above.

For further details and code examples, refer back to this guide as you continue to develop your API.

Watch Video

Watch video content

Previous
Schema Validation Pydantic