Skip to main content
In this lesson, we will adjust the project structure to improve code organization by moving the main file into a dedicated folder named “app”. This change groups all application-specific code in one place and simplifies package management.

FastAPI Endpoint for Updating a Post

Below is an example of a FastAPI endpoint for updating a post. Notice that this code sample is presented only once for clarity:

Sample Console Output

The following snippet shows a sample of the console output you may see when making requests to the API:

Restructuring the Project

We are now creating a new folder named app to store our main application file. In Python, a folder can be used as a package if it contains an __init__.py file.
Even though the __init__.py file can be empty, its presence is essential as it signals to Python that the folder should be treated as a package.
After creating the app folder and adding an __init__.py file inside it, move the main file into the app directory. At this point, you’ll encounter an error because the startup command is still referencing the main file from the base directory.

Updating the Startup Command

Previously, the application was launched using a command similar to:
In this command, main referred to the file in the project’s base directory. Now that the main file has been moved into the app package, the startup command must be updated to indicate the new package location:
This updated command directs Uvicorn to search for the file named main within the app folder and then use the FastAPI instance (named app) defined therein.

Main File Code Snippet

Below is a snippet from the main file for clarity:

Running the Application

After starting the application with the updated command, you might see output similar to the following:

Verifying the Endpoint

Finally, test the endpoint for retrieving an individual post. For example, a GET request to the endpoint might return a JSON response similar to this:
Everything should now function as expected. This new structure will help keep your application code well-organized as you continue development.

Watch Video