Delphi DLL Requires Chilkat v11.0.0+
Delphi DLL
Look up an IP location with IPinfo Core
See more Geolocation Examples
Use an IPinfo token with Core, Plus, or Max access. The current api.ipinfo.io/lookup/ endpoint returns location fields inside geo and network fields inside as. Authenticate with a Bearer token.
IPinfo continues to support its older ipinfo.io API for existing integrations. This example uses the current Core response structure. The free Lite API uses /lite/ and a different, country-level response. Choose the endpoint and parser for your subscription.
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');
CkHttp_putAuthToken(http,'IPINFO_TOKEN');
url := CkStringBuilder_Create();
CkStringBuilder_Append(url,'https://api.ipinfo.io/lookup/');
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,'error.title') = 1) then
begin
Memo1.Lines.Add(CkJsonObject__stringOf(json,'error.title'));
end;
if (CkJsonObject_JsonTypeOf(json,'error.message') = 1) then
begin
Memo1.Lines.Add(CkJsonObject__stringOf(json,'error.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,'error') = 3)) then
begin
Memo1.Lines.Add('The service could not complete the lookup.');
if (CkJsonObject_JsonTypeOf(json,'error.title') = 1) then
begin
Memo1.Lines.Add(CkJsonObject__stringOf(json,'error.title'));
end;
if (CkJsonObject_JsonTypeOf(json,'error.message') = 1) then
begin
Memo1.Lines.Add(CkJsonObject__stringOf(json,'error.message'));
end;
Exit;
end;
if ((CkJsonObject_JsonTypeOf(json,'bogon') = 5) and (CkJsonObject_BoolOf(json,'bogon') = True)) then
begin
Memo1.Lines.Add('This is a reserved or otherwise non-public IP address.');
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,'geo.country') = 1) and (CkJsonObject_StringOfEquals(json,'geo.country','',True) <> True)) then
begin
Memo1.Lines.Add('Country: ' + CkJsonObject__stringOf(json,'geo.country'));
end
else
begin
Memo1.Lines.Add('Country: unavailable');
end;
if ((CkJsonObject_JsonTypeOf(json,'geo.country_code') = 1) and (CkJsonObject_StringOfEquals(json,'geo.country_code','',True) <> True)) then
begin
Memo1.Lines.Add('Country code: ' + CkJsonObject__stringOf(json,'geo.country_code'));
end
else
begin
Memo1.Lines.Add('Country code: unavailable');
end;
if ((CkJsonObject_JsonTypeOf(json,'geo.region') = 1) and (CkJsonObject_StringOfEquals(json,'geo.region','',True) <> True)) then
begin
Memo1.Lines.Add('Region: ' + CkJsonObject__stringOf(json,'geo.region'));
end
else
begin
Memo1.Lines.Add('Region: unavailable');
end;
if ((CkJsonObject_JsonTypeOf(json,'geo.city') = 1) and (CkJsonObject_StringOfEquals(json,'geo.city','',True) <> True)) then
begin
Memo1.Lines.Add('City: ' + CkJsonObject__stringOf(json,'geo.city'));
end
else
begin
Memo1.Lines.Add('City: unavailable');
end;
if ((CkJsonObject_JsonTypeOf(json,'geo.timezone') = 1) and (CkJsonObject_StringOfEquals(json,'geo.timezone','',True) <> True)) then
begin
Memo1.Lines.Add('Time zone: ' + CkJsonObject__stringOf(json,'geo.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,'geo.latitude') = 2) and (CkJsonObject_JsonTypeOf(json,'geo.longitude') = 2)) then
begin
Memo1.Lines.Add('Coordinates: ' + CkJsonObject__stringOf(json,'geo.latitude') + ', ' + CkJsonObject__stringOf(json,'geo.longitude'));
end
else
begin
Memo1.Lines.Add('Coordinates: unavailable');
end;
if ((CkJsonObject_JsonTypeOf(json,'as.asn') = 1) and (CkJsonObject_StringOfEquals(json,'as.asn','',True) <> True)) then
begin
Memo1.Lines.Add('ASN: ' + CkJsonObject__stringOf(json,'as.asn'));
end
else
begin
Memo1.Lines.Add('ASN: unavailable');
end;
if ((CkJsonObject_JsonTypeOf(json,'as.name') = 1) and (CkJsonObject_StringOfEquals(json,'as.name','',True) <> True)) then
begin
Memo1.Lines.Add('Network organization: ' + CkJsonObject__stringOf(json,'as.name'));
end
else
begin
Memo1.Lines.Add('Network organization: unavailable');
end;
CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkHttpResponse_Dispose(response);
CkJsonObject_Dispose(json);