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

C
#include <C_CkHttp.h>
#include <C_CkStringBuilder.h>
#include <C_CkHttpResponse.h>
#include <C_CkJsonObject.h>

void ChilkatSample(void)
    {
    BOOL success;
    const char *ipAddress;
    HCkHttp http;
    HCkStringBuilder url;
    HCkHttpResponse response;
    HCkJsonObject json;
    BOOL parsed;

    success = FALSE;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    //  Use a public IP address. Provider results can change as their databases are updated.
    ipAddress = "8.8.8.8";
    http = CkHttp_Create();
    CkHttp_putRequireSslCertVerify(http,TRUE);
    CkHttp_putConnectTimeout(http,10);
    CkHttp_putReadTimeout(http,30);
    CkHttp_putAccept(http,"application/json");
    url = CkStringBuilder_Create();
    CkStringBuilder_Append(url,"https://api.db-ip.com/v2/free/");
    CkStringBuilder_Append(url,CkHttp_urlEncode(http,ipAddress));
    response = CkHttpResponse_Create();
    success = CkHttp_HttpNoBody(http,"GET",CkStringBuilder_getAsString(url),response);
    if (CkHttpResponse_getStatusCode(response) == 0) {
        //  Request diagnostics can contain an API key. Report transport failure without logging credentials.
        printf("No HTTP response. Check connectivity, DNS, and TLS configuration.\n");
        CkHttp_Dispose(http);
        CkStringBuilder_Dispose(url);
        CkHttpResponse_Dispose(response);
        return;
    }

    json = CkJsonObject_Create();
    parsed = CkJsonObject_Load(json,CkHttpResponse_bodyStr(response));
    if ((success != TRUE) || (CkHttpResponse_getStatusCode(response) < 200) || (CkHttpResponse_getStatusCode(response) >= 300)) {
        printf("Lookup failed. HTTP %d\n",CkHttpResponse_getStatusCode(response));
        if (parsed == TRUE) {
            if (CkJsonObject_JsonTypeOf(json,"errorCode") == 1) {
                printf("%s\n",CkJsonObject_stringOf(json,"errorCode"));
            }

            if (CkJsonObject_JsonTypeOf(json,"error") == 1) {
                printf("%s\n",CkJsonObject_stringOf(json,"error"));
            }

        }

        CkHttp_Dispose(http);
        CkStringBuilder_Dispose(url);
        CkHttpResponse_Dispose(response);
        CkJsonObject_Dispose(json);
        return;
    }

    if (parsed != TRUE) {
        printf("Expected a JSON object in the lookup response.\n");
        CkHttp_Dispose(http);
        CkStringBuilder_Dispose(url);
        CkHttpResponse_Dispose(response);
        CkJsonObject_Dispose(json);
        return;
    }

    if ((CkJsonObject_HasMember(json,"errorCode") == TRUE) || (CkJsonObject_JsonTypeOf(json,"error") == 1)) {
        printf("The service could not complete the lookup.\n");
        if (CkJsonObject_JsonTypeOf(json,"errorCode") == 1) {
            printf("%s\n",CkJsonObject_stringOf(json,"errorCode"));
        }

        if (CkJsonObject_JsonTypeOf(json,"error") == 1) {
            printf("%s\n",CkJsonObject_stringOf(json,"error"));
        }

        CkHttp_Dispose(http);
        CkStringBuilder_Dispose(url);
        CkHttpResponse_Dispose(response);
        CkJsonObject_Dispose(json);
        return;
    }

    if ((CkJsonObject_JsonTypeOf(json,"ipAddress") != 1) || (CkJsonObject_StringOfEquals(json,"ipAddress","",TRUE) == TRUE)) {
        printf("The response did not contain the expected IP address.\n");
        CkHttp_Dispose(http);
        CkStringBuilder_Dispose(url);
        CkHttpResponse_Dispose(response);
        CkJsonObject_Dispose(json);
        return;
    }

    printf("IP address: %s\n",CkJsonObject_stringOf(json,"ipAddress"));
    if ((CkJsonObject_JsonTypeOf(json,"countryName") == 1) && (CkJsonObject_StringOfEquals(json,"countryName","",TRUE) != TRUE)) {
        printf("Country: %s\n",CkJsonObject_stringOf(json,"countryName"));
    }
    else {
        printf("Country: unavailable\n");
    }

    if ((CkJsonObject_JsonTypeOf(json,"countryCode") == 1) && (CkJsonObject_StringOfEquals(json,"countryCode","",TRUE) != TRUE)) {
        printf("Country code: %s\n",CkJsonObject_stringOf(json,"countryCode"));
    }
    else {
        printf("Country code: unavailable\n");
    }

    if ((CkJsonObject_JsonTypeOf(json,"stateProv") == 1) && (CkJsonObject_StringOfEquals(json,"stateProv","",TRUE) != TRUE)) {
        printf("State or province: %s\n",CkJsonObject_stringOf(json,"stateProv"));
    }
    else {
        printf("State or province: unavailable\n");
    }

    if ((CkJsonObject_JsonTypeOf(json,"city") == 1) && (CkJsonObject_StringOfEquals(json,"city","",TRUE) != TRUE)) {
        printf("City: %s\n",CkJsonObject_stringOf(json,"city"));
    }
    else {
        printf("City: unavailable\n");
    }



    CkHttp_Dispose(http);
    CkStringBuilder_Dispose(url);
    CkHttpResponse_Dispose(response);
    CkJsonObject_Dispose(json);

    }