Sample code for 30+ languages & platforms
Dart Requires Chilkat v11.0.0+

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.

Service documentation

Chilkat Dart Downloads

Dart
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');
  }
}