The title might seem a bit odd, but I have a situation where I have a web application (Node.js application served up behind Azure Frontdoor + Azure App Service) and need to know if a request has come from Scotland, so that I can show some specific assets (mostly images) to the user in place of the default ones.
I was hoping to do this programmatically, leveraging Frontdoor’s capabilities to inject something into the request coming into App Service and then checking the header in my Node.js application and decide what assets to load that way.
However, Frontdoor will only tell me if a request originated in the United Kingdom. I’ve also looked at a few libraries/APIs and nothing seems to be that granular. I’ve also tried looking up if there is a canonical list of IP ranges within Scotland, but that (expectedly) turned up nothing.
I know this problem could be solved with a UX change (i.e. asking someone at the beginning of their journey where they reside) but was hoping to make things slightly more seamless.
Any suggestions would be appreciated.
2
Answers
You could use a service like https://ipinfo.io/ it has an
IP Geolocation API
.You can get the user IP in the header
X-Azure-ClientIP
from Frontdoor and then make an API call to get the region. Furthermore, you should link that information to a user for persistence you won’t have to make an API call every time a user access your website.Disclaimer — I’m not associated with IPinfo in any way, I just use its API myself.
As Lauden mentioned in his answer, the IP Geolocation API of IPinfo is a convenient choice. For a low number of batch requests — something like 100-150 subsequent ones for the same requesting IP — the service is free, for more professional use there are paid models.
For example you can check on the region using
curl
as follows:Than you’ll get e.g.
If you replace
region
and check on the parametercity
, the first one yields Glasgow, the second one Edinburgh.The full dataset for
84.64.83.219
(Glasgow), when using free access to the API:And, to the last, IPinfo also provides an Official Command Line Interface for its APIs, supporting Linux (Debian, Ubuntu, Arch), FreeBSD, and even obscure operating systems like Windows and MacOS.