Geolocation of IP Address
See more REST Examples
Retrieve country, state or province, and city for a public IP address using the DB-IP v2 free JSON API. This replaces the retired freegeoip.net request and the historical two-service comparison. The sample sends one HTTPS request, checks transport and service errors, and reads the returned fields.
DB-IP documents 500 daily requests for its free service. Its free HTTPS endpoint returned a successful lookup during verification on November 20, 2026. For other providers and authenticated services, see the Geolocation examples.
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 Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
var success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Use a public IP address. Provider results can change as their databases are updated.
final ipAddress = '8.8.8.8';
final http = CkHttp();
http.requireSslCertVerify = true;
http.connectTimeout = 10;
http.readTimeout = 30;
http.accept = 'application/json';
final url = CkStringBuilder();
url.append('https://api.db-ip.com/v2/free/');
url.append(http.urlEncode(ipAddress));
final response = CkHttpResponse();
success = true;
try {
http.httpNoBody('GET', url.getAsString(), response);
} on ChilkatException {
success = false;
}
if (response.statusCode == 0) {
// Request diagnostics can contain an API key. Report transport failure without logging credentials.
print('No HTTP response. Check connectivity, DNS, and TLS configuration.');
return;
}
final json = CkJsonObject();
var parsed = true;
try {
json.load(response.bodyStr);
} on ChilkatException {
parsed = false;
}
if ((!success) || ((response.statusCode < 200) || (response.statusCode >= 300))) {
print('Lookup failed. HTTP ${response.statusCode}');
if (parsed) {
if (json.jsonTypeOf('errorCode') == 1) {
print(json.stringOf('errorCode'));
}
if (json.jsonTypeOf('error') == 1) {
print(json.stringOf('error'));
}
}
return;
}
if (!parsed) {
print('Expected a JSON object in the lookup response.');
return;
}
if ((json.hasMember('errorCode')) || (json.jsonTypeOf('error') == 1)) {
print('The service could not complete the lookup.');
if (json.jsonTypeOf('errorCode') == 1) {
print(json.stringOf('errorCode'));
}
if (json.jsonTypeOf('error') == 1) {
print(json.stringOf('error'));
}
return;
}
if ((json.jsonTypeOf('ipAddress') != 1) || (json.stringOfEquals('ipAddress', '', true))) {
print('The response did not contain the expected IP address.');
return;
}
print('IP address: ${json.stringOf('ipAddress')}');
if ((json.jsonTypeOf('countryName') == 1) && (!json.stringOfEquals('countryName', '', true))) {
print('Country: ${json.stringOf('countryName')}');
} else {
print('Country: unavailable');
}
if ((json.jsonTypeOf('countryCode') == 1) && (!json.stringOfEquals('countryCode', '', true))) {
print('Country code: ${json.stringOf('countryCode')}');
} else {
print('Country code: unavailable');
}
if ((json.jsonTypeOf('stateProv') == 1) && (!json.stringOfEquals('stateProv', '', true))) {
print('State or province: ${json.stringOf('stateProv')}');
} else {
print('State or province: unavailable');
}
if ((json.jsonTypeOf('city') == 1) && (!json.stringOfEquals('city', '', true))) {
print('City: ${json.stringOf('city')}');
} else {
print('City: unavailable');
}
}