Saturday, 22 May 2021

VAT's in it for me? - or I call my layer.

 - or a shout-out to the various services of apilayer.com



Sorry about the intended pun in the title - but it will all make sense.

Have you ever wanted to know the various VAT rates in the EU - no? - well neither have I, because it might put me in a bad mood, but it fits the intended pun for the post, and I also wanted to show how it is done from from Delphi.

The mother company of Embarcadero, Idera acquired another company early this year - this time apilayer.com, which provides numerous "simple" cloud-based API services - among these are:

  • IP geolocation and reverse lookup
  • Language detection
  • Mail address validation
  • Phone number validation
  • Flight tracking
  • Currency conversion and rates (including crypto currency)
  • Weather data and forecast
  • News, Headline and Stock apis
  • Conversion PDF and scraping
But go to https://apilayer.com/, and read more about their various layers and tiers for these.

We will in this short demo look at the vatlayer - which does EU VAT stuff.

So I started by signing up for the free tier on https://vatlayer.com/ - which gives you an API access key and access to a dashboard. As always do not share the access key - just saying.

Now you can fire up Delphi, create a new application and either use the REST Debugger or just manually throw in the REST component or create runtime - what you prefer.

I just threw in a edit control and a couple of memos and buttons, as seem above. And then I added an parameter on the RESTClient with the access_key - since I wanted to clear the request parameters on the RESTRequest, but keep the access_key. And set the BaseURL property to http://apilayer.net/api.

I just took two of the methods/resources from https://vatlayer.com/documentation - so the VAT lookup button looks like this:

var
  json: TJSONValue;
begin
  Memo1.Clear;
  RESTRequest1.Method := rmGET;
  RESTRequest1.Resource := 'validate';
  RESTRequest1.Params.Clear;
  RESTRequest1.AddParameter('vat_number', LabeledEdit2.Text, pkGETorPOST);
  RESTRequest1.Execute;
  if RESTResponse1.StatusCode=200 then
  begin
    json := RESTResponse1.JSONValue;
    Memo1.Lines.Add(json.GetValue<string>('company_name'));
    Memo1.Lines.Add(json.GetValue<string>('company_address'));
  end;
end;

..and the VAT rates for HU (since they have a higher VAT rate than DK :D) button looks like this:

var
  json: TJSONValue;
begin
  Memo2.Clear;
  RESTRequest1.Method := rmGET;
  RESTRequest1.Resource := 'rate';
  RESTRequest1.Params.Clear;
  RESTRequest1.AddParameter('country_code', 'HU', pkGETorPOST);
  RESTRequest1.Execute;
  if RESTResponse1.StatusCode=200 then
  begin
    json := RESTResponse1.JSONValue;
    Memo2.Lines.Add(json.Format());
  end;
end;

There nice twists to these - by IP address or get list of rate types - and if combining these services by apilayer.com - I could make a speeding white-van "whistle-blower" mobile app - since all company owned registered vehicles in DK - need to have their VAT numbers on their fleet.

So I could lookup the company and their address, get their phone number by a different api layer - and then call and tell on them - MUHAAHAAAHAA (evil laughter)

..or maybe I should just create something useful and fun.

And the best part about writing this post, I now learned that Denmark has only the second highest VAT rate in EU :D

/Enjoy