Need to resolve an IP address to a city in your app? This is a practical, copy-paste guide — plus the caveats that keep you from trusting city data more than you should.

The quickest way

Call an IP geolocation API and read the city field. With detectip.ai:

curl "https://detectip.ai/api/v1/verdict?token=TOKEN" \
  -H "X-API-Key: sk_live_..."

The response includes country, region, city, approximate coordinates, timezone and currency. Get a free key to try it.

In code

Python

import requests
r = requests.get("https://detectip.ai/api/v1/verdict",
    params={"token": token}, headers={"X-API-Key": KEY})
city = r.json().get("city")

JavaScript

const r = await fetch(
  "https://detectip.ai/api/v1/verdict?token=" + token,
  { headers: { "X-API-Key": KEY } });
const { city } = await r.json();

Important caveats

Server-side vs client-side

Do the lookup server-side so you control the API key and can log responsibly. The visitor's IP is available on the connection; you don't need to ask the browser for anything.

FAQ

Why is the city wrong sometimes? The IP routes through a regional hub, the user is on a VPN, or the block was recently reassigned.

Can I get a postal code? Some plans expose more granular fields; see pricing.