Country Codes in IP Geolocation and Geo-Targeting
How country codes are used in IP geolocation databases, geo-fencing, content localization, and regulatory compliance for region-specific content delivery.
Detailed Explanation
Country Codes and Geolocation
IP geolocation maps an IP address to a geographic location, and the country code is the most reliable piece of data returned. City and region data can be inaccurate, but country-level geolocation is typically 95-99% accurate.
Geolocation Database Response
A typical GeoIP lookup returns:
{
"ip": "203.0.113.42",
"country_code": "JP",
"country_name": "Japan",
"region_code": "JP-13",
"region_name": "Tokyo",
"city": "Shibuya",
"postal_code": "150-0002",
"latitude": 35.6617,
"longitude": 139.7041,
"timezone": "Asia/Tokyo",
"isp": "NTT Communications"
}
Common Geolocation Use Cases
- Content localization — Show content in the user's language based on country
- Pricing — Display prices in local currency
- Compliance — Block access from sanctioned countries
- Licensing — Restrict media content by region (geo-fencing)
- Fraud detection — Flag mismatches between billing country and IP country
- Ad targeting — Serve region-specific advertisements
- CDN routing — Direct users to the nearest data center
Server-Side Geolocation
// Using MaxMind GeoIP2 in Node.js
const Reader = require('@maxmind/geoip2-node').Reader;
async function getCountry(ip) {
const reader = await Reader.open('/path/to/GeoLite2-Country.mmdb');
const response = reader.country(ip);
return {
code: response.country.isoCode, // "JP"
name: response.country.names.en, // "Japan"
isEU: response.country.isInEuropeanUnion // false
};
}
Client-Side Geolocation Headers
Many CDNs and reverse proxies add geolocation headers:
CF-IPCountry: JP (Cloudflare)
X-Vercel-IP-Country: JP (Vercel)
X-Amz-Cf-IPCountry: JP (CloudFront — custom)
X-Country-Code: JP (Custom header convention)
Geo-Fencing Patterns
// Block access from specific countries
const BLOCKED = ['KP', 'IR', 'CU', 'SY'];
function checkAccess(countryCode) {
if (BLOCKED.includes(countryCode)) {
return { allowed: false, reason: 'Service unavailable in your region' };
}
return { allowed: true };
}
// EU-specific compliance
function requiresCookieConsent(countryCode) {
const EU_EEA = ['AT','BE','BG', /* ... */,'SE','IS','LI','NO'];
return EU_EEA.includes(countryCode);
}
Accuracy Considerations
| Level | Typical Accuracy |
|---|---|
| Country | 95-99% |
| Region/State | 80-90% |
| City | 60-80% |
| Postal code | 50-70% |
VPNs, proxies, and mobile networks can reduce accuracy. Always provide a way for users to manually set their country.
Use Case
A streaming service uses the Cloudflare CF-IPCountry header to enforce content licensing agreements. If the country code is not in the licensed regions list, the user sees a 'Not available in your region' message. Users traveling abroad can override their country in account settings.
Try It — Country Code Reference
Related Topics
ISO 3166-1 Alpha-2 Codes — The Two-Letter Country Standard
Standards
EU Country Codes — All 27 European Union Member States
Industry
Country Codes in Payment Processing and Financial Systems
Industry
Using Country Codes in REST APIs and GraphQL
Programming
Country Codes in Address Forms — Building International Address UIs
Historical