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 Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;
public class ChilkatExample {
public static void main(String[] args) {
boolean success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Use a public IP address. Provider results can change as their databases are updated.
String ipAddress = "8.8.8.8";
Http http = new Http();
http.setRequireSslCertVerify(true);
http.setConnectTimeout(10);
http.setReadTimeout(30);
http.setAccept("application/json");
StringBuilder url = new StringBuilder();
url.append("https://api.db-ip.com/v2/free/");
url.append(http.urlEncode(ipAddress));
HttpResponse response = new HttpResponse();
try {
http.httpNoBody("GET", url.getAsString(), response);
success = true;
} catch (ChilkatException e) {
success = false;
}
if (response.getStatusCode() == 0) {
// Request diagnostics can contain an API key. Report transport failure without logging credentials.
System.out.println("No HTTP response. Check connectivity, DNS, and TLS configuration.");
return;
}
JsonObject json = new JsonObject();
boolean parsed;
try {
json.load(response.getBodyStr());
parsed = true;
} catch (ChilkatException e) {
parsed = false;
}
if ((!success) || ((response.getStatusCode() < 200) || (response.getStatusCode() >= 300))) {
System.out.println("Lookup failed. HTTP " + response.getStatusCode());
if (parsed) {
if (json.jsonTypeOf("errorCode") == 1) {
System.out.println(json.stringOf("errorCode"));
}
if (json.jsonTypeOf("error") == 1) {
System.out.println(json.stringOf("error"));
}
}
return;
}
if (!parsed) {
System.out.println("Expected a JSON object in the lookup response.");
return;
}
if ((json.hasMember("errorCode")) || (json.jsonTypeOf("error") == 1)) {
System.out.println("The service could not complete the lookup.");
if (json.jsonTypeOf("errorCode") == 1) {
System.out.println(json.stringOf("errorCode"));
}
if (json.jsonTypeOf("error") == 1) {
System.out.println(json.stringOf("error"));
}
return;
}
if ((json.jsonTypeOf("ipAddress") != 1) || (json.stringOfEquals("ipAddress", "", true))) {
System.out.println("The response did not contain the expected IP address.");
return;
}
System.out.println("IP address: " + json.stringOf("ipAddress"));
if ((json.jsonTypeOf("countryName") == 1) && (!json.stringOfEquals("countryName", "", true))) {
System.out.println("Country: " + json.stringOf("countryName"));
} else {
System.out.println("Country: unavailable");
}
if ((json.jsonTypeOf("countryCode") == 1) && (!json.stringOfEquals("countryCode", "", true))) {
System.out.println("Country code: " + json.stringOf("countryCode"));
} else {
System.out.println("Country code: unavailable");
}
if ((json.jsonTypeOf("stateProv") == 1) && (!json.stringOfEquals("stateProv", "", true))) {
System.out.println("State or province: " + json.stringOf("stateProv"));
} else {
System.out.println("State or province: unavailable");
}
if ((json.jsonTypeOf("city") == 1) && (!json.stringOfEquals("city", "", true))) {
System.out.println("City: " + json.stringOf("city"));
} else {
System.out.println("City: unavailable");
}
}
}