Skip to content

Instantly share code, notes, and snippets.

@graphicbeacon
Last active April 21, 2022 10:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save graphicbeacon/cc905a338c16091716cde774e0a6505b to your computer and use it in GitHub Desktop.
Save graphicbeacon/cc905a338c16091716cde774e0a6505b to your computer and use it in GitHub Desktop.
How to create an application using the Monzo Bank API (Full solution)
const express = require('express');
const request = require('request');
const app = express();
const oauthDetails = {
client_id: '[your client id]',
client_secret: '[your client secret]',
redirect_uri: 'http://localhost:3000/oauth/callback'
};
// Will be populated once received
let accessToken = null;
app.get('/', (req, res) => {
const { client_id, redirect_uri } = oauthDetails;
const monzoAuthUrl = 'https://auth.monzo.com';
res.type('html');
res.send(`
<h1>Hello</h1>
<form action="${monzoAuthUrl}">
<input type="hidden" name="client_id" value="${client_id}" />
<input type="hidden" name="redirect_uri" value="${redirect_uri}" />
<input type="hidden" name="response_type" value="code" />
<button>Authorise app</button>
</form>
`);
});
app.get('/oauth/callback', (req, res) => {
const { client_id, client_secret, redirect_uri } = oauthDetails;
const { code } = req.query;
const monzoAuthUrl = `https://api.monzo.com/oauth2/token`;
request.post({
url: monzoAuthUrl,
form: {
grant_type: 'authorization_code',
client_id,
client_secret,
redirect_uri,
code
}
}, (err, response, body) => {
accessToken = JSON.parse(body); // Populate accessToken
res.redirect('/accounts');
});
});
app.get('/accounts', (req, res) => {
const accountsUrl = 'https://api.monzo.com/accounts';
const { token_type, access_token } = accessToken;
request.get(accountsUrl, {
headers: {
Authorization: `${token_type} ${access_token}`
}
}, (req, response, body) => {
const { accounts } = JSON.parse(body);
res.type('html');
res.write('<h1>Accounts</h1><ul>');
for(let account of accounts) {
const {id, type, description } = account;
res.write(`
<li>
${description}(<i>${type}</i>) - <a href="/transactions/${id}">View transaction history</a>
</li>
`);
}
res.end('</ul>');
});
});
app.get('/transactions/:acc_id', (req, res) => {
const { acc_id } = req.params;
const { token_type, access_token } = accessToken;
const transactionsUrl = `https://api.monzo.com/transactions?expand[]=merchant&account_id=${acc_id}`;
request.get(transactionsUrl, {
headers: {
Authorization: `${token_type} ${access_token}`
}
}, (req, response, body) => {
const { transactions } = JSON.parse(body);
res.type('html');
res.write(`
<h1>Transactions</h1>
<table>
<thead>
<th>Description</th>
<th>Amount</th>
<th>Category</th>
</thead>
<tbody>
`);
for(let transaction of transactions) {
const {
description,
amount,
category
} = transaction;
res.write(`
<tr>
<td>${description}</td>
<td>&pound;${(amount/100).toFixed(2)}</td>
<td>${category}</td>
</tr>
`);
}
res.write('</tbody></table>');
res.end('<br /><a href="/accounts">&lt; Back to accounts</a>');
});
});
app.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment