![smartmockupsks80sdpc | Embarcadero RAD Studio Delphi C++Builder Blogs smartmockups ks80sdpc](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/08/smartmockups_ks80sdpc-9039137.jpg?resize=1140%2C694&ssl=1)
Wouldn’t it be nice to know where your customers access your website from and customize user experiences based on IP?! ipstack offers a powerful, real-time IP to geolocation API capable of looking up accurate location data and assessing security threats originating from risky IP addresses. Results are delivered within milliseconds in JSON or XML format. Using the ipstack API you will be able to locate website visitors at first glance and adjust your user experience and application accordingly.
In this article, we will see how fast and easy it is to use Windows App Development tools like RAD Studio and Delphi to create a Firemonkey multidevice application using the LowCode Wizard in addition to a REST client library to take advantage of ipStack API and retrieve a JSON format response for IP to geolocation in real-time.
ipstack API has years of partnership with large ISPs ensure accurate and consistent data any day of the year and covers over 2 million unique locations in more than 200.000 cities around the world.
Our RAD Studio and Delphi applications will be able to call the API and request information based on the name of parameters you provide.
Table of Contents
How do I set up the IpStack API?
Make sure you refer to IpStack API website (https://IpStack.com/) and and SignUp for the free Plan providing only your email and some basic information (no credit card required). Once you are in the website will redirect you to a Quickstart guide dashboard and your API Access Key will be provided. The Access Key unique, personal and is required to authenticate with the API. Keep it safe!
How do I call IpStack API endpoints?
Now all we need to do is to call the API base URL (http://api.ipStack.com/) via a HTTP POST method with no JSON request body needed and some few requested parameters added to the URL address depending on the ednpoint we choose to call. One can do that using REST Client libraries available on several programming languages.
ipStack offers three API endpoints to choose from:
- Standard Lookup: Look up the data behind an IP address.
- Bulk Lookup: Look up the data behind multiple IP addresses at once.
- Requester Lookup: Look up the data behind the IP address your API request is coming from.
Our demo will focus on the Standard Lookup Endpoint but all the others follow similar logic but with different parameters passed. For a complete and detailed list of endpoints and its parameters make sure you refer to IpStack Quickstart guide (https://ipStack.com/quickstart)
1 2 3 4 5 6 7 8 9 10 11 12 |
// Standard IP Lookup - look up the data behind an IP address http://api.ipstack.com/134.201.250.155 ? access_key = YOUR_ACCESS_KEY // optional parameters: & fields = ip,location,security & hostname = 1 & security = 1 & language = en & callback = MY_CALLBACK & output = json // more parameters available please refer to the API Documentation (<a href="https://ipStack.com/documentation">https://ipStack.com/documentation</a>) |
What does the IpStack API endpoint returns?
After the call the main results will be as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
{"ip":"35.208.128.246", "type":"ipv4", "continent_code":"NA", "continent_name":"North America", "country_code":"US", "country_name":"United States", "region_code":"IA", "region_name":"Iowa", "city":"Council Bluffs", "zip":"51501", "latitude":41.23295974731445, "longitude":-95.87735748291016, "location":{"geoname_id":4852832, "capital":"Washington D.C.", "languages":[{"code":"en", "name":"English", "native":"English"}], "country_flag":"https://assets.ipstack.com/flags/us.svg", "country_flag_emoji":" ", "country_flag_emoji_unicode":"U+1F1FA U+1F1F8", "calling_code":"1", "is_eu":false}} |
How do I connect my applications to IpStack API?
Once you have followed basic steps to set up the IpStack API we need to make sure we are able to connect and communicate with it before we start writing some code.
RAD Studio Delphi and C++Builder make it very easy to connect to APIs as you can you REST Debugger to automatically create the REST components and paste them into your app.
In Delphi all the job is done using 3 components tot make the API call. They are the TRESTClient, TRESTRequest, and TRESTResponse. Once you connect the REST Debugger successfully, copy and past the components you will notice that the API URL is set on the BaseURL of TRESTClient. On the TRESTRequest component you will see that the request type is set to rmPOST, the ContentType is set to ctAPPLICATION_JSON, and that it contains one request body for the POST.
Run your RAD Studio Delphi and on the main menu click on Tools > REST Debbuger. Configure the REST Debugger as follows marking the content-type as application/json, and adding the POST url, the JSON request body and the API key you created. Once you click the Send Request button you should see the JSON response, just like we demonstrated below.
The REST Debugger in action
![Captura de tela 20210810 102603 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Make An App To Convert IP to Geolocation screen capture 1](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/08/Captura-de-tela-2021-08-10-10.26.03-4000290.png?resize=750%2C529&ssl=1)
What can I use to build a Windows, macOS, Android or iOS application using the IpStack API?
Low code app development is increasingly popular today, and for good reason: it speaks to the demand for software paired with the difficulty of creating it. In many ways, Delphi led the low code revolution: RAD, where you build software by connecting components in a visual designer, is low code. The key with many low code solutions is extensibility: low code which cannot be extended to a traditionally coded app has a hard upper limit of functionality. If you’re a technology adviser, you do not want to recommend something which will limit your client or company. The best development solutions that target low code provide an avenue to grow and extend. In this too, Delphi excels.
Until recently in Delphi you still had to create a new project and create the forms or screens yourself – no matter how little code you may have to write to get powerful functionality when you did. This is something we’ve now addressed with an addon to 10.4.2. Run your RAD Studio Delphi and on the main menu click on Tools > GetIt Package Manager. In GetIt for 10.4.2 and later search for “Low Code App Wizard for FireMonkey”, our technology for cross-platform applications.
How to find the Low Code app wizard
![Captura de tela 20210714 1622112 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Make An App To Convert IP to Geolocation EULA screen](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/08/Captura-de-tela-2021-07-14-16.22.112-1188450.png?resize=750%2C422&ssl=1)
Now that you have installed Low Code App Wizard for FireMonkey and that you were able to sucessfully configure and test your API calls on the REST Debbuger, go back to the REST Debbuger and click the Copy Components button. Open RAD Studio Delphi and on the main menu click File > New> Multi-Device Application Delphi,
Select the Firemonkey Template App and follow some simples Wizard steps to create your Low Code application
![Captura de tela 20210714 162350 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Make An App To Convert IP to Geolocation Firemonkey template app](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/08/Captura-de-tela-2021-07-14-16.23.50-2910723.png?resize=750%2C422&ssl=1)
The result will be a fully functional cross-platform application, which already contains basic functionality that every mobile app will need — you can run and use the app as is — and hooks to build more, plus demonstrates application development best practices with a very clean extensible design to add more screens with low code, plus a set of tests to ensure your app remains high quality as you customise it.
The Low Code App is configurable, customizable and extendable
The app that Delphi builds for you is configurable through the wizard and you can choose any set of the following:
- User accounts: sign up, sign in, and a profile screen, with hooks for your own authentication
- Settings screen (where you can change the app theme (light or dark), plus some example settings you can reuse or extend)
- Contacts screen
- Terms of Use, About, and Privacy policy screens, easily configurable for your own data
- A database layer: using Interbase, store data in your app
- An example new screen you can use as a template or example for adding new functionality
As you can see the Wizard saves you a lot of time as it creates a complete project with many forms ready for you to implement the code. Open the recently created NewFormFrame and hit Ctrl+V in order to paste the components you copied from REST Debbuger. This acction will result in three components added to the the form RESTClient1, RESTRequest1 and RESTResponse1.
Now we will simply add very simple code to a TButton OnClick event to make sure every thing is configured correctly and voila! In five minutes we have made our very first call to IpStack API and we are able to receive JSON response for whatever parameter we want.
How to add some custom code to a Delphi Firemonkey FMX Low Code App
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
procedure TNewFormFrame.Button1Click(Sender: TObject); begin inherited; memo1.Lines.text := ''; RESTClient1.ResetToDefaults; RESTClient1.Accept := 'application/json, text/plain; q=0.9, text/html;q=0.8,'; RESTClient1.AcceptCharset := 'UTF-8, *;q=0.8'; RESTClient1.BaseURL := 'http://api.ipstack.com/' + edit2.text; RESTClient1.HandleRedirects := True; RESTClient1.RaiseExceptionOn500 := False; //here is were we pass the access_key and the additional parameters RESTRequest1.Resource := Format('?access_key=%s', [edit1.Text]); RESTRequest1.Client := RESTClient1; RESTRequest1.Response := RESTResponse1; RESTRequest1.SynchronizedEvents := False; RESTResponse1.ContentType := 'application/json'; RESTRequest1.Execute; memo1.Lines.text := RESTResponse1.Content; end; |
The sample application features a TEdit as a place to paste in the access key and additional TEdit for each parameter, a TMemo to display the JSON results of the REST API call. Now you have every thing you need in order to interate with the response data and make your application process the information and present it visually in the way it better suits your needs!
Here is the REST result of calling the API with Delphi
![Captura de tela 20210810 103135 | Embarcadero RAD Studio Delphi C++Builder Blogs How To Make An App To Convert IP to Geolocation REST response](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/08/Captura-de-tela-2021-08-10-10.31.35-1609042.png?resize=750%2C422&ssl=1)
In this blog post we’ve seen how to sign up for the IpStack API in order to get instant access to structured and readable IP to geolocation data. We’ve seen how to use the RAD Studio REST Debugger to connect to the endpoint and copy that code into a real application. And finally we’ve seen how easy and fast it is to use RAD Studio Delphi and the Low Code App Wizard for FireMonkey to create a real Multidevice (Windows/MacOS desktop or Android/iOS mobile) application which connects to the API and retrives the data ready for you to iterate!
You can download the full source code to the example app here: https://github.com/checkdigits/ipstackApp_MediaStackAPI_example
Next, learn about a service called IPApi, which is one of the market-leading solutions for all your IP Intelligence requirements in one place.
RAD Studio Delphi is the perfect tool to help you turn your great ideas into powerful apps. Why not download a free trial today?
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition
Hi,
Thanks for the article.
You wrote:
Run your RAD Studio Delphi and on the main menu click on Tools > REST Debugger. Configure the REST Debugger as follows marking the content-type as application/json, and adding the POST url, the JSON request body and the API key you created. Once you click the Send Request button you should see the JSON response, just like we demonstrated below.
Your REST Debugger Screenshot:
Method=GET
Content-Type=empty
Custom body=empty
What JSON request body? Is that Custom body? Where is that JSON request body and where to put it? Sorry but never used REST Debugger before. Or does your screenshot has nothing to do with your text?
Regards
Michael
I can see the confusion. If you download the example program I think it might be a little more clear. The content body, as shown in the screenshot, is actually empty. The content body is indeed the “custom body” field as you correctly surmised.