OpenG2P FastAPI Common


This is a Python package that can be used as a library to bootstrap REST API services, based on FastAPI. This page describes different concepts within the library and instructions on how to use it.

Technical Concepts

This package/library contains basic components, like Configuration helpers, Logging helpers, DB ORM helpers, Base Initializers, etc that are required to bootstrap a basic service built using Python and FastAPI. A detailed description is given below.



  • A Component is an object that gets stored in a global registry (called Component Registry) when initialized.

  • Components are usually only initialized once mostly inside an Initializer class. Once initialized in Initializer, they can be obtained using the class method; <ComponentClass>.get_component().

  • A name (optional) can be given to instances of Components so that they can be retrieved using that name if there are multiple instances of a Component. <ComponentClass>.get_component("mycomp").

  • BaseComponent is the base Class for Components.


  • A Service is also a Component that usually contains some logic that is executed inside a Controller.

  • Use <ServiceClass>.get_component() to retrieve a service.

  • BaseService is the base Class for Services.

  • Service is technically the same as Component. Prefer Service over Component when defining new.


  • A Controller is also a Component, that contains a FastAPI APIRouter and API Routes inside it. The APIRouter and APIs get initialized when the Controller is initialized.

  • In the init method of a Controller, the API Routes need to be manually added.

  • Controllers also have a post_init method that adds the APIRouter and APIs into a global FastAPI App. So inside an Initializer, the post_init method can be called immediately after the Controller is initialized.

  • Use <ControllerClass>.get_component() to retrieve a Controller.

  • BaseController is the base Class for Controllers.


  • Settings, based on Pydantic's BaseSettings, establishes configuration options.

  • Configuration Parameters defined inside Settings can be loaded through Env variables / .env file.

  • Settings Class in config can be used as the base class by other Settings classes to inherit.


  • BaseORMModel is an SQLAlchemy ORM Model, that can be used as a base class for other ORM Classes to inherit.


  • BaseExceptionHandler is an Exception Handler Implementation that uses FastAPI Exception Handler at the base and handles extra exceptions defined in this module, mainly BaseAppException.

  • This can also be extended to further handle custom Exceptions when defined.


  • Initializer is a class that initializes all the components, services, controllers, configs, loggers, etc along with any additional Components of a particular Python Package/Module.

  • The Components have to be individually initialized inside the init method of an Initializer.


This section describes instructions for installing the package. Primarily intended for developers using this module to build their own projects.

  • Install python3 for your environment.

  • Set up a virtualenv in your project directory. Using:

    python3 -n venv .venv
    source .venv/bin/activate
  • Then Install the common package using pip:

    pip install -e <path-to-cloned-common-repo>/openg2p-fastapi-common

Usage Guide

