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

Look up an IP address with the DB-IP free API

See more Geolocation Examples

Use the DB-IP v2 free service to retrieve country, state or province, and city without an API key. This example uses HTTPS, which returned a successful free lookup during verification on November 20, 2026. The provider documents a limit of 500 daily requests for the free service. Paid services provide additional fields and quotas.

Check errorCode before reading the location. Free results have a smaller field set, and the application should handle missing location values.

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