Introducing the .NET wrapper for the Sage One Accounting API

Photo by Paulina H. on Unsplash

Introducing the .NET wrapper for the Sage One Accounting API

A while back my accountant convinced me to move our accounting package to Sage One Online. Not only am I very happy with the change, I’m also intrigued with their API, which enables developers to integrate with Sage One and perform a wide range of tasks . As a result I’ve created an open source project with the aim to create a fully fledged .Net wrapper library for the Sage One Accounting API. The code is available on GitHub, so please feel free to grab your fork and start contributing! The project is still under active development and I’m adding features as I need them. As of writing, the library supports the following operation s:

  • Account

    • Get All
    • Get
    • Save (Create and Update)
    • Delete
    • Get with System Accounts
    • Get by Category
  • Customer

    • Get All
    • Get
    • Save (Create and Update)
    • Delete
  • Company

    • Get Current
  • Item

    • Get All
    • Get
    • Save (Create and Update)
    • Delete
  • Categories (Account, Asset, Bank Account, Customer, Item, Supplier)

    • Get All
    • Get
    • Save (Create and Update) excl. Account Categories
    • Delete
  • Sales Representative

    • Get All
    • Get
    • Save (Create and Update)
    • Delete
    • HasActivity
  • Supplier

    • Get All
    • Get
    • Save (Create and Update)
    • Delete
  • Tax Invoice

    • Get All
    • Get
    • Save (Create and Update)
    • Calculate

For example to get a list of customers on Sage One you’ll use the following C# code:

public void GetAllCustomers()
{
    var api = new ApiRequest(Username, Password, Apikey, CompanyId);
    var customers = api.CustomerRequest.Get();
}

The Sage One API is pretty neat in the fact that it support OData queries out of the box. For example to find a customer with an e-mail address, you can simply pass in a filter parameter to the Get method:

public void GetCustomerByEmail()
{
    string filter = "Email eq 'info@contoso.com'";
    var api = new ApiRequest(Username, Password, Apikey, CompanyId);
    var customers = api.CustomerRequest.Get(filter);
}

In case you’re wondering how OData queries work, take a look at the following example. The load a customer by ID, you’ll use the following:

public void GetCustomer()
{
    int customerId = 12345;
    var api = new ApiRequest(Username, Password, Apikey, CompanyId);
    var customer = api.CustomerRequest.Get(customerId);
}

In order to create a new Sage One Customer, you’ll need to create a new instance of the Customer class, as illustrated below:

public void SaveCustomer()
{
    var api = newApiRequest(Username, Password, Apikey, CompanyId);
    var customer = new Customer
    {
        Name = "Mr. David R. Robinett",
        AcceptsElectronicInvoices = true,
        Active = true,
        AutoAllocateToOldestInvoice = true,
        Balance = 0,
        ContactName = "Mr. David R. Robinett",
        DeliveryAddress01 = "Pappelallee 6667",
        DeliveryAddress02 = "Solingen",
        DeliveryAddress03 = "Nordrhein-Westfalen",
        DeliveryAddress04 = "42651",
        DeliveryAddress05 = "Germany",
        CommunicationMethod = 1,
        PostalAddress01 = "Pappelallee 6667",
        PostalAddress02 = "Solingen",
        PostalAddress03 = "Nordrhein-Westfalen",
        PostalAddress04 = "42651",
        PostalAddress05 = "Germany",
        Telephone = "238-555-0100",
        SalesRepresentativeId = null
    };
    api.CustomerRequest.Save(customer);
}

To delete a Sage One Customer, simply call the Delete method:

public void Delete()
{
    int customerId = 12345;
    var api = new ApiRequest(Username, Password, Apikey, CompanyId);
    var result = api.CustomerRequest.Delete(customerId);
}

To create a Tax Invoice:

public void CreateInvoice()
{
    var api = new ApiRequest(Username, Password, Apikey, CompanyId);
    var customerId = 0;
    var salesRepId = 0;
    var itemId = 0;
    var taxTypeId = 0;
    TaxInvoice invoice = new TaxInvoice();
    var customer = api.CustomerRequest.Get(customerId);
    var salesRep = api.SalesRepresentativeRequest.Get(salesRepId);
    // Must set both CustomerId and Customer in order to work
    invoice.CustomerId = customerId;
    invoice.Customer = customer;
    // Must set both SalesRepresentativeId and SalesRepresentative in order to work
    invoice.SalesRepresentativeId = salesRepId;
    invoice.SalesRepresentative = salesRep;
    invoice.Date = DateTime.Now;
    invoice.DueDate = new DateTime(2015, 12, 12);
    invoice.Lines = new List<CommercialDocumentLine>();
    var line1 = new CommercialDocumentLine
    {
        SelectionId = itemId, // This must be an item or account id
        TaxTypeId = taxTypeId, // Use TaxTypeRequest to get list of Tax Types
        LineType = 0, // 0=Item/1=Account
        Quantity = 1,
        UnitPriceExclusive = 390,
        UnitPriceInclusive = 390,
        DiscountPercentage = 0
    };
    invoice.Lines.Add(line1);
    var newTaxInvoice = api.TaxInvoiceRequest.Save(invoice);
}

Lots more to come. Keep watching this space!