Look up an IP address with the DB-IP free API
See more Geolocation Examples
Use the DB-IP v2 free service to retrieve country, state or province, and city without an API key. This example uses HTTPS, which returned a successful free lookup during verification on November 20, 2026. The provider documents a limit of 500 daily requests for the free service. Paid services provide additional fields and quotas.
Check errorCode before reading the location. Free results have a smaller field set, and the application should handle missing location values.
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;
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');
url := CkStringBuilder_Create();
CkStringBuilder_Append(url,'https://api.db-ip.com/v2/free/');
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,'errorCode') = 1) then
begin
Memo1.Lines.Add(CkJsonObject__stringOf(json,'errorCode'));
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_HasMember(json,'errorCode') = True) or (CkJsonObject_JsonTypeOf(json,'error') = 1)) then
begin
Memo1.Lines.Add('The service could not complete the lookup.');
if (CkJsonObject_JsonTypeOf(json,'errorCode') = 1) then
begin
Memo1.Lines.Add(CkJsonObject__stringOf(json,'errorCode'));
end;
if (CkJsonObject_JsonTypeOf(json,'error') = 1) then
begin
Memo1.Lines.Add(CkJsonObject__stringOf(json,'error'));
end;
Exit;
end;
if ((CkJsonObject_JsonTypeOf(json,'ipAddress') <> 1) or (CkJsonObject_StringOfEquals(json,'ipAddress','',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,'ipAddress'));
if ((CkJsonObject_JsonTypeOf(json,'countryName') = 1) and (CkJsonObject_StringOfEquals(json,'countryName','',True) <> True)) then
begin
Memo1.Lines.Add('Country: ' + CkJsonObject__stringOf(json,'countryName'));
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,'stateProv') = 1) and (CkJsonObject_StringOfEquals(json,'stateProv','',True) <> True)) then
begin
Memo1.Lines.Add('State or province: ' + CkJsonObject__stringOf(json,'stateProv'));
end
else
begin
Memo1.Lines.Add('State or province: 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;
CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkHttpResponse_Dispose(response);
CkJsonObject_Dispose(json);