Site icon Embarcadero RAD Studio, Delphi, & C++Builder Blogs

Automate Access To Current And Historical Currency Exchange Rates Via API

exchangeratedelphiide

Currency exchange rates are an interesting financial dataset similar to stock quotes and cryptocurrency datasets. For building solutions like software or a POS system for a currency exchange business, it is a necessary dataset and there are APIs online that give you easy access to this data. One API that is available is from ExchangeRatesAPI.io which is owned by apilayer (a sister company of Embarcadero). The API is easy to use and it is even easier to build cross-platform currency exchange rate solutions with RAD Studio and Delphi for Windows, Android, iOS, macOS, and Linux with a single codebase and single UI.

Delphi (and RAD Studio) includes a tool called the REST Debugger where you can configure all of your REST API settings and then export them as components into your Delphi application. This includes wiring up the incoming data automatically to an in-memory database table (TFDMemTable). It literally takes only a few minutes to get up and running with the API from ExchangeRatesAPI.io API from within Delphi and RAD Studio.

In this post, we’ll dive into the ExchangeRatesAPI.io API and see how to build a desktop and mobile application utilizing its REST API with the use of IDE Software.

How can I get currency exchange rates with an API?

The two important API calls which will be used in this project are (full documentation available here):

https://api.exchangeratesapi.io/v1/symbols?access_key=API_KEY – This first API call returns a list of all available currencies including a symbol and full name for each.

[crayon-6767a9742a15c829362454/]

https://api.exchangeratesapi.io/v1/latest?access_key=API_KEY&base=USD – This second API call returns real-time exchange rate data from the USD currency to all other currencies which gets updated every 60 minutes, every 10 minutes, or every 60 seconds.

[crayon-6767a9742a167883237954/]

As you can see these are pretty straightforward APIs to get both a list of the available currencies and a list of real-time (or delayed) conversion rates from one of the currencies to the rest of the currencies.

How do I connect to the ExchangeRatesAPI.io API REST end point with Delphi?

This is what it looks like by taking the first API call above and plugging it into the REST Debugger in Delphi. You’ll notice how the JSON Root Element field is set to symbols. That takes the symbols element in JSON and makes it the root element to turn into the in-memory table dataset.

Once you have made the API call how you want it you can click the Copy Components button and then paste that into the application form in the Delphi IDE. It will create a TRESTClient, TRESTRequest, TRESTResponse, TRESTResponseDatasetAdapter, and TFDMemTable. This can be done for both of the API calls listed above. You can actually right click on the TRESTRequest component at this point and select Execute to execute the REST request at design time and fill the TFDMemTable with data so that you can use LiveBindings to bind the data to other controls.

How can I pivot the dataset of an TFDMemTable in Delphi?

In the case of this API the response data is in the form of a JSON object so each item is a column instead of the data being in the form of a JSON array which would have made each item a row. I prefer to have each item as a row so I perform a simple pivot of the data using the following code with the data from the /symbols API call. The TmpCurrencyMemTable contains the data directly from the REST request and all of the items are in columns. The CurrencyMemTable is the pivoted version of the data. You can see there is a for loop that loops through the fields of the TmpCurrencyMemTable and then appends their names and value to the CurrencyMemTable one record at a time.

[crayon-6767a9742a174451864564/]

The same operation is done for the second /latest API query with the TmpExchangeMemTable and the ExchangeMemTable.

[crayon-6767a9742a179345696935/]

Now that we have covered the two API calls in the application and how to create components automatically for those two API calls lets take a look at the demo application.

How can I build a Windows desktop application?

We have a TLayout bar at the top with a Menu button and a Refresh button. Additionally, we gave a TEdit in the middle which features the value Base value for the /latest API call. There is a TMultiView setup on the left as a pushing drawer which pushes the TStringGrid on the right. The TMultiView contains a TListBox which holds the pivoted data from the /symbols API call. It is controlled by the Menu button. The TStringGrid contains the pivoted data results from the /latest API call. At the bottom of the view, there are some TLabels which display the current Base value and the selected conversion value (in this case 1 USD equals 44.21 UYZ).

How can I customize the format of LiveBindings text?

LiveBindings is used to fill the TLabels at the bottom of the window. The CustomFormat property is used to format the display of the two labels. The TListBox and the TStringGrid data is also filled via LiveBindings. The first CustomFormat you can see concatenates 3 strings together with %s representing the original value of the dataset field.

[crayon-6767a9742a17c440743605/]

The second LiveBindings CustomFormat concatenates 4 strings together with the Self.Owner.FieldByName(‘CurrencyValue’).Text code accessing the CurrencyValue field and the Self.Owner.FieldByName(‘Symbol’).Text code accessing the Symbol field from the dataset.

[crayon-6767a9742a17e340499957/]

In this article we have demonstrated how to build a Windows 10 desktop application to access a list of currencies and a list of the latest exchange rates in relation to all of the other currencies. The desktop application is using the Delphi FireMonkey framework which supports Android, iOS, macOS, Windows, and Linux with a single codebase and responsive UI. The same application demoed here can also run on the other 4 platforms. We’ve shown how easy it is to access a REST API in Delphi and how to access the APIs from ExchangeRatesAPI.io. Lastly, we’ve show how to pivot the data and livebind it to the visual controls within the application.

Check out the full source code for accessing currency exchange rates via a REST API on GitHub.

Head over and get your own free API key for the currency exchange rates API.

Exit mobile version