Have you ever wanted your apps to have the power to examine an IP address and then find out everything you possibly could about it, such as what time zone is local to the IP, what currency they use, or what internet service provider is used? This capability is called IP address intelligence and it can add a powerfully rich package of knowledge to your applications allowing you to customize how you present information to your users. In this post, you will learn how to quickly and easily integrate IP address intelligence into your IDE software and mobile applications. We are going to use a service called IPApi, which is one of the market-leading solutions for all your IP Intelligence requirements in one place.
Table of Contents
Why would we want to find out information about an IP Address?
The IP address is often tied to an individual user. If you know information about that IP address you can customize some of the information, services or even the user interface to better meet the needs of your users. For example, if you find out that the current time for the IP address is night time then you might want to offer different services to those you offer during their daytime hours. Knowing the local currency in use where the IP address is hosted allows you can offer that as a default for purchases.
Lastly, you may want or need to restrict some of the app’s features if an IP address is located in a different country, possibly for security reasons or to comply with local laws or copyright compliance. With proper IP address intelligence information all of this should be possible.
What is the IPApi service?
The IPApi service is a powerful IP lookup RESTful web service that helps to find a different set of information using only the IP address. You can do more fascinating things with IP addresses. For instance:
- Content Personalization based on location
- Time Zone Lookup
- Language Redirection based on geolocation
- Currency and Fraud detection
- and more
The IPApi platform is platform agnostic and offers accurate information. IPApi say their RESTful web service is “trusted by 30,000+ businesses worldwide”.
How do I start getting information about an IP address in my application?
Head over to the IPApi official website to sign up. There is a Free plan to test it out.
How do I use IPApi to get information about an IP address?
After you sign up, you will have an API access key. And you can utilize that API access key to send requests to the endpoint.
Here is an example of calling to the endpoint:
And depending on your subscription plan, you get different sets of data in your response:
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
{ "ip": "161.185.160.93", "type": "ipv4", "continent_code": "NA", "continent_name": "North America", "country_code": "US", "country_name": "United States", "region_code": "NY", "region_name": "New York", "city": "Brooklyn", "zip": "11238", "latitude": 40.676, "longitude": -73.9629, "location": { "geoname_id": 5110302, "capital": "Washington D.C.", "languages": [ { "code": "en", "name": "English", "native": "English" } ], "country_flag": "http://assets.ipapi.com/flags/us.svg", "country_flag_emoji": "🇺🇸", "country_flag_emoji_unicode": "U+1F1FA U+1F1F8", "calling_code": "1", "is_eu": false }, "time_zone": { "id": "America/New_York", "current_time": "2018-09-24T05:07:10-04:00", "gmt_offset": -14400, "code": "EDT", "is_daylight_saving": true }, "currency": { "code": "USD", "name": "US Dollar", "plural": "US dollars", "symbol": "$", "symbol_native": "$" }, "connection": { "asn": 22252, "isp": "The City of New York" } } |
Furthermore, you can have your IP lookup data in two different representations:
- JSON
- XML
Here is a sample request for information about a particular IP address
While sending a request to the base API endpoint you need to specify output type XML or JSON. And here is a sample request for that:
Besides, you can customize response fields. Since the response size is big or you need only location data, for instance. So at this point, you can specify response fields also:
How do I connect my applications to the IPApi API so I can get information about IP addresses?
RAD Studio comes with its handy tools for different jobs. For instance, REST Debugger is one of the most popular tools when you work with RESTful web services. You can customize your API request methods, and based on the response from the endpoint, you can click the “Copy Components” button, which gives an already configured set of REST Client components. Then you can easily add them to your Delphi or C++ Builder project.
These REST Client components are TRESTClient, TRESTResponse, and TRESTRequest
How do I build an app which runs on Windows, macOS, Android & iOS and works with the IPApi API?
FireMonkey App Low Code Wizard is the new tool in the RAD Studio toolset, and we will use it in this tutorial.
FireMonkey Low Code Wizard makes the development process several times faster. You can create a fully functional Delphi cross-platform application in seconds. With the low code wizard, you will be asked what kind of features you want. For instance, Settings, Contacts, Profile, Home and Menu windows, and database layer for database needs and more. Moreover, you can customize your app as much as you can. You can read more about what the Low Code Wizard can do for you by clicking here: https://blogs.embarcadero.com/?s=low+code+wizard
How do you start Low Code App development in the RAD Studio Delphi IDE?
Download the newest community edition of Delphi IDE from here. And run the IDE, then go to the Tools menu and open GetIt Package Manager to download FireMonkey App Low-Code Wizard (https://getitnow.embarcadero.com/firemonkey-app-low-code-wizard/)
After installation is complete, you need to go on the menu: File->New->Other and select Multi-Device->Firemonkey Template App. And give the project name and choose the screens that you need.
The initial Low Code App Wizard screen
How can we connect RESTful web service and parse JSON in Delphi?
Well, we’ve already connected to the endpoint using the REST Debugger and copied REST components from it. Now we need to just design our blank new form, and here is our UI structure:
After you pasted the REST components that you copied from the REST Debugger, select the RESTRequest1 and open its Params property then clear the access key parameter. Because in this demo, we will customize and parse some exact values.
At this current state, you can call the endpoint using this one line of code:
1 |
RESTRequest1.Execute |
Here is the final step to getting complete information about an IP address using Delphi
Create an OnClick event on the Check IP button and paste this code:
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 26 |
procedure TNewFormFrame.BtnCheckIPClick(Sender: TObject); begin inherited; RESTClient1.ResetToDefaults; RESTClient1.Accept := 'application/json'; RESTClient1.AcceptCharset := 'UTF-8, *;q=0.8'; RESTClient1.BaseURL := Format('http://api.ipapi.com/api/%s', [EdtIP.Text]); RESTResponse1.ContentType := 'application/json'; RESTRequest1.Resource := Format('?access_key=%s', [EdtAccKey.Text]); RESTRequest1.Execute; var JSONValue := TJSONObject.ParseJSONValue(RESTResponse1.Content); try if JSONValue is TJSONObject then begin LblLatitude.Text := 'Lat: ' + JSONValue.GetValue<String>('latitude'); LblLongitude.Text := 'Lon: ' + JSONValue.GetValue<String>('longitude'); LblIPLocation.Text := 'Location: ' + JSONValue.GetValue<String>('country_name'); LblCity.Text := 'City: ' + JSONValue.GetValue<String>('city'); LblZipCode.Text := 'Zip Code: ' + JSONValue.GetValue<String>('zip'); end; finally JSONValue.Free; end; end; |
This is the finished Delphi IP address information mobile app example running
As you can see, creating a cross-platform application and integrating with RESTful web services is easy with Delphi FireMonkey!
Head over and download the full source code for the IPApi with Delphi demo here: https://github.com/checkdigits/IP-Intelligence-in-Delphi-using-ipapi_example
RAD Studio Delphi can easily make your great ideas become real applications in a very short space of time. Why not download a trial today and see for yourself?
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition
Informative post! This is a great share thank you
Hey! I’m happy that you liked it. Be sure to check weekly for more interesting demonstrations 👍