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

Look up an IP address with the ip-api free endpoint

See more Geolocation Examples

This example uses the free, keyless ip-api endpoint for non-commercial use. The free service supports HTTP and is limited to 45 requests per minute per source IP. Its Pro service provides HTTPS and commercial use. ip-api Pro.

Always include status and message when selecting fields. Invalid or private addresses can produce status=fail inside a JSON response. Retain the X-Rl and X-Ttl response values between calls, and pause further requests when X-Rl reaches zero.

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
oleobject loo_Url
oleobject loo_Parameters
oleobject loo_Response
string ls_Remaining
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"
//  The free endpoint is HTTP-only. This single lookup sends no API credentials.
loo_Url = create oleobject
li_rc = loo_Url.ConnectToNewObject("Chilkat.StringBuilder")

loo_Url.Append("http://ip-api.com/json/")
loo_Url.Append(loo_Http.UrlEncode(ls_IpAddress))
loo_Parameters = create oleobject
li_rc = loo_Parameters.ConnectToNewObject("Chilkat.JsonObject")

loo_Parameters.UpdateString("fields","status,message,query,country,countryCode,regionName,city,lat,lon,timezone")
loo_Response = create oleobject
li_rc = loo_Response.ConnectToNewObject("Chilkat.HttpResponse")

li_Success = loo_Http.HttpParams("GET",loo_Url.GetAsString(),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_Url
    destroy loo_Parameters
    destroy loo_Response
    return
end if

//  Save these values if the application will make further requests.
ls_Remaining = loo_Response.GetHeaderField("X-Rl")
if ls_Remaining = "0" then
    Write-Debug "Rate limit reached. Wait " + loo_Response.GetHeaderField("X-Ttl") + " seconds before the next request."
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("message") = 1 then
            Write-Debug loo_Json.StringOf("message")
        end if

    end if

    destroy loo_Http
    destroy loo_Url
    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_Url
    destroy loo_Parameters
    destroy loo_Response
    destroy loo_Json
    return
end if

if loo_Json.StringOfEquals("status","success",1) <> 1 then
    Write-Debug "The service could not complete the lookup."
    if loo_Json.JsonTypeOf("message") = 1 then
        Write-Debug loo_Json.StringOf("message")
    end if

    destroy loo_Http
    destroy loo_Url
    destroy loo_Parameters
    destroy loo_Response
    destroy loo_Json
    return
end if

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

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

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

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

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

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

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



destroy loo_Http
destroy loo_Url
destroy loo_Parameters
destroy loo_Response
destroy loo_Json