Building and Deploying Mambu Functions
  • 05 Jun 2024
  • 7 Minutes To Read
  • Dark
    Light
  • PDF

Building and Deploying Mambu Functions

  • Dark
    Light
  • PDF

Article summary

Creating a Mambu Function that will be available for binding to a product involves using TypeScript or JavaScript to code the business logic you want to implement. Before you can start the process, please ensure that you have installed the Mambu CLI.

The workflow for creating, deploying, and binding a Mambu Function

1. Create the project scaffolding

The Mambu Functions CLI allows you to easily create all the files you need to start working on your new Function with the command:

mambu functions generate --template=TEMPLATE_NAME --functionDirectory=PREFERRED_LOCATION --functionName=PREFFERED_FUNCTION_NAME
ExampleDescriptionRequired
mambu functions generateThis part of the command is always required.YES
-t or --templateThe templates to use for an extension point. Templates are ready-to-use implementations for an extension point that allow you to start writing code immediately for common processes. For more information, see the Extensions Point page.YES
-d or --functionDirectoryThe local directory to use for your Mambu Function.YES
-n or --functionNameThe name of the Mambu Function. The name can only contain letters, digits, and hyphens - and it can not be the same as a previously used name unless you use the update flag.YES

For example, if you wanted to create your workspace on the desktop the command would be mambu functions generate -d=~/Desktop/my-new-mambu-function -t=DEPOSIT_FEE_AMOUNT -n=my-new-mambu-function. The name of the directory you create will not necessarily be the name of your Mambu Function. You can decide to name your Function using whatever name you choose, but we recommend that you use the same name for the folder and your Function to keep track of your work.

2. Install the npm dependencies

Navigate to the newly created directory and install the node packages with the command:

cd NEW_DIRECTORY_NAME
npm install

Your folder should have the following files in your newly created folder:

  • function.ts: Contains the logic of your Mambu Function.
  • jest.config.js: Contains the Jest testing framework for testing your Function.
  • mambu-function.json: Contains the manifest file.

Other files will include standard Node project files.

3. Edit the Mambu Functions' logic

Please note

We recommend that you use TypeScript, as it allows you to access the types defined for inputs used in your logic in certain editors, such as Visual Studio Code. In this way you can avoid type errors when building your Function. JavaScript (ECMAScript 2020) is fully supported, but may not be as efficient to use.

Open the function.ts file, which holds the logic of your Mambu Function. The default file has the following TypeScript code:

import { DepositFeeAmountInput, FeeAmount } from "@mambucom/mambu-functions";
import { Big } from "big.js";

// Example function: apply a fee based on deposit balance
export default async function feePercentageFunction(input: DepositFeeAmountInput): Promise<FeeAmount> {
 let feePercentage = 0.02;
 const totalBalance = new Big(input.account.totalBalance);

 if (totalBalance.lt(1000)) {
     feePercentage = 0.01;
 }

 return totalBalance.times(feePercentage).toFixed(2).toString();
}

The default code uses the totalBalance input, which provides the total balance of the account in Mambu as an input. If the total balance is less than 1000, the fee percentage is set to 0.01 - otherwise it remains at 0.02. Early versions of Functions are limited to the calculation of fees in Deposit products, either manually or monthly, using account data and standard (or ungrouped) custom fields. For more information on the input parameters available see Extension Points.

For example, you may use a custom field in your logic in this way:

import { DepositFeeAmountInput, FeeAmount } from "@mambucom/mambu-functions";
import { Big } from "big.js";

// Example function: apply a fee based on deposit balance
export default async function feePercentageFunction(input: DepositFeeAmountInput): Promise<FeeAmount> {
    let feePercentage = 0.02;
    let result = new Big(input.accountCustomFields['AccountFee']).times(new Big(10)).toFixed(2).toString();

 return result;
}
Warning!

Sensitive information, including financial details, usernames, passwords, and personally identifiable information (PII) needs to be protected. Do not add PII or sensitive data to the code.

This logic uses the big.js library, which is included by default, to work with decimal values. You may import your own JavaScript libraries by adding them as dependencies in the dependencies section of the package.json file inside your working directory.

Please note

We restrict the size of the libraries that you may use in your code to 4.7mb and libraries that use NodeJS APIs are not supported. Please ensure that you run vulnerability and security scans on all dependencies that are used in your Function.

3.1 Logging your Mambu Functions

