This application contains Apex utilities for accessing the Xero REST APIs.
These classes are set up to use the Private Application type.
The aim of this project is to act as a starting point for accessing Xero APIs via Apex. I wanted to share this project as I spent a lot of time getting the authentication working for Xero, as Apex doesn't have a standard OAuth 1.0 library and there wasn't much detail online about it.
For more information about the Xero APIs, check out: http://developer.xero.com/documentation/api/api-overview/
- Create your private and public keys
- Setup -> Security Controls -> Certification and Key Management
- Create a Self-Signed Certification. Give it the name of XeroCertificate
- Click Download Certificate. This is the certificate you upload into Xero.
- Create your application in Xero and upload your public key .cer file generated above
- Deploy this package to a Salesforce environment (Deploy to Org)
- Access the Xero Settings Custom Setting (Setup -> Develop -> Custom Settings -> Click Manage on Xero Settings) and enter your Xero credentials (from your application created in Xero)
- You can now access Xero API resources via Apex. Eg...
XeroAccountingApi.getContacts();
Once the above steps are complete, you can now access the example methods to access the Xero API resources. There are currently only a few pre-built methods set up to start using. Please use these as a base and extend as necessary.
This method queries all contacts in your Xero org. To execute, simply run:
XeroAccountingApi.getContacts();
And a list of type XeroContact is returned.
This method creates a contact for the given XML:
// Create a Contact
XeroContact newContact = new XeroContact();
newContact.Name = 'ABC Limited 1';
// Send Contact to Xero
XeroAccountingApi.createContact(XeroXmlUtility.serialize(newContact, 'Contact'));
You can view example XML requests here
This method queries all invoices in your Xero org. To execute, run:
XeroAccountingApi.getInvoices();
This method creates an invoice for the given XML:
XeroInvoice newInvoice = new XeroInvoice();
newInvoice.Date_x = system.today();
... // Add additional Invoice details based on the XeroInvoice wrapper
// Send Invoice to Xero
XeroAccountingApi.createInvoice(XeroXmlUtility.serialize(newInvoice, 'Invoice'));
You can view example XML requests here
Feel free to fork this repo and use as you wish. I'd welcome anyone to add additional methods and add to this project, I will do the same as I go.