Module ipgeolocation
![](https://bcentral-packageicons.azureedge.net/images/ballerinax_ipgeolocation_1.1.0.png)
ballerinax/ipgeolocation Ballerina library
Overview
This is a generated connector for Abstract IP geolocation API version 1 OpenAPI Specification.
Abstract IP geolocation API allows developers to retrieve the region, country and city behind any IP worldwide. The API covers the geolocation of IPv4 and IPv6 addresses in 180+ countries worldwide. Extra information can be retrieved like the currency, flag or language associated to an IP.
Prerequisites
Before using this connector in your Ballerina application, complete the following:
-
Create an Abstract API account for IP Geolocation API. (If you already have one, you can use that.)
-
Obtain tokens - Follow this guide to find your private API key.
Quickstart
To use the IP Geolocation
connector in your Ballerina application, update the .bal file as follows:
Step 1: Import connector
Import the ballerinax/ipgeolocation module into the Ballerina project.
import ballerinax/ipgeolocation;
Step 2: Create a new connector instance
You can now make the connection configuration using API key and initialize the connector with it.
You can do this step in two ways. You can use any one of this.
-
Option 1 : Configure API key in ballerina file directly.
ApiKeysConfig apiKeyConfig = { apiKey: "<YOUR API KEY HERE>" }; ipgeolocation:Client ipGeolocationClient = check new Client(apiKeyConfig);
-
Option 2 : Configure API key in
Config.toml
file and configure it in ballerina file, using configurables.- Set up API key in
Config.toml
as shown below.
[apiKeyConfig.apiKeys] api_key = "<YOUR API KEY HERE>"
- Configure the client in ballerina file as shown below.
configurable ApiKeysConfig & readonly apiKeyConfig = ?; ipgeolocation:Client ipGeolocationClient = check new Client(apiKeyConfig);
Note: When you configure the API key value in the
Config.toml
file, useapi_key
as name and give the value of API key. (Eg: api_key = "YOUR API KEY") - Set up API key in
Step 3: Invoke connector operation
-
Now you can use the operations available within the connector. Note that they are in the form of remote operations. Following is an example on how to get geolocation using the connector.
public function main() { string ipAddress = "<IP ADDRESS TO GET GEOLOCATION>" Geolocation|error geolocation = ipGeolocationClient->getGeolocation(ipAddress); // Your logic here }
-
Use
bal run
command to compile and run the Ballerina program.
Note : Here
ipAddress
parameter is the IP address to geolocate. Both IPv4 and IPv6 addresses are supported. Note that if this parameter is left blank, the service will geolocate the IP address from which the request was made.