Xero Accounting Software wallpaper

The integration with Xero seems quite complicate. I explained in my previous post, how to create the token to integrate your application with Xero. Now, I’m going to explain the code for getting organization list from Xero in C# applications.

My goal is to have a package for a Xero Connector with basic functionalities. So, I can use it and share it. The functionalities I want to have are:

With these basic functionalities, I will have the ability to satisfied most of the client requests. It is time to start.

The first step in the integration is to give from Xero your TenandId. The TenantId is the GUID of your organization. Probably, you have more than one organization in your account like me:

  • Demo Company is the company Xero creates for you so you can play without breaking your real data
  • Your company where you have all your real invoices, clients and so on
Organizations in Xero
Organizations in Xero

In the Xero home page, I see that the dropdown list at the top left shows the selected current company (Demo Company (global)). If I click on it, the menu shows other options but in particular my other organizations or I can create a new one.

Remember I had an access token from Xero from Postman and ClientId and ClientSecret from the Xero developer portal. See my other post to understand how I got them.

Quick look at Postman

Call Organization in Postman
Call Organization in Postman

In the Postman collection from the Xero documentation, I see what url is using and what headers are required in the request. What I get is a json with the list of organizations.

[
    {
        "id": "dd69c6a4-12e2-43fb-9578-87ee27f12887",
        "tenantId": "1c0611b5-8fd4-4c1d-a300-04d7b1eaf89d",
        "tenantType": "ORGANISATION",
        "tenantName": "Demo Company (Global)",
        "createdDateUtc": "2020-05-12T15:24:36.1047450",
        "updatedDateUtc": "2020-05-12T15:24:36.1069920"
    }
]

Here I see the TenandId, required for next calls.

Time for coding in C#

Based on the json I got in Postman, I create a model for the list of organizations.

OrganizationResponse

/// <summary>
/// Class OrganizationResponse.
/// </summary>
public class OrganizationResponse : BaseResponses
{
    /// <summary>
    /// Gets or sets the organizations.
    /// </summary>
    /// <value>The organizations.</value>
    public IList<Organization> Organizations { get; set; }
}

Organization model

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;

namespace PSC.Accounting.Models.Common
{
    /// <summary>
    /// Class Organization.
    /// </summary>
    public class Organization
    {
        /// <summary>
        /// Gets or sets the identifier.
        /// </summary>
        /// <value>The identifier.</value>
        [JsonProperty("id")]
        public string Id { get; set; }

        /// <summary>
        /// Gets or sets the tenant identifier.
        /// </summary>
        /// <value>The tenant identifier.</value>
        [JsonProperty("tenantId")]
        public string TenantId { get; set; }

        /// <summary>
        /// Gets or sets the type of the tenant.
        /// </summary>
        /// <value>The type of the tenant.</value>
        [JsonProperty("tenantType")]
        public string TenantType { get; set; }

        /// <summary>
        /// Gets or sets the name of the tenant.
        /// </summary>
        /// <value>The name of the tenant.</value>
        [JsonProperty("tenantName")]
        public string TenantName { get; set; }

        /// <summary>
        /// Gets or sets the created date UTC.
        /// </summary>
        /// <value>The created date UTC.</value>
        [JsonProperty("createdDateUtc")]
        public DateTime CreatedDateUtc { get; set; } = DateTime.Now;

        /// <summary>
        /// Gets or sets the updated date UTC.
        /// </summary>
        /// <value>The updated date UTC.</value>
        [JsonProperty("updatedDateUtc")]
        public DateTime? UpdatedDateUtc { get; set; }
    }
}

Now that I defined what I receive for getting organization list from Xero in C#, I have to call the endpoint. To do that, I use RestSharp. The request is a simple GET and in the header the access token is required.

/// <summary>
/// Gets the organizations.
/// </summary>
/// <returns>OrganizationResponse.</returns>
public OrganizationResponse GetOrganizations()
{
    OrganizationResponse rtn = new OrganizationResponse();

    _client = new RestClient("https://api.xero.com/connections");
    _client.Timeout = -1;

    var request = new RestRequest(Method.GET);
    request.AddHeader("Authorization", "Bearer " + _settings.AccessToken);
    var response = _client.Execute<IList<Organization>>(request);

    rtn.HttpStatusCode = response.StatusCode;
    rtn.Status = (int)response.StatusCode == 200 ? 
                 PSC.Common.Enums.ResponseStatus.Success : 
                 PSC.Common.Enums.ResponseStatus.Failed;
    if (response.StatusCode == System.Net.HttpStatusCode.OK)
    {
        rtn.Organizations = response.Data;
    }

    return rtn;
}

So, let me explain few things although the code I think is clear. _client.Execute is calling the endpoint and passing the header and parameters. Then, Xero API returns something: the HttpStatusCode tells us if the call has had success or not (200 is OK). I translate this status in my internal status.

If the result is HttpStatusCode OK, I put all organizations from Xero to my OrganizationResponse.

Happy coding!

By Enrico

My greatest passion is technology. I am interested in multiple fields and I have a lot of experience in software design and development. I started professional development when I was 6 years. Today I am a strong full-stack .NET developer (C#, Xamarin, Azure)

This site uses Akismet to reduce spam. Learn how your comment data is processed.