NodeJS client for Xero bank feeds API with OAuth 2.0 support
npm install xero-node-bankfeeds``sh`
npm install --save xero-node-bankfeeds
* Create a free Xero user account (if you don't have one)
* Login to Xero developer center
* Click "Try oAuth2" link
* Enter your App name, company url, privacy policy url.
* Enter the redirect URI (this is your callback url - localhost, etc)
* Agree to terms and condition and click "Create App".
* Click "Generate a secret" button.
* Copy your client id and client secret and save for use later.
* Click the "Save" button. You secret is now hidden.
Start with an empty folder
sh
npm init
`$3
`sh
npm install --save xero-node-bankfeeds
``sh
npm install express --save
``sh
npm install express-session --save
`Create your index.js using the code below - don't forget to add your client id and secret
`js
'use strict';const express = require('express');
const session = require('express-session');
const xero_node_bankfeeds = require('xero-node-bankfeeds');
const client_id = 'YOUR-CLIENT_ID'
const client_secret = 'YOUR-CLIENT_SECRET'
const redirectUri = 'http://localhost:5000/callback'
const scopes = 'openid profile email bankfeeds offline_access'
const xeroClient = new xero_node_bankfeeds.XeroBankFeedClient({
clientId: client_id,
clientSecret: client_secret,
redirectUris: [redirectUri],
scopes: scopes.split(" "),
});
let app = express()
app.set('port', (process.env.PORT || 3000))
app.use(express.static(__dirname + '/public'))
app.use(session({
secret: 'something crazy',
resave: false,
saveUninitialized: true,
cookie: { secure: false }
}));
app.get('/', function(req, res) {
res.send('Connect to Xero');
})
app.get('/connect', async function(req, res) {
try {
let consentUrl = await xeroClient.buildConsentUrl();
res.redirect(consentUrl);
} catch (err) {
res.send("Sorry, something went wrong");
}
})
app.get('/callback', async function(req, res) {
const url = "http://localhost:5000/" + req.originalUrl;
await xeroClient.setAccessTokenFromRedirectUri(url);
// Optional: read user info from the id token
let tokenClaims = await xeroClient.readIdTokenClaims();
const accessToken = await xeroClient.readTokenSet();
req.session.tokenClaims = tokenClaims;
req.session.accessToken = accessToken;
res.redirect('/feedconnections');
})
app.get('/feedconnections', async function(req, res) {
try {
const accessToken = req.session.accessToken;
await xeroClient.setTokenSet(accessToken);
// CREATE
var feedConnection = new xero_node_bankfeeds.FeedConnection();
feedConnection.accountName = "SDK Test Account";
feedConnection.accountNumber = "123321";
feedConnection.accountToken = "foobar321";
feedConnection.accountType = xero_node_bankfeeds.FeedConnection.AccountTypeEnum.BANK;
feedConnection.currency = xero_node_bankfeeds.CurrencyCode.GBP;
const feedConnections = new xero_node_bankfeeds.FeedConnections();
feedConnections.items = [feedConnection];
const response = await xeroClient.bankFeedsApi.createFeedConnections(xeroClient.tenantIds[0], feedConnections);
res.send("Bank account create with ID: " + response.body.items[0].id );
} catch (err) {
console.log(err.body);
res.send("Sorry, something went wrong");
}
})
const PORT = process.env.PORT || 5000;
app.listen(PORT, function() {
console.log("Your Xero basic public app is running at localhost:" + PORT)
})
`#### Project Structure
`
src/
|- gen/ autogenerated TypeScript
- *.ts handwritten TypeScript