How to automatically detect and select a user's country

Learn how to personalize your website by detecting the visitor’s country using IP geolocation with the IPLocate API.

Want to personalize your website, localize content, or comply with regulations based on where your users are coming from? Automatically detecting a visitor's country is the first step.

With IPLocate, you can detect a user's country using just their IP address — no cookies, no prompts, no client permissions required. It's fast, accurate, and easy to implement in both client-side JavaScript and server-side code.

Sign up for a free API key to get started — no credit card required.

Why Detect a Visitor’s Country?

Developers and product teams often need to tailor websites based on user location. Here are some common use cases for automatic country detection:

  • Geo-targeted content: Show localized offers, promotions, or product availability.
  • Form autofill: Pre-populate country fields in signup or checkout forms.
  • Local currencies and payment methods: Improve conversion by using the visitor’s local settings.
  • Legal compliance: Show GDPR consent banners to EU visitors only.
  • Access control: Restrict or redirect users from unsupported regions.

In all these cases, the simplest way to determine a user’s country is by using their IP address — and that’s where IPLocate comes in.

Using IPLocate to Detect Country by IP

The IPLocate API gives you real-time data about your users based on their IP address — including the country name, country code, timezone, currency, and more. You can view all the fields in our docs.

And best of all: if you don't provide an IP address, the API automatically uses the IP of the requester. That makes client-side detection incredibly easy.

Client-Side Example (JavaScript)

Here’s how to detect the visitor’s country using JavaScript on the client:

const res = await fetch('https://iplocate.io/api/lookup?apikey=YOUR_API_KEY');
const data = await res.json();

if (data.country_code === 'US') {
  console.log('Welcome, American visitor!');
} else {
  console.log(`Hello from ${data.country}!`);
}

Use this to:

  • Show a greeting in the user’s local language
  • Preselect their country in a form
  • Customize pricing or shipping messages

You don’t need to ask for location permission — it works based on IP address alone.

You also don’t need to worry about getting the user’s IP address — IPLocate will automatically use the user’s IP address if you don’t provide one in the request.

Server-Side Example (Python)

You can also detect the user’s country on the server side — great for pre-rendered content, access control, or conditional logic.

import requests

ip = request.remote_addr

res = requests.get(f'https://iplocate.io/api/lookup/{ip}', params={'apikey': 'YOUR_API_KEY'})
data = res.json()

if data['country_code'] != 'US':
    print("Sorry, this service is only available in the United States.")
    exit()
else:
    print("Welcome, US-based visitor!")

Server-side detection is perfect for:

  • Redirecting users to country-specific versions of your site
  • Blocking traffic from unsupported regions
  • Localizing emails or invoices

What Else Can You Do?

In addition to country-level data, IPLocate provides fields like:

  • currency_code — to show prices in local currency
  • time_zone — to schedule notifications or content drops
  • is_vpn, is_proxy, is_abuser — to detect potential threats
  • company, asn, hosting — to enrich your analytics or threat detection

For the full list of available fields, check out the API documentation.

Start Using IPLocate Today

Geolocation doesn't need to be complicated. With IPLocate, detecting a user's country is just a single API call away.

Sign up now to get a free API key and start detecting user countries instantly.