Table of Contents
This backend project is a FastAPI-based template designed to serve as a robust, reliable, and RESTful API backend. It is crucial for facilitating user authentication, real-time monitoring, data processing, and an advanced alerting system. By leveraging the principles of RESTful architecture, this API ensures standardized communication and a scalable, maintainable infrastructure for the associated mobile application. This project is not only vital for the functionality it provides but also for ensuring the scalability and maintainability of the overall solution.
This project is built using a suite of modern, powerful technologies and frameworks to ensure high performance, ease of development, and robustness:
The system is composed of the following main components, each playing a crucial role in providing a scalable, maintainable, and robust application:
- FastAPI Backend: Serves as the core of the RESTful API, handling incoming HTTP requests, processing data, and sending responses. It's designed for high performance and encourages standard API development practices.
- PostgreSQL & Redis: PostgreSQL is used for structured relational data storage, while Redis serves as a fast, in-memory data store for caching and temporary data to enhance performance.
- JWT Authentication: Implements secure authentication mechanisms using JSON Web Tokens (JWT), ensuring secure access to the API.
This project adheres to RESTful design principles, ensuring an intuitive and standard approach to API development. This design facilitates easy integration, scalability, and interaction with various clients, including web and mobile applications. The API provides clear, resource-oriented URLs, uses HTTP response codes to indicate API errors, and employs JWT for secure, stateless authentication.
-
Clone the repository
git clone https://github.com/jpcadena/fastapi-boilerplate.git
-
Change the directory to root project
cd fastapi-boilerplate
-
Install Poetry package manager
pip install poetry
-
Install the project's dependencies
poetry install
-
Activate the environment
poetry shell
-
Setting up environment variables:
If you find a
.env.sample
in the project directory, make a copy of it and rename to.env
.cp .env.sample .env
This
.env
file will be used to manage your application's environment variables. -
Configuring your credentials:
Open the
.env
file in a text editor and replace the placeholder values with your actual credentials.# .env file POSTGRES_USER=your_database_user SECRET_KEY=your_api_key
Be sure to save the file after making these changes.
-
Generating RSA keys
To ensure secure communication in this project, RSA keys are used. Before running the application, you need to generate a public and private RSA key pair. We've provided a Python script to automatically generate these keys. You can find the script at
app\services\infrastructure\encryption.py
. To generate your keys, simply run:python app\services\infrastructure\encryption.py.py
This will create
public_key.pem
andprivate_key.pem
files in your specified directory.Once the keys are generated, the application will use them for cryptographic operations. Ensure that these files are kept secure and are not exposed publicly. The default configuration expects these keys in the root directory of the project.
-
Starting the server:
To start the local server on your machine, run the following command in your terminal:
uvicorn main:app --reload
The
--reload
flag enables hot reloading, which means the server will automatically update whenever you make changes to the code. -
Interacting with the app:
Once your server is running, you can interact with it using any API client like Postman or your web browser. You can send GET, POST, PUT, DELETE requests to the API endpoints as defined in your
main.py
file. For example, to get all users, you can send a GET request tohttp://localhost:8000/api/v1/users
. -
Using Swagger UI:
FastAPI provides automatic interactive API documentation using Swagger UI. Once your server is up and running, you can go to
http://localhost:8000/docs
in your web browser to access it. From there, you can explore and interact with your API directly.
To facilitate the interaction between the frontend and the API, we use the OpenAPI TypeScript Codegen to generate TypeScript models and APIs based on our OpenAPI specification file.
First, you need to install the OpenAPI TypeScript Codegen as a devDependency. In your project directory, run:
npm install openapi-typescript-codegen --save-dev
Add the following to your package.json file:
"generate-client": "openapi --input C:/Users/user/fastapi-boilerplate/openapi.json --output . /src/client --client axios"
To generate the TypeScript code, you need to use the openapi.json specification file. Run the following command in your terminal:
npm run generate-client
Please read our contributing guide for details on our code of conduct, and the process for submitting pull requests to us.
For security considerations and best practices, please refer to our Security Guide for a detailed guide.
We enforce a code of conduct for all maintainers and contributors. Please read our Code of Conduct to understand the expectations before making any contributions.
Distributed under the MIT License. See LICENSE for more information.