We strongly recommend that you add logging to you Mambu Functions, so that you can easily monitor and debug them later. For more on fetching logs for your Mambu Functions, see Retrieving Function logs. The logs produced by the console print statements are available to retrieve for two weeks.

To add logs use standard JavaScript console print statements in your code:

  • Debug: Use console.debug(message) to output a debug message.
  • Log: Use console.log(message) to output a generic log message.
  • Message: Use console.info(message) to output a standard message.
  • Warn: Use console.warn(message) to output a warning message.
  • Error: Use console.error(message) to output an error message.

For example, you may add a console print out to log the custom fee that the Function returns and a message when the fee is too low:

import { DepositFeeAmountInput, FeeAmount } from "@mambucom/mambu-functions";
import { Big } from "big.js";

// Example function: apply a fee based on deposit balance
export default async function feePercentageFunction(input: DepositFeeAmountInput): Promise<FeeAmount> {
    let feePercentage = 0.02;
    let result = new Big(input.accountCustomFields['AccountFee']).times(new Big(10)).toFixed(2).toString();
    console.log(result);
    
    if (result < 10) {
        console.info("The calculated custom fee is too low.")
    }

 return result;
}
Note
  • To ensure the system stability and performance Functions are soft limited to 5Gb of logs per 1 million executions per month. If this soft limit is consistently exceeded, Mambu reserves the right to disable Functions that produce logs temporarily.
  • In order to maintain transparency, accountability, and security within the system, Management Functions such as create/delete/update function/subscription/secrets are integrated with Audit Trail. However invocations are excluded. If you need to track invocations, log the relevant details as shown above. These logs can be retrieved by using mambu functions logs command or by using the logging API.
Please be Aware

Note that privileged Mambu roles can have access to logs.

4. Build your Mambu Function

Once you have written your logic, you can run the build command, which outputs a single file containing JavaScript code, including any packaged libraries:

npm run build

The output file bundle.js can be found in the /dist folder inside your working directory. We recommend that you verify the manifest file, do a dependency scan on the package.json file, and do a Static Application and Security (SAST) tests on the final build before deploying the Function.

In our templates we use esbuild to compile TypeScript projects into JavaScript bundles. You may use any other bundler as long as:

  • it bundles the whole project into a single JavaScript file (including libraries),
  • it targets ECMAScript 2020 specification, and
  • the resulting bundle is a module exporting the handler as a default function using ES6 modules syntax.

5. Deploy your Mambu Function

The command to deploy your Function to your Mambu environment is:

mambu functions create --functionDirectory=DIRECTORY_NAME [--functionName=FUNCTION_NAME] [--profile=PROFILE_NAME] [--version=FUNCTION_VERSION]
ExampleDescriptionRequired
mambu functions createThis part of the command is always required.YES
-d or --functionDirectoryThe local directory where your Mambu Function is located.YES
-n or --functionNameThe name of the Mambu Function, which can be changed when deploying your Function. The name can only contain letters, digits, and hyphens - and it can not be the same as a previously used name, unless you are updating a Function.NO
-v=1 or --version=3The default version is set to v1 if this flag is not used. For most cases, we recommend that you append a version number to your Function to keep track of versions that have been deployed.NO
-p or --profileThe profile flag allows you to use a different credentials profile, with a different Mambu API key and Mambu environment URL, for this command. If this is omitted, the default profile is used. For more information, see Using credentials profiles.NO

For example, if we wanted to deploy our example function the command would be mambu functions create --functionDirectory=Documents/my-new-mambu-function. The deploy command makes your Function available for binding to a product in the Mambu UI. If the Function has already been deployed, you would need to use the mambu functions update command. For more information, see Updating a Mambu Function.

6. Confirm that the Function has been deployed

You can confirm that the Mambu Function has been deployed to your Mambu tenant by running the command:

mambu functions list [--profile=PROFILE_NAME]
ExampleDescriptionRequired
mambu functions listThis part of the command is always required.YES
-p or --profileThe profile flag allows you to use a different credentials profile, with a different Mambu API key and Mambu environment URL, for this command. If this is omitted, the default profile is used. For more information, see Using credentials profiles.NO

7. Bind your Function to a product

Log into the Mambu UI to bind your Function to a product. See the Deposit Fees Setup page for further instructions.


Was this article helpful?

ESC

Eddy, a generative AI, facilitating knowledge discovery through conversational intelligence