Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
News

Using Apilayer REST APIs from Delphi

As you might have seen online, Idera (Embarcadero parent company) recently acquired apilayer, a company offering a number of REST API micro-services, ranging from IP address geolocation to email and phone number checks, from financial information to weather and flight tracking. You can find a list of the company products at https://apilayer.com/. Each product has itw own web site, where you can generally sign in for a account with a limited number of free REST calls.

apilayer00-1445673

In this blog post, I don’t want to discuss the available services, but focus on how to call them from RAD Studio. I’ll use only two services, the free and open REST countries API and one of their premium geolocation services.

Using the REST Countries API

My starting point for exploring apilayer services is a simple and totally free service, available along with full documentation at https://github.com/apilayer/restcountries. To make an initial experiment, I’ve used the REST Debugger to query the service, using the name endpoint and optionally passing a parameter, as you can see below (with the parameter value “united”):

apilayer01

Once the data looks correct in the REST Debugger, you can just use the Copy Components button to make a snapshot of the configuration for the REST Client Library component required for building an application. Now, create a Delphi or C++ application in RAD Studio, either VCL or FMX, and paste the components in a data module — or a form if you are lazy. In this case I’ve used Delphi and VCL… and I was lazy.

I dropped a panel with an edit and a button and DBGrid (plus a DataSource component), wired them up, and executed the RESTRequest at design time to get a preview of the data:

apilayer02

 

The code for filtering is very simple:

procedure TForm43.Button1Click(Sender: TObject);
begin
if Edit1.Text = ” then
RESTRequest1.Resource := ‘name’
else
RESTRequest1.Resource := ‘name/’ + Edit1.Text;
RESTRequest1.Execute;
end;

The only other change I had to do was to change the TypesMode property of the RESTResponseDataSetAdapter1 component to JSONOnly, as some of the data analysis was mistakenly trying to convert some timezone related text to a date, resulting in an exception.

Reverse GeoCoding with the Position Stack API

Next I made an attempt at using the free level of a paid API, https://positionstack.com/. This service offers direct and inverse geocoding, mapping services, and more. Here for example I’ve done a simple “forward” geocoding, providing an address and reading the latitude, longitude, and other local information in the REST Debugger. To obtain the tabular result I had to configure the JSON root using the data element:

apilayer03

In this case, you need to sign up for a developer key and enter it as an additional parameter for the request. (You might want to consider encoding that key rather than having it as a plain string in the final application.)

Now, similarly to what I’ve done above, we can copy the components into a RAD Studio application and have a geo coding FMX app good to go, this time based on Live Bindings. Also this app can display data at design time, in this case the latitude and longitude of my city (plus another city with the same name in the USA):

apilayer04

Conclusion

Leveraging micro-service APIs for application development, both desktop and mobile, can really speed up building and deploying innovative features, and have them on a scalable infrastructure at the same time. I’m looking forward to leverage more of the apilayer services and encourage you to have a look at what they can offer for your current of future projects.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Get some love as Delphi turns 26
About author

Marco is one of the RAD Studio Product Managers, focused on Delphi. He's the best selling author of over 20 books on Delphi.

Leave a Reply

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

IN THE ARTICLES