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

Look up an IP country with MaxMind GeoIP

See more Geolocation Examples

Call the GeoIP Country web service using HTTP Basic authentication. Set the username to your numeric MaxMind account ID and the password to your license key. The account needs access to the web service, such as a trial or service credit. Both IPv4 and IPv6 are supported.

country describes the geolocated country. registered_country describes where the network is registered. The Country service returns country-level data. City-level lookups use the separately entitled /geoip/v2.1/city/ service. A GeoLite web-service account uses geolite.info as its host.

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 Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
ipAddress: PWideChar;
http: HCkHttp;
url: HCkStringBuilder;
response: HCkHttpResponse;
json: HCkJsonObject;
parsed: Boolean;

begin
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');
CkHttp_putBasicAuth(http,True);
CkHttp_putLogin(http,'MAXMIND_ACCOUNT_ID');
CkHttp_putPassword(http,'MAXMIND_LICENSE_KEY');
url := CkStringBuilder_Create();
CkStringBuilder_Append(url,'https://geoip.maxmind.com/geoip/v2.1/country/');
CkStringBuilder_Append(url,CkHttp__urlEncode(http,ipAddress));
response := CkHttpResponse_Create();
success := CkHttp_HttpNoBody(http,'GET',CkStringBuilder__getAsString(url),response);
if (CkHttpResponse_getStatusCode(response) = 0) then
  begin
    //  Request diagnostics can contain an API key. Report transport failure without logging credentials.
    Memo1.Lines.Add('No HTTP response. Check connectivity, DNS, and TLS configuration.');
    Exit;
  end;
json := CkJsonObject_Create();
parsed := CkJsonObject_Load(json,CkHttpResponse__bodyStr(response));
if ((success <> True) or (CkHttpResponse_getStatusCode(response) < 200) or (CkHttpResponse_getStatusCode(response) >= 300)) then
  begin
    Memo1.Lines.Add('Lookup failed. HTTP ' + IntToStr(CkHttpResponse_getStatusCode(response)));
    if (parsed = True) then
      begin
        if (CkJsonObject_JsonTypeOf(json,'code') = 1) then
          begin
            Memo1.Lines.Add(CkJsonObject__stringOf(json,'code'));
          end;
        if (CkJsonObject_JsonTypeOf(json,'error') = 1) then
          begin
            Memo1.Lines.Add(CkJsonObject__stringOf(json,'error'));
          end;
      end;
    Exit;
  end;
if (parsed <> True) then
  begin
    Memo1.Lines.Add('Expected a JSON object in the lookup response.');
    Exit;
  end;
if ((CkJsonObject_JsonTypeOf(json,'traits.ip_address') <> 1) or (CkJsonObject_StringOfEquals(json,'traits.ip_address','',True) = True)) then
  begin
    Memo1.Lines.Add('The response did not contain the expected IP address.');
    Exit;
  end;
Memo1.Lines.Add('IP address: ' + CkJsonObject__stringOf(json,'traits.ip_address'));
if ((CkJsonObject_JsonTypeOf(json,'country.names.en') = 1) and (CkJsonObject_StringOfEquals(json,'country.names.en','',True) <> True)) then
  begin
    Memo1.Lines.Add('Country: ' + CkJsonObject__stringOf(json,'country.names.en'));
  end
else
  begin
    Memo1.Lines.Add('Country: unavailable');
  end;
if ((CkJsonObject_JsonTypeOf(json,'country.iso_code') = 1) and (CkJsonObject_StringOfEquals(json,'country.iso_code','',True) <> True)) then
  begin
    Memo1.Lines.Add('Country code: ' + CkJsonObject__stringOf(json,'country.iso_code'));
  end
else
  begin
    Memo1.Lines.Add('Country code: unavailable');
  end;
if ((CkJsonObject_JsonTypeOf(json,'continent.names.en') = 1) and (CkJsonObject_StringOfEquals(json,'continent.names.en','',True) <> True)) then
  begin
    Memo1.Lines.Add('Continent: ' + CkJsonObject__stringOf(json,'continent.names.en'));
  end
else
  begin
    Memo1.Lines.Add('Continent: unavailable');
  end;
if ((CkJsonObject_JsonTypeOf(json,'registered_country.iso_code') = 1) and (CkJsonObject_StringOfEquals(json,'registered_country.iso_code','',True) <> True)) then
  begin
    Memo1.Lines.Add('Registered country code: ' + CkJsonObject__stringOf(json,'registered_country.iso_code'));
  end
else
  begin
    Memo1.Lines.Add('Registered country code: unavailable');
  end;

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