Delphi ActiveX Requires Chilkat v11.0.0+
Delphi ActiveX
Look up an IP address with the ip-api free endpoint
See more Geolocation Examples
This example uses the free, keyless ip-api endpoint for non-commercial use. The free service supports HTTP and is limited to 45 requests per minute per source IP. Its Pro service provides HTTPS and commercial use. ip-api Pro.
Always include status and message when selecting fields. Invalid or private addresses can produce status=fail inside a JSON response. Retain the X-Rl and X-Ttl response values between calls, and pause further requests when X-Rl reaches zero.
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 Delphi ActiveX Downloads
var
success: Integer;
ipAddress: WideString;
http: TChilkatHttp;
url: TChilkatStringBuilder;
parameters: TChilkatJsonObject;
response: TChilkatHttpResponse;
remaining: WideString;
json: TChilkatJsonObject;
parsed: Integer;
begin
success := 0;
// 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 := TChilkatHttp.Create(Self);
http.RequireSslCertVerify := 1;
http.ConnectTimeout := 10;
http.ReadTimeout := 30;
http.Accept := 'application/json';
// The free endpoint is HTTP-only. This single lookup sends no API credentials.
url := TChilkatStringBuilder.Create(Self);
url.Append('http://ip-api.com/json/');
url.Append(http.UrlEncode(ipAddress));
parameters := TChilkatJsonObject.Create(Self);
parameters.UpdateString('fields','status,message,query,country,countryCode,regionName,city,lat,lon,timezone');
response := TChilkatHttpResponse.Create(Self);
success := http.HttpParams('GET',url.GetAsString(),parameters.ControlInterface,response.ControlInterface);
if (response.StatusCode = 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;
// Save these values if the application will make further requests.
remaining := response.GetHeaderField('X-Rl');
if (remaining = '0') then
begin
Memo1.Lines.Add('Rate limit reached. Wait ' + response.GetHeaderField('X-Ttl') + ' seconds before the next request.');
end;
json := TChilkatJsonObject.Create(Self);
parsed := json.Load(response.BodyStr);
if ((success <> 1) or (response.StatusCode < 200) or (response.StatusCode >= 300)) then
begin
Memo1.Lines.Add('Lookup failed. HTTP ' + IntToStr(response.StatusCode));
if (parsed = 1) then
begin
if (json.JsonTypeOf('message') = 1) then
begin
Memo1.Lines.Add(json.StringOf('message'));
end;
end;
Exit;
end;
if (parsed <> 1) then
begin
Memo1.Lines.Add('Expected a JSON object in the lookup response.');
Exit;
end;
if (json.StringOfEquals('status','success',1) <> 1) then
begin
Memo1.Lines.Add('The service could not complete the lookup.');
if (json.JsonTypeOf('message') = 1) then
begin
Memo1.Lines.Add(json.StringOf('message'));
end;
Exit;
end;
if ((json.JsonTypeOf('query') <> 1) or (json.StringOfEquals('query','',1) = 1)) then
begin
Memo1.Lines.Add('The response did not contain the expected IP address.');
Exit;
end;
Memo1.Lines.Add('IP address: ' + json.StringOf('query'));
if ((json.JsonTypeOf('country') = 1) and (json.StringOfEquals('country','',1) <> 1)) then
begin
Memo1.Lines.Add('Country: ' + json.StringOf('country'));
end
else
begin
Memo1.Lines.Add('Country: unavailable');
end;
if ((json.JsonTypeOf('countryCode') = 1) and (json.StringOfEquals('countryCode','',1) <> 1)) then
begin
Memo1.Lines.Add('Country code: ' + json.StringOf('countryCode'));
end
else
begin
Memo1.Lines.Add('Country code: unavailable');
end;
if ((json.JsonTypeOf('regionName') = 1) and (json.StringOfEquals('regionName','',1) <> 1)) then
begin
Memo1.Lines.Add('Region: ' + json.StringOf('regionName'));
end
else
begin
Memo1.Lines.Add('Region: unavailable');
end;
if ((json.JsonTypeOf('city') = 1) and (json.StringOfEquals('city','',1) <> 1)) then
begin
Memo1.Lines.Add('City: ' + json.StringOf('city'));
end
else
begin
Memo1.Lines.Add('City: unavailable');
end;
if ((json.JsonTypeOf('timezone') = 1) and (json.StringOfEquals('timezone','',1) <> 1)) then
begin
Memo1.Lines.Add('Time zone: ' + json.StringOf('timezone'));
end
else
begin
Memo1.Lines.Add('Time zone: unavailable');
end;
// Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
if ((json.JsonTypeOf('lat') = 2) and (json.JsonTypeOf('lon') = 2)) then
begin
Memo1.Lines.Add('Coordinates: ' + json.StringOf('lat') + ', ' + json.StringOf('lon'));
end
else
begin
Memo1.Lines.Add('Coordinates: unavailable');
end;