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

Look up an IP location with the ipify v2 API

See more Geolocation Examples

Use the ipify IP Geolocation v2 country,city endpoint with an API key. This endpoint returns country and city details, including coordinates when available, and currently costs two credits per request. The country endpoint has a smaller result and costs one credit.

The example explicitly supplies the target IPv4 or IPv6 address and URL-encodes the query parameters. location.timezone is a UTC offset. This replaces the older v1 request used by this example.

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;
parameters: HCkJsonObject;
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');
parameters := CkJsonObject_Create();
CkJsonObject_UpdateString(parameters,'apiKey','YOUR_API_KEY');
CkJsonObject_UpdateString(parameters,'ipAddress',ipAddress);
response := CkHttpResponse_Create();
success := CkHttp_HttpParams(http,'GET','https://geo.ipify.org/api/v2/country,city',parameters,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,'messages') = 1) then
          begin
            Memo1.Lines.Add(CkJsonObject__stringOf(json,'messages'));
          end;
        if (CkJsonObject_JsonTypeOf(json,'message') = 1) then
          begin
            Memo1.Lines.Add(CkJsonObject__stringOf(json,'message'));
          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,'ip') <> 1) or (CkJsonObject_StringOfEquals(json,'ip','',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,'ip'));
if ((CkJsonObject_JsonTypeOf(json,'location.country') = 1) and (CkJsonObject_StringOfEquals(json,'location.country','',True) <> True)) then
  begin
    Memo1.Lines.Add('Country code: ' + CkJsonObject__stringOf(json,'location.country'));
  end
else
  begin
    Memo1.Lines.Add('Country code: unavailable');
  end;
if ((CkJsonObject_JsonTypeOf(json,'location.region') = 1) and (CkJsonObject_StringOfEquals(json,'location.region','',True) <> True)) then
  begin
    Memo1.Lines.Add('Region: ' + CkJsonObject__stringOf(json,'location.region'));
  end
else
  begin
    Memo1.Lines.Add('Region: unavailable');
  end;
if ((CkJsonObject_JsonTypeOf(json,'location.city') = 1) and (CkJsonObject_StringOfEquals(json,'location.city','',True) <> True)) then
  begin
    Memo1.Lines.Add('City: ' + CkJsonObject__stringOf(json,'location.city'));
  end
else
  begin
    Memo1.Lines.Add('City: unavailable');
  end;
if ((CkJsonObject_JsonTypeOf(json,'location.timezone') = 1) and (CkJsonObject_StringOfEquals(json,'location.timezone','',True) <> True)) then
  begin
    Memo1.Lines.Add('UTC offset: ' + CkJsonObject__stringOf(json,'location.timezone'));
  end
else
  begin
    Memo1.Lines.Add('UTC offset: unavailable');
  end;
//  Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
if ((CkJsonObject_JsonTypeOf(json,'location.lat') = 2) and (CkJsonObject_JsonTypeOf(json,'location.lng') = 2)) then
  begin
    Memo1.Lines.Add('Coordinates: ' + CkJsonObject__stringOf(json,'location.lat') + ', ' + CkJsonObject__stringOf(json,'location.lng'));
  end
else
  begin
    Memo1.Lines.Add('Coordinates: unavailable');
  end;

CkHttp_Dispose(http);
CkJsonObject_Dispose(parameters);
CkHttpResponse_Dispose(response);
CkJsonObject_Dispose(json);