Interacting with Xero API through VBA (Microsoft Excel)
You can watch the project demo video on YouTube by clicking the image below:
This VBA script allows access and interaction with the Xero API through Microsoft Excel. It handles the authentication process (OAuth2) and the interactions with the API.
v1.0.0 current features:
- Handles Xero authentication (OAuth2) flows: Xero Auth Flow
- Generates "Profit and Loss" report
- Caches tokens; once authorized, access will be available for 60 days without re-login
- Calls the Xero API for Profit and Loss report and loads it into an Excel sheet
- Caches authorized Xero organization IDs; once retrieved, they will be saved within the Excel file
- Clears cached tokens and Xero organization IDs
I have written a step-by-step guide explaining how I implemented the authentication flow for this project.
You can check it out in this Medium article!
The Xero API has robust authentication, applying the industry-standard OAuth2, which is quite complex.
As finance and accounting generally perform analyses inside Microsoft Excel, there is a need for integration.
VBA lacks support for implementing this authentication, making it a challenge.
For example, part of the auth flow requires a browser for user login, while the only VBA built-in browser (Internet Explorer) has been deprecated since 2022.
Thanks to the community and their open-source projects, alternatives can be implemented.
I decided to make this project public as a significant part of it works due to open-source projects.
Hopefully, it will help anyone looking for VBA solutions related to API and OAuth2, just like me.
This project was built in the VBA 7.1 programming language. It was made possible thanks to open-source modules/packages:
- VBA-Web by Tim Hall
- Chromium Automation with CDP for VBA by ChrisK23 & Long Vh
- Calendar Input Form in Excel VBA by Siddharth Rout & logicworkz
How to get started using the scripts:
- Download the
Xero API - Demo.xlsm
file. - It contains all of the modules & forms inside
exported_source_code
and a sheet with a simple user interface. - The interface provides users with options to:
- Login: call out the browser to have the user log in to the Xero page
- Generate Report: generate Xero reports, currently capable of generating P&L reports only
- Clear Cache: as the script is capable of caching (tokens and organization details), this option can clear/delete all those caches.
- To start generating reports with the Xero API, you need to register for a Client ID & Secret on the Xero website.
- During registration, ensure that the
Redirect URI
you enter on the Xero page matches the one specified in the script. For a quick solution, usehttps://developer.xero.com/
, which is currently set in the script. You can view or modify theRedirect URI
through a private constant namedauth_RedirectUrl
within the XeroAuthenticator module. - Once obtained, provide your Client ID and Secret through an Input Box dialog while running the program, or enter them directly as private constants in the XeroAPICall module. Look for the constants named
cXEROCLIENTID
andcXEROCLIENTSECRET
.
You can modify the script as needed or transfer all modules, forms, and the interface sheet to your own Excel file.
Warning
The current authentication flow requires a Client Secret to be provided. This might have some risks as there is no secure place to store the Client Secret inside VBA/Excel.
Main user interface
Userform to select a report period
Userform to select a Xero organization
Generated report result; a new sheet with the requested formatted report
Distributed under the MIT License. See LICENSE.txt
for more information.