- Published on
How to get recent transactions from HDFC Bank via Unofficial API
- Authors
- Name
- Ashik Nesin
- @AshikNesin
Whether you're building you're own personal finance app or some custom app that requires your bank transaction it is usually hard to get an api access from your bank.
In our case, we'll getting those details from their internal api that they're using for their mobile web app.
For browser automation we'll be using Google puppeteer
const puppeteer = require('puppeteer');
const username = 'YOUR_USERNAME';
const password = 'YOUR_PASSWORD';
const launchConfig = {
headless: false,
};
let browser;
let recentTransactions;
(async () => {
browser = await puppeteer.launch({
...launchConfig,
});
try {
const page = await browser.newPage();
const loginUrl = 'https://mobilebanking.hdfcbank.com/mobilebanking/#login';
// Go to the url and wait until it is loaded
await page.goto(loginUrl, {
waitUntil: ['domcontentloaded', 'networkidle0'],
});
await page.waitForSelector('#fldLoginUserId');
await page.type('#fldLoginUserId', username);
await page.waitFor(5000);
await page.click('[name="fldSubmit"]');
await page.waitForSelector('#upass');
await page.type('#upass', password);
await page.click('#chkLogin');
await page.click('[name="fldSubmit"]');
await page.waitForResponse(
'https://mobilebanking.hdfcbank.com/mobilebanking/Views/Menu/mymenu.html'
);
await page.goto(
'https://mobilebanking.hdfcbank.com/mobilebanking/#rrsin01'
);
await page.waitFor('#fldFromDate', { visible: true });
await page.evaluate(() => {
// https://stackoverflow.com/a/1648448/5012005
function addMonths(date, months) {
date.setMonth(date.getMonth() + months);
return date;
}
// https://stackoverflow.com/a/13460045/5012005
function formattedDate(d = new Date()) {
return [d.getDate(), d.getMonth() + 1, d.getFullYear()]
.map(n => (n < 10 ? `0${n}` : `${n}`))
.join('/');
}
const fromDt = formattedDate(addMonths(new Date(), -6));
const toDt = formattedDate();
document.querySelector('#fldFromDate').setAttribute('value', fromDt);
document.querySelector('#fldToDate').setAttribute('value', toDt);
});
await page.select('#fldNbrStmt', '40');
await page.waitFor(3000);
await page.click('#frmsin01 [type="submit"]');
const response = await page.waitForResponse(
'https://mobilebanking.hdfcbank.com/meap/apps/services/api/meap/mobilewebapp/query'
);
recentTransactions = await response.json();
console.log(JSON.stringify(recentTransactions, null, 2));
await page.waitFor(3000);
// Logout
await page.click('#logoutbutton a');
await page.close();
} catch (error) {
console.log(error);
} finally {
if (browser !== null) {
await browser.close();
}
}
})();