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

Visual FoxPro
LOCAL lnSuccess
LOCAL lcIpAddress
LOCAL loHttp
LOCAL loUrl
LOCAL loParameters
LOCAL loResponse
LOCAL lcRemaining
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"
*  The free endpoint is HTTP-only. This single lookup sends no API credentials.
loUrl = CreateObject('Chilkat.StringBuilder')
loUrl.Append("http://ip-api.com/json/")
loUrl.Append(loHttp.UrlEncode(lcIpAddress))
loParameters = CreateObject('Chilkat.JsonObject')
loParameters.UpdateString("fields","status,message,query,country,countryCode,regionName,city,lat,lon,timezone")
loResponse = CreateObject('Chilkat.HttpResponse')
lnSuccess = loHttp.HttpParams("GET",loUrl.GetAsString(),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 loUrl
    RELEASE loParameters
    RELEASE loResponse
    CANCEL
ENDIF

*  Save these values if the application will make further requests.
lcRemaining = loResponse.GetHeaderField("X-Rl")
IF (lcRemaining = "0") THEN
    ? "Rate limit reached. Wait " + loResponse.GetHeaderField("X-Ttl") + " seconds before the next request."
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("message") = 1) THEN
            ? loJson.StringOf("message")
        ENDIF

    ENDIF

    RELEASE loHttp
    RELEASE loUrl
    RELEASE loParameters
    RELEASE loResponse
    RELEASE loJson
    CANCEL
ENDIF

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

IF (loJson.StringOfEquals("status","success",1) <> 1) THEN
    ? "The service could not complete the lookup."
    IF (loJson.JsonTypeOf("message") = 1) THEN
        ? loJson.StringOf("message")
    ENDIF

    RELEASE loHttp
    RELEASE loUrl
    RELEASE loParameters
    RELEASE loResponse
    RELEASE loJson
    CANCEL
ENDIF

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

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

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

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

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

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

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

RELEASE loHttp
RELEASE loUrl
RELEASE loParameters
RELEASE loResponse
RELEASE loJson