Artur Docenko

Software Engineer

Building a Monobank Integration for NestJS

nestjs-monobank is a library for integrating NestJS applications with the Monobank Merchant API. It wraps the interaction with the bank into a familiar Nest module with forRoot/forRootAsync, so you don't have to write your own HTTP client and scatter the API key across controllers.

Example: connecting the module

First, install the package:

npm install nestjs-monobank

Set an environment variable with the key from your Monobank business dashboard (Merchant API section):

MONOBANK_API_KEY=your_api_key

Then use synchronous configuration via forRoot():

import { Module } from '@nestjs/common'
import { MonobankModule } from 'nestjs-monobank'

@Module({
  imports: [
    MonobankModule.forRoot({
      apiKey: 'MONOBANK_API_KEY',
    }),
  ],
})
export class AppModule {}

Or async configuration via forRootAsync(), if the key comes from ConfigService:

import { Module } from '@nestjs/common'
import { ConfigModule, ConfigService } from '@nestjs/config'
import { MonobankModule } from 'nestjs-monobank'

@Module({
  imports: [
    ConfigModule.forRoot(),
    MonobankModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService) => ({
        apiKey: configService.getOrThrow('MONOBANK_API_KEY'),
      }),
      inject: [ConfigService],
    }),
  ],
})
export class AppModule {}

For larger projects, it's convenient to move the config into a separate file, src/config/monobank.config.ts:

import { ConfigService } from '@nestjs/config'
import type { MonobankOptions } from 'nestjs-monobank'

export function getMonobankConfig(
  configService: ConfigService,
): MonobankOptions {
  return {
    apiKey: configService.getOrThrow('MONOBANK_API_KEY'),
  }
}

and reference it instead of an inline useFactory.

Why use a module like this?

  • A single point of configuration: the Monobank key and settings live in one place (forRoot/forRootAsync) instead of being scattered across services.
  • DI instead of a manual client: inject the service wherever you need it, without building your own HTTP wrappers.
  • Environment flexibility: forRootAsync lets you pull the key from ConfigService, a secrets manager, or any other async source.

Further reading


The library is distributed under the MIT License, authored by Artur Docenko (MikroTik3).