How to get a user's country from their IP

Detect a visitor's country from their IP address in one request, with no API key — and get the full country record back, not just a two-letter code.

Back

"Where's this user from?" almost always means "what country," not a street address. You can get that from their IP in one call.

The request

curl https://countries.dev/ip/8.8.8.8
{
  "ip": "8.8.8.8",
  "countryCode": "US",
  "country": { "name": "United States of America", "flag": "🇺🇸", "currencies": [{ "code": "USD", "symbol": "$" }] }
}

Notice the country object. Most IP APIs hand you a bare "US" and leave the rest to you. This returns the whole record, so you can show a flag, default a currency, or preselect a country without a second request.

Detect the caller

Drop the IP and it geolocates whoever's asking:

const { country } = await fetch("https://countries.dev/ip").then((r) => r.json());
console.log(`Hi from ${country.name} ${country.flag}`);

Call it server-side where you can read the real client IP — behind a proxy or CDN that's the forwarded address. In the browser it still works; it just resolves the user's own connection, which is usually the point.

A note on accuracy

This is country-level geolocation: right the large majority of the time, but it's an IP guess, not GPS. Don't gate anything legally sensitive on it. For city- or network-level detail, reach for a dedicated provider. For "show the right flag and currency," it's plenty.

Geolocation docs →

Written by

Dov Azencot

At

Fri Jun 19 2026