Guides

Detect a visitor's country for tax and currency defaults

Asking a new visitor to pick their country from a long dropdown before showing them a price is one extra step that a decent default can remove entirely.

Getting the country code

A single call to /v1/ip with the visitor's address returns a country_code field in standard ISO 3166-1 alpha-2 format, which is the format most tax and currency lookup tables already key on.

GET /v1/ip?ip=198.51.100.7
{
  "status": "ok",
  "ip": "198.51.100.7",
  "version": 4,
  "found": true,
  "country": "Germany",
  "country_code": "DE",
  "region": "Berlin",
  "city": "Berlin",
  "postcode": "10115",
  "lat": 52.5200,
  "lon": 13.4050,
  "timezone": "Europe/Berlin",
  "asn": 6789,
  "org": "Example Telecom"
}

From country code to tax and currency

Map the country_code onto your own tax rate table and currency table, the same way you would map a country a customer selected manually. Treat the IP-derived country as a default, not a final answer, and let the visitor change it, since a traveling customer or one behind a VPN will not always match the billing country they actually need.

When the lookup does not resolve

A request against an address with no location data on file returns found set to false rather than an error, which is worth planning for explicitly. Fall back to a single sensible default country and currency in that case rather than leaving the field blank or letting an empty country_code reach your tax calculation unnoticed.

Where My Geocode itself differs

My Geocode's own pricing runs in EUR only, worldwide, regardless of where a customer is billing from. That is a separate matter from what you show your own visitors, where localizing the displayed currency by detected country is entirely reasonable even if your own backend settles everything in one currency, the same way ours does.

A mistake worth avoiding

Locking a visitor into the detected country with no way to change it is a bigger problem than getting the default wrong occasionally. A customer shopping from a hotel abroad, or from behind a corporate VPN that exits in a different country than they are actually in, will hit a wrong default sooner or later. Always show the detected country as an editable field rather than a fixed value baked into the order.

Request cost

One lookup per new visitor session, cached for the length of that session, is the sensible pattern. That is one request against your daily allowance per session rather than per page view, which keeps even a busy site well inside the 2,500 free requests a day included with every key or available with no key at all from a single address.

The same response also includes a timezone field, so a checkout flow that wants both a currency default and a sensible local time display for order confirmations can get both from one call rather than two separate lookups.

Getting a currency default right on the first page view avoids a jarring price switch later in checkout. The IPv4 lookup docs list every field the endpoint returns.