-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathmain.js
61 lines (49 loc) · 2.12 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { randomUUID } from 'crypto'
import dotenv from 'dotenv'
import NordigenClient from './lib/index.js';
dotenv.config();
// Get secretId and secretKey from bankaccountdata.gocardless.com portal and pass to NordigenClient or load from .env file
const client = new NordigenClient({
secretId: process.env.SECRET_ID,
secretKey: process.env.SECRET_KEY
});
// Generate new access token
const tokenData = await client.generateToken();
// Get access and refresh token
// Note: access_token is automatically injected to other requests after you successfully obtain it
const token = tokenData.access;
const refreshToken = tokenData.refresh;
// Exchange refresh token
const newToken = await client.exchangeToken({refreshToken: refreshToken});
// Use existing token
client.token = process.env.TOKEN;
// Get list of institutions
const institutions = await client.institution.getInstitutions({country: "LV"});
// As example we will use Revolut institution id that you can get from getInstitutions response
const institutionId = "REVOLUT_REVOGB21";
// Initialize new bank session
const init = await client.initSession({
redirectUrl: "https://gocardless.com",
institutionId: institutionId,
referenceId: randomUUID(),
})
// Get link to authorize in the bank
// Authorize with your bank via this link, to gain access to account data
const link = init.link;
// requisition id is needed to get accountId in the next step
const requisitionId = init.id;
// Get account id after completed authorization with a bank
const requisitionData = await client.requisition.getRequisitionById(requisitionId);
const accountId = requisitionData.accounts[0];
// Create account data instance
const account = client.account(accountId);
// Fetch account metadata
const metadata = await account.getMetadata();
// Fetch account balances
const balances = await account.getBalances();
// Fetch account details
const details = await account.getDetails();
// Fetch account transactions
const transactions = await account.getTransactions();
//Optional. You can filter transactions by specific date range
await account.getTransactions({dateFrom: "2021-12-01", dateTo: "2022-01-30"});