Delphi DLL Requires Chilkat v11.0.0+
Delphi DLL
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 DLL Downloads
var
success: Boolean;
ipAddress: PWideChar;
http: HCkHttp;
url: HCkStringBuilder;
parameters: HCkJsonObject;
response: HCkHttpResponse;
remaining: PWideChar;
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');
// The free endpoint is HTTP-only. This single lookup sends no API credentials.
url := CkStringBuilder_Create();
CkStringBuilder_Append(url,'http://ip-api.com/json/');
CkStringBuilder_Append(url,CkHttp__urlEncode(http,ipAddress));
parameters := CkJsonObject_Create();
CkJsonObject_UpdateString(parameters,'fields','status,message,query,country,countryCode,regionName,city,lat,lon,timezone');
response := CkHttpResponse_Create();
success := CkHttp_HttpParams(http,'GET',CkStringBuilder__getAsString(url),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;
// Save these values if the application will make further requests.
remaining := CkHttpResponse__getHeaderField(response,'X-Rl');
if (remaining = '0') then
begin
Memo1.Lines.Add('Rate limit reached. Wait ' + CkHttpResponse__getHeaderField(response,'X-Ttl') + ' seconds before the next request.');
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,'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_StringOfEquals(json,'status','success',True) <> True) then
begin
Memo1.Lines.Add('The service could not complete the lookup.');
if (CkJsonObject_JsonTypeOf(json,'message') = 1) then
begin
Memo1.Lines.Add(CkJsonObject__stringOf(json,'message'));
end;
Exit;
end;
if ((CkJsonObject_JsonTypeOf(json,'query') <> 1) or (CkJsonObject_StringOfEquals(json,'query','',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,'query'));
if ((CkJsonObject_JsonTypeOf(json,'country') = 1) and (CkJsonObject_StringOfEquals(json,'country','',True) <> True)) then
begin
Memo1.Lines.Add('Country: ' + CkJsonObject__stringOf(json,'country'));
end
else
begin
Memo1.Lines.Add('Country: unavailable');
end;
if ((CkJsonObject_JsonTypeOf(json,'countryCode') = 1) and (CkJsonObject_StringOfEquals(json,'countryCode','',True) <> True)) then
begin
Memo1.Lines.Add('Country code: ' + CkJsonObject__stringOf(json,'countryCode'));
end
else
begin
Memo1.Lines.Add('Country code: unavailable');
end;
if ((CkJsonObject_JsonTypeOf(json,'regionName') = 1) and (CkJsonObject_StringOfEquals(json,'regionName','',True) <> True)) then
begin
Memo1.Lines.Add('Region: ' + CkJsonObject__stringOf(json,'regionName'));
end
else
begin
Memo1.Lines.Add('Region: unavailable');
end;
if ((CkJsonObject_JsonTypeOf(json,'city') = 1) and (CkJsonObject_StringOfEquals(json,'city','',True) <> True)) then
begin
Memo1.Lines.Add('City: ' + CkJsonObject__stringOf(json,'city'));
end
else
begin
Memo1.Lines.Add('City: unavailable');
end;
if ((CkJsonObject_JsonTypeOf(json,'timezone') = 1) and (CkJsonObject_StringOfEquals(json,'timezone','',True) <> True)) then
begin
Memo1.Lines.Add('Time zone: ' + CkJsonObject__stringOf(json,'timezone'));
end
else
begin
Memo1.Lines.Add('Time zone: unavailable');
end;
// Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
if ((CkJsonObject_JsonTypeOf(json,'lat') = 2) and (CkJsonObject_JsonTypeOf(json,'lon') = 2)) then
begin
Memo1.Lines.Add('Coordinates: ' + CkJsonObject__stringOf(json,'lat') + ', ' + CkJsonObject__stringOf(json,'lon'));
end
else
begin
Memo1.Lines.Add('Coordinates: unavailable');
end;
CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkJsonObject_Dispose(parameters);
CkHttpResponse_Dispose(response);
CkJsonObject_Dispose(json);