Looking up a country from an IP takes one HTTP request. Getting the right IP, handling IPv6 and private addresses, caching results and failing gracefully takes a little more. Working examples in four languages.
Most "best free IP geolocation API" lists repeat numbers that are years out of date. We read each provider's own pricing and documentation pages and quote what they say about the free tier, including the conditions that decide whether you can actually use it.
The country tells you where an address is. The autonomous system number tells you whose network it is, and that is often the more useful question. How to look up ASNs by hand and in code, and what to do with them.
Automatically redirecting by IP looks simple, and it is one of the easiest ways to make your other country sites invisible in search. Here is what Google recommends, a pattern that works for people and crawlers, and code for it.
IP geolocation is an estimate built from registry records, operator feeds and network measurements. It is usually right about the country and only approximate about the city. Knowing why helps you use it where it works and avoid it where it doesn't.
IP geolocation
Guides
Try it
One GET request: location, network and bot signals
Free keys include 1,000 lookups a day. No card needed.