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#
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.
string ipAddress = "8.8.8.8";
Chilkat.Http http = new Chilkat.Http();
http.RequireSslCertVerify = true;
http.ConnectTimeout = 10;
http.ReadTimeout = 30;
http.Accept = "application/json";
string apiKey = "YOUR_API_KEY";
Chilkat.StringBuilder url = new Chilkat.StringBuilder();
url.Append("https://api.ipdata.co/");
url.Append(http.UrlEncode(ipAddress));
Chilkat.JsonObject parameters = new Chilkat.JsonObject();
parameters.UpdateString("api-key",apiKey);
Chilkat.HttpResponse response = new Chilkat.HttpResponse();
success = http.HttpParams("GET",url.GetAsString(),parameters,response);
if (response.StatusCode == 0) {
    //  Request diagnostics can contain an API key. Report transport failure without logging credentials.
    Debug.WriteLine("No HTTP response. Check connectivity, DNS, and TLS configuration.");
    return;
}

Chilkat.JsonObject json = new Chilkat.JsonObject();
bool parsed = json.Load(response.BodyStr);
if ((success != true) || (response.StatusCode < 200) || (response.StatusCode >= 300)) {
    Debug.WriteLine("Lookup failed. HTTP " + Convert.ToString(response.StatusCode));
    if (parsed == true) {
        if (json.JsonTypeOf("message") == 1) {
            Debug.WriteLine(json.StringOf("message"));
        }

    }

    return;
}

if (parsed != true) {
    Debug.WriteLine("Expected a JSON object in the lookup response.");
    return;
}

if ((json.JsonTypeOf("ip") != 1) || (json.StringOfEquals("ip","",true) == true)) {
    Debug.WriteLine("The response did not contain the expected IP address.");
    return;
}

Debug.WriteLine("IP address: " + json.StringOf("ip"));
if ((json.JsonTypeOf("country_name") == 1) && (json.StringOfEquals("country_name","",true) != true)) {
    Debug.WriteLine("Country: " + json.StringOf("country_name"));
}
else {
    Debug.WriteLine("Country: unavailable");
}

if ((json.JsonTypeOf("country_code") == 1) && (json.StringOfEquals("country_code","",true) != true)) {
    Debug.WriteLine("Country code: " + json.StringOf("country_code"));
}
else {
    Debug.WriteLine("Country code: unavailable");
}

if ((json.JsonTypeOf("region") == 1) && (json.StringOfEquals("region","",true) != true)) {
    Debug.WriteLine("Region: " + json.StringOf("region"));
}
else {
    Debug.WriteLine("Region: unavailable");
}

if ((json.JsonTypeOf("city") == 1) && (json.StringOfEquals("city","",true) != true)) {
    Debug.WriteLine("City: " + json.StringOf("city"));
}
else {
    Debug.WriteLine("City: unavailable");
}

if ((json.JsonTypeOf("time_zone.name") == 1) && (json.StringOfEquals("time_zone.name","",true) != true)) {
    Debug.WriteLine("Time zone: " + json.StringOf("time_zone.name"));
}
else {
    Debug.WriteLine("Time zone: unavailable");
}

//  Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
if ((json.JsonTypeOf("latitude") == 2) && (json.JsonTypeOf("longitude") == 2)) {
    Debug.WriteLine("Coordinates: " + json.StringOf("latitude") + ", " + json.StringOf("longitude"));
}
else {
    Debug.WriteLine("Coordinates: unavailable");
}

if ((json.JsonTypeOf("asn.asn") == 1) && (json.StringOfEquals("asn.asn","",true) != true)) {
    Debug.WriteLine("ASN: " + json.StringOf("asn.asn"));
}
else {
    Debug.WriteLine("ASN: unavailable");
}

if ((json.JsonTypeOf("asn.name") == 1) && (json.StringOfEquals("asn.name","",true) != true)) {
    Debug.WriteLine("Network organization: " + json.StringOf("asn.name"));
}
else {
    Debug.WriteLine("Network organization: unavailable");
}