(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.
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. Having said that, Xero has now migrated to OAuth 2.0 so it's a bit easier however there is still some setup involved. Once the authentication is done the rest of the API is relatively straight forward.
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://identity.xero.com/connect/token
- User Info Endpoint URL =
https://identity.xero.com/connect/userinfo
- Token Issuer =
https://identity.xero.com
- 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://developer.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();
Note: If you want to connect to multiple Xero Orgs, you would need to create a more scalable solution for this, such as a Custom Object or Custom Setting/Metadata to track the various Xero connections.
In order to use Xero Webhooks, some additional setup is required:
- First, you need to create a public URL for Xero to send webhooks to. To do this:
- Setup -> Develop -> Sites -> New (Note: you could use an existing Site if you prefer)
- Give the Site a name. Could be "API" or "Webhooks" or what is preferred.
- Give the site a URL suffix (eg. "api")
- For Active Site Homepage, select InMaintance or you could create a landing page. This isn't actually needed for this scenario
- Click Save, then Public Access Settings
- Under Enabled Apex Classes, select XeroWebhook and save.
- Now, you need to enable Xero Webhooks in Xero. Following the instructions here: https://developer.xero.com/documentation/guides/webhooks/creating-webhooks/
- For the Notifications URL, enter the domain and URL created above and also include
/services/apexrest/xero/webhook
which is the endpoint for the Apex Class XeroWebhook. For examplehttps://mydomain.my.salesforce-sites.com/api/services/apexrest/xero/webhook
. You can test the URL by navigating to it, you should receive a "HTTP Method 'GET' not allowed. Allowed are POST" error, which at least means the URL is correct - Click Save and copy the "Webhook Key"
- Paste this key into the Xero Settings custom setting.
- For the Notifications URL, enter the domain and URL created above and also include
- Lastly, you need to activate the "Intent to Receive". This is a security measure which tells Xero the webhooks are signed and going to the right place.
- Setup -> Develop -> Custom Settings -> Xero Settings -> Manage
- Check "Intent to Receive" and click save
- Click "Send Intent to Recieve" on the Xero side. This will send a few messages to Salesforce and Salesforce will validate these are received and Xero will validate they're processed correctly. Once Xero returns "OK", go back to Salesforce and uncheck "Intent to Receive".
- Webhooks should now be sending! You will need to update the
XeroWebhook.processRecordChange()
with your logic to process webhooks how you like.
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.