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

Look up an IP address with ipstack over HTTPS

See more Geolocation Examples

Set your ipstack access key and request one public IP address over HTTPS. HTTP status errors and the API’s success=false error object are checked separately. Account quotas and optional modules depend on the subscription. The example displays core geolocation fields and handles null 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 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 accessKey = "YOUR_ACCESS_KEY";
Chilkat.StringBuilder url = new Chilkat.StringBuilder();
url.Append("https://api.ipstack.com/");
url.Append(http.UrlEncode(ipAddress));
Chilkat.JsonObject parameters = new Chilkat.JsonObject();
parameters.UpdateString("access_key",accessKey);
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("error.type") == 1) {
            Debug.WriteLine(json.StringOf("error.type"));
        }

        if (json.JsonTypeOf("error.info") == 1) {
            Debug.WriteLine(json.StringOf("error.info"));
        }

    }

    return;
}

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

if (((json.JsonTypeOf("success") == 5) && (json.BoolOf("success") != true)) || (json.JsonTypeOf("error") == 3)) {
    Debug.WriteLine("The service could not complete the lookup.");
    if (json.JsonTypeOf("error.type") == 1) {
        Debug.WriteLine(json.StringOf("error.type"));
    }

    if (json.JsonTypeOf("error.info") == 1) {
        Debug.WriteLine(json.StringOf("error.info"));
    }

    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_name") == 1) && (json.StringOfEquals("region_name","",true) != true)) {
    Debug.WriteLine("Region: " + json.StringOf("region_name"));
}
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("zip") == 1) && (json.StringOfEquals("zip","",true) != true)) {
    Debug.WriteLine("Postal code: " + json.StringOf("zip"));
}
else {
    Debug.WriteLine("Postal code: 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");
}