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

Look up an IP location with IPinfo Core

See more Geolocation Examples

Use an IPinfo token with Core, Plus, or Max access. The current api.ipinfo.io/lookup/ endpoint returns location fields inside geo and network fields inside as. Authenticate with a Bearer token.

IPinfo continues to support its older ipinfo.io API for existing integrations. This example uses the current Core response structure. The free Lite API uses /lite/ and a different, country-level response. Choose the endpoint and parser for your subscription.

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_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"
loo_Http.AuthToken = "IPINFO_TOKEN"
loo_Url = create oleobject
li_rc = loo_Url.ConnectToNewObject("Chilkat.StringBuilder")

loo_Url.Append("https://api.ipinfo.io/lookup/")
loo_Url.Append(loo_Http.UrlEncode(ls_IpAddress))
loo_Response = create oleobject
li_rc = loo_Response.ConnectToNewObject("Chilkat.HttpResponse")

li_Success = loo_Http.HttpNoBody("GET",loo_Url.GetAsString(),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_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.title") = 1 then
            Write-Debug loo_Json.StringOf("error.title")
        end if

        if loo_Json.JsonTypeOf("error.message") = 1 then
            Write-Debug loo_Json.StringOf("error.message")
        end if

    end if

    destroy loo_Http
    destroy loo_Url
    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_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.title") = 1 then
        Write-Debug loo_Json.StringOf("error.title")
    end if

    if loo_Json.JsonTypeOf("error.message") = 1 then
        Write-Debug loo_Json.StringOf("error.message")
    end if

    destroy loo_Http
    destroy loo_Url
    destroy loo_Response
    destroy loo_Json
    return
end if

if (loo_Json.JsonTypeOf("bogon") = 5) AND (loo_Json.BoolOf("bogon") = 1) then
    Write-Debug "This is a reserved or otherwise non-public IP address."
    destroy loo_Http
    destroy loo_Url
    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_Url
    destroy loo_Response
    destroy loo_Json
    return
end if

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

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

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

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

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

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

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

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



destroy loo_Http
destroy loo_Url
destroy loo_Response
destroy loo_Json