Skip to main content
In previous examples, we discussed best practices and naming conventions for building APIs. In this updated guide, our FastAPI code is refactored to align with RESTful principles and industry standards.

Initial Implementation

Initially, our application defined GET and POST endpoints as follows:
Notice that the POST endpoint is defined with the path /createposts, which is inconsistent with RESTful naming standards. Our goal is to standardize all endpoints, so we update the POST endpoint to use /posts.

Improved Endpoint Definition

By changing the POST endpoint to /posts, our API maintains a consistent naming scheme. The updated endpoint is shown below:
After updating the endpoint, be sure to adjust your testing tools—such as Postman—with the new endpoint URL. For example, sending the following JSON in a POST request:
will now target the /posts endpoint. When tested, this endpoint responds as expected.
Keep in mind that this example only prints and returns the submitted post; it does not persist data in a database.

Simulating Data Persistence

Since this simple example does not use a database, we simulate data persistence by storing posts in an in-memory list. In a production setting, these posts would be saved to a database where ID creation is handled automatically. Here, we assign a unique ID to each post using a random integer. Below is the consolidated version of our FastAPI application:

Code Breakdown

  1. Data Model:
    The Post class (a Pydantic model) defines the structure and validation rules for incoming post data.
  2. In-Memory Storage:
    The my_posts list simulates a database and is pre-populated with two sample posts. Note that any data stored here will be lost when the server restarts.
  3. GET Endpoint:
    The /posts GET endpoint returns all stored posts in response to client requests.
  4. POST Endpoint:
    The /posts POST endpoint validates the input post using the Post model, converts it to a dictionary, assigns a unique ID using randrange, appends the post to the in-memory list, and returns the new post data.
When you test the GET endpoint (using Postman, for example), you should see a response similar to this:
As you continue to enhance your API, consider incorporating input validation and error handling mechanisms to build a fully functional CRUD-based application.
By following these guidelines, you ensure your API is both maintainable and aligned with RESTful best practices.

Watch Video