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

Look up an IP address with ipdata

See more Geolocation Examples

Use an ipdata API key to request a public IPv4 or IPv6 address. The key is encoded as the api-key query parameter. The example displays location fields and the optional ASN object using its current nested structure. Data access and quotas depend on the account plan.

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 <CkHttp.h>
#include <CkStringBuilder.h>
#include <CkJsonObject.h>
#include <CkHttpResponse.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    //  Use a public IP address. Provider results can change as their databases are updated.
    const char *ipAddress = "8.8.8.8";
    CkHttp http;
    http.put_RequireSslCertVerify(true);
    http.put_ConnectTimeout(10);
    http.put_ReadTimeout(30);
    http.put_Accept("application/json");
    const char *apiKey = "YOUR_API_KEY";
    CkStringBuilder url;
    url.Append("https://api.ipdata.co/");
    url.Append(http.urlEncode(ipAddress));
    CkJsonObject parameters;
    parameters.UpdateString("api-key",apiKey);
    CkHttpResponse response;
    success = http.HttpParams("GET",url.getAsString(),parameters,response);
    if (response.get_StatusCode() == 0) {
        //  Request diagnostics can contain an API key. Report transport failure without logging credentials.
        std::cout << "No HTTP response. Check connectivity, DNS, and TLS configuration." << "\r\n";
        return;
    }

    CkJsonObject json;
    bool parsed = json.Load(response.bodyStr());
    if ((success != true) || (response.get_StatusCode() < 200) || (response.get_StatusCode() >= 300)) {
        std::cout << "Lookup failed. HTTP " << response.get_StatusCode() << "\r\n";
        if (parsed == true) {
            if (json.JsonTypeOf("message") == 1) {
                std::cout << json.stringOf("message") << "\r\n";
            }

        }

        return;
    }

    if (parsed != true) {
        std::cout << "Expected a JSON object in the lookup response." << "\r\n";
        return;
    }

    if ((json.JsonTypeOf("ip") != 1) || (json.StringOfEquals("ip","",true) == true)) {
        std::cout << "The response did not contain the expected IP address." << "\r\n";
        return;
    }

    std::cout << "IP address: " << json.stringOf("ip") << "\r\n";
    if ((json.JsonTypeOf("country_name") == 1) && (json.StringOfEquals("country_name","",true) != true)) {
        std::cout << "Country: " << json.stringOf("country_name") << "\r\n";
    }
    else {
        std::cout << "Country: unavailable" << "\r\n";
    }

    if ((json.JsonTypeOf("country_code") == 1) && (json.StringOfEquals("country_code","",true) != true)) {
        std::cout << "Country code: " << json.stringOf("country_code") << "\r\n";
    }
    else {
        std::cout << "Country code: unavailable" << "\r\n";
    }

    if ((json.JsonTypeOf("region") == 1) && (json.StringOfEquals("region","",true) != true)) {
        std::cout << "Region: " << json.stringOf("region") << "\r\n";
    }
    else {
        std::cout << "Region: unavailable" << "\r\n";
    }

    if ((json.JsonTypeOf("city") == 1) && (json.StringOfEquals("city","",true) != true)) {
        std::cout << "City: " << json.stringOf("city") << "\r\n";
    }
    else {
        std::cout << "City: unavailable" << "\r\n";
    }

    if ((json.JsonTypeOf("time_zone.name") == 1) && (json.StringOfEquals("time_zone.name","",true) != true)) {
        std::cout << "Time zone: " << json.stringOf("time_zone.name") << "\r\n";
    }
    else {
        std::cout << "Time zone: unavailable" << "\r\n";
    }

    //  Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
    if ((json.JsonTypeOf("latitude") == 2) && (json.JsonTypeOf("longitude") == 2)) {
        std::cout << "Coordinates: " << json.stringOf("latitude") << ", " << json.stringOf("longitude") << "\r\n";
    }
    else {
        std::cout << "Coordinates: unavailable" << "\r\n";
    }

    if ((json.JsonTypeOf("asn.asn") == 1) && (json.StringOfEquals("asn.asn","",true) != true)) {
        std::cout << "ASN: " << json.stringOf("asn.asn") << "\r\n";
    }
    else {
        std::cout << "ASN: unavailable" << "\r\n";
    }

    if ((json.JsonTypeOf("asn.name") == 1) && (json.StringOfEquals("asn.name","",true) != true)) {
        std::cout << "Network organization: " << json.stringOf("asn.name") << "\r\n";
    }
    else {
        std::cout << "Network organization: unavailable" << "\r\n";
    }
    }