Lazarus Pascal Requires Chilkat v11.0.0+
Lazarus Pascal
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.
Chilkat Lazarus Pascal Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.Http,
Chilkat.HttpResponse,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
ipAddress: string;
http: THttp;
parameters: TJsonObject;
response: THttpResponse;
json: TJsonObject;
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 := THttp.Create;
http.RequireSslCertVerify := True;
http.ConnectTimeout := 10;
http.ReadTimeout := 30;
http.Accept := 'application/json';
parameters := TJsonObject.Create;
parameters.UpdateString('apiKey','YOUR_API_KEY');
parameters.UpdateString('ipAddress',ipAddress);
response := THttpResponse.Create;
success := http.HttpParams('GET','https://geo.ipify.org/api/v2/country,city',parameters,response);
if (response.StatusCode = 0) then
begin
// Request diagnostics can contain an API key. Report transport failure without logging credentials.
WriteLn('No HTTP response. Check connectivity, DNS, and TLS configuration.');
Exit;
end;
json := TJsonObject.Create;
parsed := json.Load(response.BodyStr);
if ((success <> True) or (response.StatusCode < 200) or (response.StatusCode >= 300)) then
begin
WriteLn('Lookup failed. HTTP ' + response.StatusCode);
if (parsed = True) then
begin
if (json.JsonTypeOf('messages') = 1) then
begin
WriteLn(json.StringOf('messages'));
end;
if (json.JsonTypeOf('message') = 1) then
begin
WriteLn(json.StringOf('message'));
end;
end;
Exit;
end;
if (parsed <> True) then
begin
WriteLn('Expected a JSON object in the lookup response.');
Exit;
end;
if ((json.JsonTypeOf('ip') <> 1) or (json.StringOfEquals('ip','',True) = True)) then
begin
WriteLn('The response did not contain the expected IP address.');
Exit;
end;
WriteLn('IP address: ' + json.StringOf('ip'));
if ((json.JsonTypeOf('location.country') = 1) and (json.StringOfEquals('location.country','',True) <> True)) then
begin
WriteLn('Country code: ' + json.StringOf('location.country'));
end
else
begin
WriteLn('Country code: unavailable');
end;
if ((json.JsonTypeOf('location.region') = 1) and (json.StringOfEquals('location.region','',True) <> True)) then
begin
WriteLn('Region: ' + json.StringOf('location.region'));
end
else
begin
WriteLn('Region: unavailable');
end;
if ((json.JsonTypeOf('location.city') = 1) and (json.StringOfEquals('location.city','',True) <> True)) then
begin
WriteLn('City: ' + json.StringOf('location.city'));
end
else
begin
WriteLn('City: unavailable');
end;
if ((json.JsonTypeOf('location.timezone') = 1) and (json.StringOfEquals('location.timezone','',True) <> True)) then
begin
WriteLn('UTC offset: ' + json.StringOf('location.timezone'));
end
else
begin
WriteLn('UTC offset: unavailable');
end;
// Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
if ((json.JsonTypeOf('location.lat') = 2) and (json.JsonTypeOf('location.lng') = 2)) then
begin
WriteLn('Coordinates: ' + json.StringOf('location.lat') + ', ' + json.StringOf('location.lng'));
end
else
begin
WriteLn('Coordinates: unavailable');
end;
http.Free;
parameters.Free;
response.Free;
json.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.