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

Geolocation of IP Address

See more REST Examples

Retrieve country, state or province, and city for a public IP address using the DB-IP v2 free JSON API. This replaces the retired freegeoip.net request and the historical two-service comparison. The sample sends one HTTPS request, checks transport and service errors, and reads the returned fields.

DB-IP documents 500 daily requests for its free service. Its free HTTPS endpoint returned a successful lookup during verification on November 20, 2026. For other providers and authenticated services, see the Geolocation examples.

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 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"
loUrl = CreateObject('Chilkat.StringBuilder')
loUrl.Append("https://api.db-ip.com/v2/free/")
loUrl.Append(loHttp.UrlEncode(lcIpAddress))
loResponse = CreateObject('Chilkat.HttpResponse')
lnSuccess = loHttp.HttpNoBody("GET",loUrl.GetAsString(),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 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("errorCode") = 1) THEN
            ? loJson.StringOf("errorCode")
        ENDIF

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

    ENDIF

    RELEASE loHttp
    RELEASE loUrl
    RELEASE loResponse
    RELEASE loJson
    CANCEL
ENDIF

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

IF ((loJson.HasMember("errorCode") = 1) OR (loJson.JsonTypeOf("error") = 1)) THEN
    ? "The service could not complete the lookup."
    IF (loJson.JsonTypeOf("errorCode") = 1) THEN
        ? loJson.StringOf("errorCode")
    ENDIF

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

    RELEASE loHttp
    RELEASE loUrl
    RELEASE loResponse
    RELEASE loJson
    CANCEL
ENDIF

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

? "IP address: " + loJson.StringOf("ipAddress")
IF ((loJson.JsonTypeOf("countryName") = 1) AND (loJson.StringOfEquals("countryName","",1) <> 1)) THEN
    ? "Country: " + loJson.StringOf("countryName")
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("stateProv") = 1) AND (loJson.StringOfEquals("stateProv","",1) <> 1)) THEN
    ? "State or province: " + loJson.StringOf("stateProv")
ELSE
    ? "State or province: unavailable"
ENDIF

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

RELEASE loHttp
RELEASE loUrl
RELEASE loResponse
RELEASE loJson