(deploy from Github from https://githubsfdeploy.herokuapp.com/app/githubdeploy/benedwards44/Apex-for-Xero)
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 an Auth. Provider within your Salesforce Org:
- Setup -> Security Controls -> Auth. Providers -> New
- Provider Type = "Open ID Connect"
- Name = "Xero"
- Consumer Key = "ABC" (this is temporary, we will update this with the Xero Consumer Key once we have it)
- Consumer Secret = "ABC" (as above)
- Authorize Endpoint URL = "https://login.xero.com/identity/connect/authorize"
- Token Endpoint URL = "https://login.xero.com/identity/connect/token"
- Default Scopes: "openid profile email offline_access accounting.transactions accounting.contacts" (you can see all scopes here https://developer.xero.com/documentation/oauth2/scopes)
Leave everything as is. Click save and then copy the generated "Callback URL". Eg. https://login.salesforce.com/services/authcallback/00D2v000003QVUrCAO/Xero
-
Create the Xero App:
- https://app.xero.com/ -> My Apps -> New app
- App name = Your unique name
- Company or application URL = Can be anything, suggest either your Salesforce Org URL or your company website
- OAuth 2.0 redirect URI = Paste in the "Callback URL" copied from step 1 above
- Take the generated Client Id and Client Secret and paste into the Auth. Provider created above
-
Create the Salesforce Named Credential:
- Setup -> Named Credential -> New Named Credential
- Label = "Xero"
- Name = "Xero"
- URL = "https://api.xero.com"
- Identity Type = "Named Principal"
- Authentication Protocol = "OAuth 2.0"
- Authentication Provider = "Xero" (the provider created in step 1)
- Start Authentication Flow on Save = Checked (this will trigger the OAuth process to Xero)
-
Deploy this package to a Salesforce environment (Deploy to Org)
-
You now need to retrieve the Xero Tenant ID and store in the Custom Label:
- Run the Apex method
XeroAPI.getXeroTenantId();
- Copy the returned value
- Update to the label: Setup -> Create -> Custom Labels -> Xero_Tenant_Id -> Edit -> Paste in value from above
- Run the Apex method
-
You can now access Xero API resources via Apex. Eg...
XeroAPI.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:
XeroAPI.getContacts();
And a list of type XeroContact is returned.
// Send Contact to Xero
Account myAccount = [SELECT Name, ... FROM Account];
XeroAPI.sendAccount(myAccount);
You can view example JSON requests here
This method queries all invoices in your Xero org. To execute, run:
XeroAPI.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
XeroAPI.sendInvoice(XeroXmlUtility.serialize(newInvoice, 'Invoice'));
You can view example JSON 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.