Sample code for 30+ languages & platforms
Visual FoxPro 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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL lcIpAddress
LOCAL loHttp
LOCAL loParameters
LOCAL loResponse
LOCAL loJson
LOCAL lnParsed

lnSuccess = 0

*  Assumes Chilkat is unlocked. See Global Unlock Sample.
*  Use a public IP address. Provider results can change as their databases are updated.
lcIpAddress = "8.8.8.8"
loHttp = CreateObject('Chilkat.Http')
loHttp.RequireSslCertVerify = 1
loHttp.ConnectTimeout = 10
loHttp.ReadTimeout = 30
loHttp.Accept = "application/json"
loParameters = CreateObject('Chilkat.JsonObject')
loParameters.UpdateString("apiKey","YOUR_API_KEY")
loParameters.UpdateString("ipAddress",lcIpAddress)
loResponse = CreateObject('Chilkat.HttpResponse')
lnSuccess = loHttp.HttpParams("GET","https://geo.ipify.org/api/v2/country,city",loParameters,loResponse)
IF (loResponse.StatusCode = 0) THEN
    *  Request diagnostics can contain an API key. Report transport failure without logging credentials.
    ? "No HTTP response. Check connectivity, DNS, and TLS configuration."
    RELEASE loHttp
    RELEASE loParameters
    RELEASE loResponse
    CANCEL
ENDIF

loJson = CreateObject('Chilkat.JsonObject')
lnParsed = loJson.Load(loResponse.BodyStr)
IF ((lnSuccess <> 1) OR (loResponse.StatusCode < 200) OR (loResponse.StatusCode >= 300)) THEN
    ? "Lookup failed. HTTP " + STR(loResponse.StatusCode)
    IF (lnParsed = 1) THEN
        IF (loJson.JsonTypeOf("messages") = 1) THEN
            ? loJson.StringOf("messages")
        ENDIF

        IF (loJson.JsonTypeOf("message") = 1) THEN
            ? loJson.StringOf("message")
        ENDIF

    ENDIF

    RELEASE loHttp
    RELEASE loParameters
    RELEASE loResponse
    RELEASE loJson
    CANCEL
ENDIF

IF (lnParsed <> 1) THEN
    ? "Expected a JSON object in the lookup response."
    RELEASE loHttp
    RELEASE loParameters
    RELEASE loResponse
    RELEASE loJson
    CANCEL
ENDIF

IF ((loJson.JsonTypeOf("ip") <> 1) OR (loJson.StringOfEquals("ip","",1) = 1)) THEN
    ? "The response did not contain the expected IP address."
    RELEASE loHttp
    RELEASE loParameters
    RELEASE loResponse
    RELEASE loJson
    CANCEL
ENDIF

? "IP address: " + loJson.StringOf("ip")
IF ((loJson.JsonTypeOf("location.country") = 1) AND (loJson.StringOfEquals("location.country","",1) <> 1)) THEN
    ? "Country code: " + loJson.StringOf("location.country")
ELSE
    ? "Country code: unavailable"
ENDIF

IF ((loJson.JsonTypeOf("location.region") = 1) AND (loJson.StringOfEquals("location.region","",1) <> 1)) THEN
    ? "Region: " + loJson.StringOf("location.region")
ELSE
    ? "Region: unavailable"
ENDIF

IF ((loJson.JsonTypeOf("location.city") = 1) AND (loJson.StringOfEquals("location.city","",1) <> 1)) THEN
    ? "City: " + loJson.StringOf("location.city")
ELSE
    ? "City: unavailable"
ENDIF

IF ((loJson.JsonTypeOf("location.timezone") = 1) AND (loJson.StringOfEquals("location.timezone","",1) <> 1)) THEN
    ? "UTC offset: " + loJson.StringOf("location.timezone")
ELSE
    ? "UTC offset: unavailable"
ENDIF

*  Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
IF ((loJson.JsonTypeOf("location.lat") = 2) AND (loJson.JsonTypeOf("location.lng") = 2)) THEN
    ? "Coordinates: " + loJson.StringOf("location.lat") + ", " + loJson.StringOf("location.lng")
ELSE
    ? "Coordinates: unavailable"
ENDIF

RELEASE loHttp
RELEASE loParameters
RELEASE loResponse
RELEASE loJson