This section describes instructions for using the package/library. Primarily intended for developers using this module to build their own projects.

  • The file in the project acts as the main file which initializes all the components of the project. It should contain an Initializer.

  • Initialize the Components (like Services, Controllers, etc.) inside the initialize method of the Initializer. Example

    # ruff: noqa: E402
    from .config import Settings
    _config = Settings.get_config()
    from import Initializer
    from .services.ping_service import PingService
    from .controllers.ping_controller import PingController
    class PingInitializer(Initializer):
        def initialize(self):
  • Note: If the Initializer is only supposed to be used by external modules to inherit/extend/use. Then do not run super().initialize() inside the initialize method. If the Initializer is the main Initializer that sets up the FastAPI apps etc then run super().initialize() inside the initialize method.

  • Note: Due to a limitation in the way the config is set up, the Settings.get_config() needs to be put at the beginning of the (only applies to, before importing other Initializers. If your Linters/Code Formatters are throwing up an E402 error, ignore the error at the beginning of Check the above example.


  • If you are using the template given above, use the config file present in the src folder of your Python package. If not, create a file in your project that looks like this.

    from openg2p_fastapi_common.config import Settings
    from pydantic_settings import SettingsConfigDict
    class Settings(Settings):
        model_config = SettingsConfigDict(
            env_prefix="myproject_", env_file=".env", extra="allow"
  • This Settings class derives from pydantic_settings 's BaseSettings.

  • To define configuration parameters for your project, add the properties to the above Settings class defined in Example

    class Settings(AuthSettings, Settings):
        m_param_a : str = "default_value"
        m_param_b : int = 12
  • The parameters defined here can be loaded through environment variables or .env file. The environment variables can be case insensitive. For example

    • The environment variable prefix, myproject_ in the above example, can be configured under model_config of Settings class, under env_prefix.

  • To use this config in other components of your project like models/controllers/services, etc. use the get_config class method of the above Settings class. Example inside a controller file

    from .config import Settings
    _config = Settings.get_config()
    class PingController(BaseController):
        def get_ping(self):
  • Refer to Additional Configuration to see the configuration properties already available in the base Settings class.


  • To add more APIs to your project, create controllers in the controllers directory.

  • Add each API route using the add_api_route method of the router. Example

    from openg2p_fastapi_common.controller import BaseController
    from .config import Settings
    _config = Settings.get_config()
    class PingController(BaseController):
        def __init__(self, name="", **kwargs):
            super().__init__(name, **kwargs)
            self.router.tags += ["ping"]
        async def get_ping(self):
            return "pong"
  • Initialize the Controller, preferably in an Initializer like given above. It is important to run the post_init method of the Controller after initializing it since that will add the API Router of the Controller to the FastAPI App. Example

    from .controllers.ping_controller import PingController
    class PingInitializer(Initializer):
        def __init__(self):
  • A Controller will automatically initialize Response models for the APIs for the following HTTP Codes: 401, 403, 404, and 500 (with the ErrorListResponse response model defined in this module). These can be changed/updated accordingly.


  • Create Services in the services directory similar to a Controller.

  • Initialize the Service in the Initializer like given above.

  • Example

    from openg2p_fastapi_common.service import BaseService
    from .config import Settings
    _config = Settings.get_config()
    class PingService(BaseService):
        def ping(self, pong: str):
            return _config.m_param_a + " " + pong


  • To retrieve an instance of a Component (Service, Controller, etc) use Component.get_component().

  • Example in PingController if you want to retrieve the PingService.

    from .services.ping_service import PingService
    class PingController(BaseController):
        def __init__(self, name="", **kwargs):
            super().__init__(name, **kwargs)
            self.ping_service = PingService.get_component()
        async def get_ping(self):


  • Get the logger using logging.getLogger(__name__) . This logger initializes JSON logging, using json-logging.

  • This can be modified using the init_logger method of an Initializer.


  • Define Pydantic Models and ORM Models (based on SQLAlcehmy 2.0 ORM) inside the models directory.

  • Use BaseORMModel as the base class for your ORM Model.

    • Use BaseORMModelWithID as the base class, to automatically add id and active fields to the ORM class, along with quick helper classmethods to get an object using id. Example <MyORMModel>.get_by_id(id) .

    • Use BaseORMModelWithTimes as the base class, to automatically add created_at and updated_at along with features of BaseORMModelWithID.

  • This module also uses the AsyncIO Extension of SQLAlchemy 2.0 ORM. Follow the link to understand how to use SQLAlchemy and async conventions to interact with the database. Example:

    from openg2p_fastapi_common.context import dbengine
    from openg2p_fastapi_common.models import BaseORMModelWithTimes
    from sqlalchemy.ext.asyncio import async_sessionmaker
    from sqlalchemy.orm import Mapped, mapped_column
    from sqlalchemy import String, select
    class MyORMModel(BaseORMModelWithTimes):
        name: Mapped[str] = mapped_column(String())
        def get_by_name(cls, name: str):
            response = []
            async_session_maker = async_sessionmaker(dbengine.get())
            async with async_session_maker() as session:
                stmt = select(cls).where(
                result = await session.execute(stmt)
                response = list(result.scalars())
            return response


The following Exceptions are defined by this module. When these exceptions are raised in code, they are caught and handled by BaseExceptionHandler.

The HTTP Response Payload looks like this when the following exceptions are raised. (The HTTP Response Status code is defined according to the Exception).

    "errors": [
            "code": "<error_code from Exception>",
            "message": "<error_message from Exception>",


Raise this to return 401 Unauthorized HTTP response. UnauthorizedError derives from BaseAppException.

Default error_code is G2P-AUT-401.

Default error_message is Unauthorized.

Default http_status_code is 401.

from openg2p_fastapi_common.errors.http_errors import UnauthorizedError


Raise this to return 403 Forbidden HTTP response. ForbiddenError derives from BaseAppException.

Default error_code is G2P-AUT-403.

Default error_message is Forbidden.

Default http_status_code is 403.

from openg2p_fastapi_common.errors.http_errors import ForbiddenError


Raise this to return 400 Bad Request HTTP response. BadRequestError derives from BaseAppException.

Default error_code is G2P-REQ-400.

Default error_message is Bad Request.

Default http_status_code is 400.

from openg2p_fastapi_common.errors.http_errors import BadRequestError


Raise this to return 404 Not Found HTTP response. BadRequestError derives from BaseAppException.

Default error_code is G2P-REQ-404.

Default error_message is Not Found.

Default http_status_code is 404.

from openg2p_fastapi_common.errors.http_errors import NotFoundError


Raise this to return 500 Internal Server Error HTTP response. InternalServerError derives from BaseAppException.

Default error_code is G2P-REQ-500.

Default error_message is Internal Server Error.

Default http_status_code is 500.

from openg2p_fastapi_common.errors.http_errors import InternalServerError


BaseAppException is the parent for the custom exceptions. It takes an error_code , error_message and http_status_code arguments. Default http_status_code is 500. If this is manually raised in code, an error response will be returned on the API Call with the given error code, and error message, and HTTP Status will be set to the given status code.

from openg2p_fastapi_common.errors import BaseAppException

Additional Configuration

The following configuration properties are already present in the base Settings class mentioned in Configuration Guide.

The following properties can also be set through the environment variables, but the env_prefix configured in your project's config Settings will have to be used, as mentioned above. Example myproject_logging_level=DEBUG .

PropertyDescriptionDefault Value


Host/IP to which the HTTP server should bind to.


Port on which the server HTTP should run.



Logging Level. Available values DEBUG, INFO, WARN, ERROR and CRITICAL.



Path to a file where the log should should be stored. If left empty, no file logging. Stdout logging is enabled by default.


Title in OpenAPI Definition. This is the title field in openapi.json generated by FastAPI. Hence also present on Swagger and API Docs.



Description in OpenAPI


Version in OpenAPI



Contact URL in OpenAPI


Contact Email in OpenAPI


License Name in OpenAPI

Mozilla Public License 2.0


License URL in OpenAPI


This is the property used by SQLALchemy for DB datasource. If left empty, this will be constructed, using the following db properties, like the following:



Driver to use while connecting to Database. Configure this based on the Database being used. If using PostgreSQL, leave it as default.



Database Host/IP



Database Port



Database Name


Database Authentication Username


Database Authentication Password


Database Logging. If true, all the database operations being made will be put out in the server logs. Useful while debugging.


Source Code

Last updated


Copyright © 2024 OpenG2P. This work is licensed under Creative Commons Attribution International LicenseCC-BY-4.0 unless otherwise noted.