Fetching Bank Data

Before you get started

In order to get data from a connected account you first need to have the following:

  • Implemented Authentication to Lean's APIs
  • Created a Customer.
  • Connected an Entity with the correct permissions for the data type you are requesting.

How account data is structured

flowchart TD
%% Nodes
    A("Customer")
    B("Entity (Bank A)")
    C("Entity (Bank B)")
    D("Accounts")
    E("Identity")
    F("Savings Account")
    G("Current Account")
    H("Balance")
    I("Transactions")


%% Edge connections between nodes
    A --> B & C
    B --> D & E
    D --> F & G
    F --> H & I

Making your first API call

With an Entity set up, the first API to call will normally be the /accounts endpoint. This simple GET request returns a list of the available accounts to query.

Let's look at a simple function to call the Accounts API and store all of the Accounts in a storedAccounts variable.

const express = require('express');
const fetch = require('node-fetch')
const app = express()
app.use(express.json())

const fetchAccounts = async (entityId) => {
    let storedAccounts;

    // fetch accounts from the /accounts endpoint
    try {
        var accounts = await fetch(`https://sandbox.leantech.me/v2/accounts?entity_id=${entityId}`, {
            method: 'GET',
            headers: {
                'Authorization': env.LEAN_BEARER_TOKEN
            }
        })
        var json = await accounts.json();
        storedAccounts = await json.payload.accounts;
    } catch (e) {
        console.log(`error: ${e}`)
    }

    // return the accounts values
    console.log('ACCOUNTS: ', storedAccounts)
    return storedAccounts
}

Getting financial account data

Now that you have a list of accounts - and importantly, their id's you can make further requests for the transaction history and balance of the account.

const updateBalances(entityId, storedAccounts) {
  for account in storedAccounts {
    try {
    	var balance = await fetch(`https://sandbox.leantech.me/v2/accounts/${account.id}/balance?entity_id=${entityId}`,
      {
      	method: 'GET',
        headers: {
        	'Authorization': env.LEAN_BEARER_TOKEN
        }
      })
      var json = await balance.json();
      console.log('balance in ', account.nickname, ' = ', balance[0].amount.amount, balance[0].amount.currency
    } catch(e) {
    	console.log('balance request failed error', e)
    }
}