How to Automate Bank Statement to Personal Accounting Software (Wave) - Part 1
Tags: productivity, tutorial, bot, tooling
Published: 2022-11-23 16:33:47


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mx5fmn7cr45pfyqdh6o2.jpg)
Recently with the rise of digital wallet, transactions your-text-here </div> are really hard to track as we use different accounts for different transaction purposes. Personally I have several bank savings accounts, credit cards and a couple of e-wallet services but all of them aren't connected.

---


## Problem Statements

1. Most of the Malaysian bank or e-wallet doesnt support public API service.
2. Service doesn't come with recurring monthly statement eg. email
3. Service provide recurring monthly statement but with different format or delivery methods
4. Records of transaction only available for 90 days

Hence, I came across wave accounting software which provides **free** personal accounting management with GraphQl Api support. Hence, the idea is straightforward: whenever the bank sends you an email statement, we are going to build an event trigger script to help us update all the statements to wave through the provided api.

## Design and Plan

![How to Automate Bank Statement to Personal Accounting Software Architecture Diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cn2zxsh7rv47j0u6jjnw.png)

The whole idea of this project will be create a event listener that will trigger a bot to process the file when:

1. We receive an email from the bank to the bot email
2. We send it manually to the bot email

But we will cover only the worker to wave accounting part in this post only.


## 1. Create a Wave Account and a API Token

Go to website [Wave official website](https://www.waveapps.com/#page-wrapper) and register an account. After registring an account, go to the dashboard and familiarize the tools, accounting terms which I am not going to discuss much here. If you are a business owner, is a great tool for you to record down your statement by creating a business account, but for personal use , a personal account will be sufficient.

Then go to [Wave Developer](https://developer.waveapps.com/hc/en-us/articles/360019762711-Manage-Applications) to create an application


![Automate Bank Statement Wave Check Agreement](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/psvi28lyu96hwg4bmizj.png)

Insert your desire application name and description (optional)

![Create Token Cover](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t678ze03rzddrk64td7k.png)

Click agree and create.

![Click agree and create.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/99uxoaixyqn6zrvumkht.png)

Once redirected back to the application page. click the application name that you just created.

![Automate Bank Statement Wave Create Token](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hz8lxez0t9htlfqx8uyh.png)

Click the create token and you should be able to generate a token, save the token somewhere secretly and we will use it during our API integration.


![Wave Accounting Create Full Access Token](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kcrkz1ogz3z9bbh0ts0r.png)

## 2. Create Your API Module
Wave is using graphql and kindly go through [Wave API Documentation](https://developer.waveapps.com/hc/en-us/articles/360019968212-API-Reference) for more info. You may test your api using postman.

### Test Your API
Eg
POST https://gql.waveapps.com/graphql/public  

Set your header with Authorization = Bearer YOUR_AUTH_TOKEN

```graphql
query {
  businesses {
    edges {
      node {
        id
        name
      }
    }
  }
}
```
And you should be getting a response like below
```json
{
  "data": {
    "businesses": {
      "edges": [
        {
          "node": {
            "id": "XXXXXXXXXXXX",
            "name": "YOUR ACCOUNT NAME"
          }
        }
      ]
    }
  }
}
```

### Making a call to Wave API

Here are a simple script that I wrote to communicate with the wave module using axios
{% embed https://gist.github.com/GarrickBee/182f060f126ffef510742c2ce3247de0.js %}

### Creating Transaction function
Next is create a function to read from your desire file such as PDF, excel, csv. I will discuss further for statement reader in the next post to guide you through how to read the pdf statement. 

#### Extract info from your bank statement
```typescript
// Get pdf file
const  mbbPdfBuffer  =  fs.readFileSync("data/april-jun-2022.pdf");

// Get transaction list
const  mbbTransactionList  = await MBBPdfModule.convertMbbPdfBufferToTransactionList(mbbPdfBuffer);
```

#### Pass Information To Wave Module
```typescript
const transactionPromises = [];
// Loop transaction list
for (let i = 0; i < mbbTransactionList.length; i++) {
  const transaction = mbbTransactionList[i];
  // * Create Transaction
  transactionPromises.push(
    createTransaction({
      externalId: transaction.externalRefId,
      date: transaction.date,
      description: transaction.description,
      anchor: {
        accountId: WAVE_ACCOUNT_ID.MAYBANK_SAVING,
        amount: transaction.amount,
        direction:
          transaction.transactionDirection == TRANSACTION_DIRECTION.INCOME
            ? WAVE_TRANSACTION_DIRECTION.DEPOSIT
            : WAVE_TRANSACTION_DIRECTION.WITHDRAWAL,
      },
      lineItems: [
        {
          accountId: transaction.target.id,
          amount: transaction.amount,
          balance: WAVE_BALANCE_TYPE.INCREASE,
        },
      ],
    })
      .then((result) => {
        return { transactionId: transaction.externalRefId, ...result };
      })
      .catch((err) => {
        throw {
          transactionId: transaction.externalRefId,
          error: err.message,
        };
      })
  );
}
```

#### Post the result
```typescript
await Promise.allSettled(transactionPromises).then((results) => {
  for (const result of results) {
    if (result.status == "rejected") {
      report.errors.push(result.reason);
      continue;
    }

    report.success++;
  }
});
```

{% embed https://gist.github.com/GarrickBee/e7cc846f5c2c01493ba98418d58ffabc.js %}

## Final Outcome 
![Wave Transaction Cover](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6q2b8m1iye3sjvnmse7i.png)

The transaction is well grouped by your own custom category with the date and time of the transaction. 

What comes after is the juicy report graph which helps you under stand your transaction better.


![Automate Bank Statement Transaction Graph](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0nz27dwuii6ldinnurca.png)

### Conclusion
We have cover how we can use wave to act as the data storage for all of your transaction but it this blog post doesnt show you how to read the data from your pdf, the details of the module etc.

Things to cover in the upcoming post:

 1. Statement Reader code walk through 
 2. Automate Bot base on event
 3. Deploy application to AWS

Follow me on:
Website - https://garrick.monster/ 
Linkedin - https://www.linkedin.com/in/garrickng724/ 
Github - https://github.com/GarrickBee