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

Look up an IP location with the ipify v2 API

See more Geolocation Examples

Use the ipify IP Geolocation v2 country,city endpoint with an API key. This endpoint returns country and city details, including coordinates when available, and currently costs two credits per request. The country endpoint has a smaller result and costs one credit.

The example explicitly supplies the target IPv4 or IPv6 address and URL-encodes the query parameters. location.timezone is a UTC offset. This replaces the older v1 request used by this example.

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    String sIpAddress
    Handle hoHttp
    Variant vParameters
    Handle hoParameters
    Variant vResponse
    Handle hoResponse
    Handle hoJson
    Boolean iParsed
    String sTemp1
    String sTemp2
    Integer iTemp1
    Integer iTemp2
    Boolean bTemp1

    Move False To iSuccess

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    //  Use a public IP address. Provider results can change as their databases are updated.
    Move "8.8.8.8" To sIpAddress
    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End
    Set ComRequireSslCertVerify Of hoHttp To True
    Set ComConnectTimeout Of hoHttp To 10
    Set ComReadTimeout Of hoHttp To 30
    Set ComAccept Of hoHttp To "application/json"
    Get Create (RefClass(cComChilkatJsonObject)) To hoParameters
    If (Not(IsComObjectCreated(hoParameters))) Begin
        Send CreateComObject of hoParameters
    End
    Get ComUpdateString Of hoParameters "apiKey" "YOUR_API_KEY" To iSuccess
    Get ComUpdateString Of hoParameters "ipAddress" sIpAddress To iSuccess
    Get Create (RefClass(cComChilkatHttpResponse)) To hoResponse
    If (Not(IsComObjectCreated(hoResponse))) Begin
        Send CreateComObject of hoResponse
    End
    Get pvComObject of hoParameters to vParameters
    Get pvComObject of hoResponse to vResponse
    Get ComHttpParams Of hoHttp "GET" "https://geo.ipify.org/api/v2/country,city" vParameters vResponse To iSuccess
    Get ComStatusCode Of hoResponse To iTemp1
    If (iTemp1 = 0) Begin
        //  Request diagnostics can contain an API key. Report transport failure without logging credentials.
        Showln "No HTTP response. Check connectivity, DNS, and TLS configuration."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get ComBodyStr Of hoResponse To sTemp1
    Get ComLoad Of hoJson sTemp1 To iParsed
    Get ComStatusCode Of hoResponse To iTemp1
    Get ComStatusCode Of hoResponse To iTemp2
    If ((iSuccess <> True) Or (iTemp1 < 200) Or (iTemp2 >= 300)) Begin
        Get ComStatusCode Of hoResponse To iTemp1
        Showln "Lookup failed. HTTP " iTemp1
        If (iParsed = True) Begin
            Get ComJsonTypeOf Of hoJson "messages" To iTemp1
            If (iTemp1 = 1) Begin
                Get ComStringOf Of hoJson "messages" To sTemp1
                Showln sTemp1
            End

            Get ComJsonTypeOf Of hoJson "message" To iTemp1
            If (iTemp1 = 1) Begin
                Get ComStringOf Of hoJson "message" To sTemp1
                Showln sTemp1
            End

        End

        Procedure_Return
    End

    If (iParsed <> True) Begin
        Showln "Expected a JSON object in the lookup response."
        Procedure_Return
    End

    Get ComJsonTypeOf Of hoJson "ip" To iTemp1
    Get ComStringOfEquals Of hoJson "ip" "" True To bTemp1
    If ((iTemp1 <> 1) Or (bTemp1 = True)) Begin
        Showln "The response did not contain the expected IP address."
        Procedure_Return
    End

    Get ComStringOf Of hoJson "ip" To sTemp1
    Showln "IP address: " sTemp1
    Get ComJsonTypeOf Of hoJson "location.country" To iTemp1
    Get ComStringOfEquals Of hoJson "location.country" "" True To bTemp1
    If ((iTemp1 = 1) And (bTemp1 <> True)) Begin
        Get ComStringOf Of hoJson "location.country" To sTemp1
        Showln "Country code: " sTemp1
    End
    Else Begin
        Showln "Country code: unavailable"
    End

    Get ComJsonTypeOf Of hoJson "location.region" To iTemp1
    Get ComStringOfEquals Of hoJson "location.region" "" True To bTemp1
    If ((iTemp1 = 1) And (bTemp1 <> True)) Begin
        Get ComStringOf Of hoJson "location.region" To sTemp1
        Showln "Region: " sTemp1
    End
    Else Begin
        Showln "Region: unavailable"
    End

    Get ComJsonTypeOf Of hoJson "location.city" To iTemp1
    Get ComStringOfEquals Of hoJson "location.city" "" True To bTemp1
    If ((iTemp1 = 1) And (bTemp1 <> True)) Begin
        Get ComStringOf Of hoJson "location.city" To sTemp1
        Showln "City: " sTemp1
    End
    Else Begin
        Showln "City: unavailable"
    End

    Get ComJsonTypeOf Of hoJson "location.timezone" To iTemp1
    Get ComStringOfEquals Of hoJson "location.timezone" "" True To bTemp1
    If ((iTemp1 = 1) And (bTemp1 <> True)) Begin
        Get ComStringOf Of hoJson "location.timezone" To sTemp1
        Showln "UTC offset: " sTemp1
    End
    Else Begin
        Showln "UTC offset: unavailable"
    End

    //  Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
    Get ComJsonTypeOf Of hoJson "location.lat" To iTemp1
    Get ComJsonTypeOf Of hoJson "location.lng" To iTemp2
    If ((iTemp1 = 2) And (iTemp2 = 2)) Begin
        Get ComStringOf Of hoJson "location.lat" To sTemp1
        Get ComStringOf Of hoJson "location.lng" To sTemp2
        Showln "Coordinates: " sTemp1 ", " sTemp2
    End
    Else Begin
        Showln "Coordinates: unavailable"
    End



End_Procedure