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 Swift Downloads
func chilkatTest() {
var success: Bool = false
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Use a public IP address. Provider results can change as their databases are updated.
var ipAddress: String? = "8.8.8.8"
let http = CkoHttp()!
http.requireSslCertVerify = true
http.connectTimeout = 10
http.readTimeout = 30
http.accept = "application/json"
let url = CkoStringBuilder()!
url.append(value: "https://api.db-ip.com/v2/free/")
url.append(value: http.urlEncode(str: ipAddress))
let response = CkoHttpResponse()!
success = http.httpNoBody(verb: "GET", url: url.getAsString(), response: response)
if response.statusCode.intValue == 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
}
let json = CkoJsonObject()!
var parsed: Bool = json.load(json: response.bodyStr)
if (success != true) || (response.statusCode.intValue < 200) || (response.statusCode.intValue >= 300) {
print("Lookup failed. HTTP \(response.statusCode.intValue)")
if parsed == true {
if json.jsonType(of: "errorCode").intValue == 1 {
print("\(json.string(of: "errorCode")!)")
}
if json.jsonType(of: "error").intValue == 1 {
print("\(json.string(of: "error")!)")
}
}
return
}
if parsed != true {
print("Expected a JSON object in the lookup response.")
return
}
if (json.hasMember(jsonPath: "errorCode") == true) || (json.jsonType(of: "error").intValue == 1) {
print("The service could not complete the lookup.")
if json.jsonType(of: "errorCode").intValue == 1 {
print("\(json.string(of: "errorCode")!)")
}
if json.jsonType(of: "error").intValue == 1 {
print("\(json.string(of: "error")!)")
}
return
}
if (json.jsonType(of: "ipAddress").intValue != 1) || (json.string(ofEquals: "ipAddress", value: "", caseSensitive: true) == true) {
print("The response did not contain the expected IP address.")
return
}
print("IP address: \(json.string(of: "ipAddress")!)")
if (json.jsonType(of: "countryName").intValue == 1) && (json.string(ofEquals: "countryName", value: "", caseSensitive: true) != true) {
print("Country: \(json.string(of: "countryName")!)")
}
else {
print("Country: unavailable")
}
if (json.jsonType(of: "countryCode").intValue == 1) && (json.string(ofEquals: "countryCode", value: "", caseSensitive: true) != true) {
print("Country code: \(json.string(of: "countryCode")!)")
}
else {
print("Country code: unavailable")
}
if (json.jsonType(of: "stateProv").intValue == 1) && (json.string(ofEquals: "stateProv", value: "", caseSensitive: true) != true) {
print("State or province: \(json.string(of: "stateProv")!)")
}
else {
print("State or province: unavailable")
}
if (json.jsonType(of: "city").intValue == 1) && (json.string(ofEquals: "city", value: "", caseSensitive: true) != true) {
print("City: \(json.string(of: "city")!)")
}
else {
print("City: unavailable")
}
}