Sample code for 30+ languages & platforms
PowerBuilder Requires Chilkat v11.0.0+

Look up an IP address with IP2Location.io

See more Geolocation Examples

Look up a public IPv4 or IPv6 address. The sample runs keyless. To use your account allowance, set apiKey and Chilkat will send it as a Bearer token. IP2Location.io recommends header authentication and also accepts its older key query parameter.

Keyless requests have a daily allowance. Registered plans have their own quotas and field availability. Check the provider’s current limits. An HTTP 401 with error 10000 can indicate an invalid key or an exhausted free allowance. Coordinates can default to the capital city when a city is unknown. The returned time_zone is a UTC offset.

IP geolocation estimates a network location. VPNs, mobile gateways, and anycast routing can affect the result. City and coordinate fields can be missing or approximate. The sample address 8.8.8.8 is a public DNS address used to demonstrate the request.

Service documentation

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
string ls_IpAddress
oleobject loo_Http
string ls_ApiKey
oleobject loo_Parameters
oleobject loo_Response
oleobject loo_Json
long li_Parsed

li_Success = 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
//  Use a public IP address. Provider results can change as their databases are updated.
ls_IpAddress = "8.8.8.8"
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Http.RequireSslCertVerify = 1
loo_Http.ConnectTimeout = 10
loo_Http.ReadTimeout = 30
loo_Http.Accept = "application/json"
//  Leave empty for keyless evaluation, or set the API key from your account.
ls_ApiKey = ""
if ls_ApiKey <> "" then
    loo_Http.AuthToken = ls_ApiKey
end if

loo_Parameters = create oleobject
li_rc = loo_Parameters.ConnectToNewObject("Chilkat.JsonObject")

loo_Parameters.UpdateString("ip",ls_IpAddress)
loo_Parameters.UpdateString("format","json")
loo_Response = create oleobject
li_rc = loo_Response.ConnectToNewObject("Chilkat.HttpResponse")

li_Success = loo_Http.HttpParams("GET","https://api.ip2location.io/",loo_Parameters,loo_Response)
if loo_Response.StatusCode = 0 then
    //  Request diagnostics can contain an API key. Report transport failure without logging credentials.
    Write-Debug "No HTTP response. Check connectivity, DNS, and TLS configuration."
    destroy loo_Http
    destroy loo_Parameters
    destroy loo_Response
    return
end if

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

li_Parsed = loo_Json.Load(loo_Response.BodyStr)
if (li_Success <> 1) OR (loo_Response.StatusCode < 200) OR (loo_Response.StatusCode >= 300) then
    Write-Debug "Lookup failed. HTTP " + string(loo_Response.StatusCode)
    if li_Parsed = 1 then
        if loo_Json.JsonTypeOf("error.error_message") = 1 then
            Write-Debug loo_Json.StringOf("error.error_message")
        end if

    end if

    destroy loo_Http
    destroy loo_Parameters
    destroy loo_Response
    destroy loo_Json
    return
end if

if li_Parsed <> 1 then
    Write-Debug "Expected a JSON object in the lookup response."
    destroy loo_Http
    destroy loo_Parameters
    destroy loo_Response
    destroy loo_Json
    return
end if

if loo_Json.JsonTypeOf("error") = 3 then
    Write-Debug "The service could not complete the lookup."
    if loo_Json.JsonTypeOf("error.error_message") = 1 then
        Write-Debug loo_Json.StringOf("error.error_message")
    end if

    destroy loo_Http
    destroy loo_Parameters
    destroy loo_Response
    destroy loo_Json
    return
end if

if (loo_Json.JsonTypeOf("ip") <> 1) OR (loo_Json.StringOfEquals("ip","",1) = 1) then
    Write-Debug "The response did not contain the expected IP address."
    destroy loo_Http
    destroy loo_Parameters
    destroy loo_Response
    destroy loo_Json
    return
end if

Write-Debug "IP address: " + loo_Json.StringOf("ip")
if (loo_Json.JsonTypeOf("country_name") = 1) AND (loo_Json.StringOfEquals("country_name","",1) <> 1) then
    Write-Debug "Country: " + loo_Json.StringOf("country_name")
else
    Write-Debug "Country: unavailable"
end if

if (loo_Json.JsonTypeOf("country_code") = 1) AND (loo_Json.StringOfEquals("country_code","",1) <> 1) then
    Write-Debug "Country code: " + loo_Json.StringOf("country_code")
else
    Write-Debug "Country code: unavailable"
end if

if (loo_Json.JsonTypeOf("region_name") = 1) AND (loo_Json.StringOfEquals("region_name","",1) <> 1) then
    Write-Debug "Region: " + loo_Json.StringOf("region_name")
else
    Write-Debug "Region: unavailable"
end if

if (loo_Json.JsonTypeOf("city_name") = 1) AND (loo_Json.StringOfEquals("city_name","",1) <> 1) then
    Write-Debug "City: " + loo_Json.StringOf("city_name")
else
    Write-Debug "City: unavailable"
end if

if (loo_Json.JsonTypeOf("time_zone") = 1) AND (loo_Json.StringOfEquals("time_zone","",1) <> 1) then
    Write-Debug "UTC offset: " + loo_Json.StringOf("time_zone")
else
    Write-Debug "UTC offset: unavailable"
end if

//  Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
if (loo_Json.JsonTypeOf("latitude") = 2) AND (loo_Json.JsonTypeOf("longitude") = 2) then
    Write-Debug "Coordinates: " + loo_Json.StringOf("latitude") + ", " + loo_Json.StringOf("longitude")
else
    Write-Debug "Coordinates: unavailable"
end if



destroy loo_Http
destroy loo_Parameters
destroy loo_Response
destroy loo_Json