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

Unicode C
#include <C_CkHttpW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    const wchar_t *ipAddress;
    HCkHttpW http;
    HCkStringBuilderW url;
    HCkHttpResponseW response;
    HCkJsonObjectW 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 = L"8.8.8.8";
    http = CkHttpW_Create();
    CkHttpW_putRequireSslCertVerify(http,TRUE);
    CkHttpW_putConnectTimeout(http,10);
    CkHttpW_putReadTimeout(http,30);
    CkHttpW_putAccept(http,L"application/json");
    url = CkStringBuilderW_Create();
    CkStringBuilderW_Append(url,L"https://api.db-ip.com/v2/free/");
    CkStringBuilderW_Append(url,CkHttpW_urlEncode(http,ipAddress));
    response = CkHttpResponseW_Create();
    success = CkHttpW_HttpNoBody(http,L"GET",CkStringBuilderW_getAsString(url),response);
    if (CkHttpResponseW_getStatusCode(response) == 0) {
        //  Request diagnostics can contain an API key. Report transport failure without logging credentials.
        wprintf(L"No HTTP response. Check connectivity, DNS, and TLS configuration.\n");
        CkHttpW_Dispose(http);
        CkStringBuilderW_Dispose(url);
        CkHttpResponseW_Dispose(response);
        return;
    }

    json = CkJsonObjectW_Create();
    parsed = CkJsonObjectW_Load(json,CkHttpResponseW_bodyStr(response));
    if ((success != TRUE) || (CkHttpResponseW_getStatusCode(response) < 200) || (CkHttpResponseW_getStatusCode(response) >= 300)) {
        wprintf(L"Lookup failed. HTTP %d\n",CkHttpResponseW_getStatusCode(response));
        if (parsed == TRUE) {
            if (CkJsonObjectW_JsonTypeOf(json,L"errorCode") == 1) {
                wprintf(L"%s\n",CkJsonObjectW_stringOf(json,L"errorCode"));
            }

            if (CkJsonObjectW_JsonTypeOf(json,L"error") == 1) {
                wprintf(L"%s\n",CkJsonObjectW_stringOf(json,L"error"));
            }

        }

        CkHttpW_Dispose(http);
        CkStringBuilderW_Dispose(url);
        CkHttpResponseW_Dispose(response);
        CkJsonObjectW_Dispose(json);
        return;
    }

    if (parsed != TRUE) {
        wprintf(L"Expected a JSON object in the lookup response.\n");
        CkHttpW_Dispose(http);
        CkStringBuilderW_Dispose(url);
        CkHttpResponseW_Dispose(response);
        CkJsonObjectW_Dispose(json);
        return;
    }

    if ((CkJsonObjectW_HasMember(json,L"errorCode") == TRUE) || (CkJsonObjectW_JsonTypeOf(json,L"error") == 1)) {
        wprintf(L"The service could not complete the lookup.\n");
        if (CkJsonObjectW_JsonTypeOf(json,L"errorCode") == 1) {
            wprintf(L"%s\n",CkJsonObjectW_stringOf(json,L"errorCode"));
        }

        if (CkJsonObjectW_JsonTypeOf(json,L"error") == 1) {
            wprintf(L"%s\n",CkJsonObjectW_stringOf(json,L"error"));
        }

        CkHttpW_Dispose(http);
        CkStringBuilderW_Dispose(url);
        CkHttpResponseW_Dispose(response);
        CkJsonObjectW_Dispose(json);
        return;
    }

    if ((CkJsonObjectW_JsonTypeOf(json,L"ipAddress") != 1) || (CkJsonObjectW_StringOfEquals(json,L"ipAddress",L"",TRUE) == TRUE)) {
        wprintf(L"The response did not contain the expected IP address.\n");
        CkHttpW_Dispose(http);
        CkStringBuilderW_Dispose(url);
        CkHttpResponseW_Dispose(response);
        CkJsonObjectW_Dispose(json);
        return;
    }

    wprintf(L"IP address: %s\n",CkJsonObjectW_stringOf(json,L"ipAddress"));
    if ((CkJsonObjectW_JsonTypeOf(json,L"countryName") == 1) && (CkJsonObjectW_StringOfEquals(json,L"countryName",L"",TRUE) != TRUE)) {
        wprintf(L"Country: %s\n",CkJsonObjectW_stringOf(json,L"countryName"));
    }
    else {
        wprintf(L"Country: unavailable\n");
    }

    if ((CkJsonObjectW_JsonTypeOf(json,L"countryCode") == 1) && (CkJsonObjectW_StringOfEquals(json,L"countryCode",L"",TRUE) != TRUE)) {
        wprintf(L"Country code: %s\n",CkJsonObjectW_stringOf(json,L"countryCode"));
    }
    else {
        wprintf(L"Country code: unavailable\n");
    }

    if ((CkJsonObjectW_JsonTypeOf(json,L"stateProv") == 1) && (CkJsonObjectW_StringOfEquals(json,L"stateProv",L"",TRUE) != TRUE)) {
        wprintf(L"State or province: %s\n",CkJsonObjectW_stringOf(json,L"stateProv"));
    }
    else {
        wprintf(L"State or province: unavailable\n");
    }

    if ((CkJsonObjectW_JsonTypeOf(json,L"city") == 1) && (CkJsonObjectW_StringOfEquals(json,L"city",L"",TRUE) != TRUE)) {
        wprintf(L"City: %s\n",CkJsonObjectW_stringOf(json,L"city"));
    }
    else {
        wprintf(L"City: unavailable\n");
    }



    CkHttpW_Dispose(http);
    CkStringBuilderW_Dispose(url);
    CkHttpResponseW_Dispose(response);
    CkJsonObjectW_Dispose(json);

    }