Free IP-based GeoLocation with Google Cloud Functions
A few weeks ago we needed country detection for one of our Single Page Applications. We looked into two options: third-party services and self-implementation. Turns out there is a third option, which is a lot cheaper and simpler to put in place.
By using Google Cloud Functions we have up to 2 million requests per month for free.
Google Cloud Functions are serverless functions, which run independently without having to run on our own servers. The code is deployed to the Google Cloud and Google handles the rest. This makes it very easy since we can implement the function and use it without much fuss, with no geographical limitations.
Sounds great! Right? Here is our game plan: 1. Compare — Let’s make a comparison of GeoLocation third-party services to get an overview of the possible limitations. Also, let’s see how Google Cloud Functions compare to those third-party services. 2. Implement — Once we get to know what we have to play with, let’s get our hands dirty and attempt to get the cloud function online within just a few minutes. 3. Extend — If you should find yourself in need of more features, there are ways to extend the function, which we’ll examine in the last part of the article.
GeoLocation services — prices and limits
IPStack (was FreeGEOIP.net)
Free requests: 10 000 per month First tier: 9.99 USD for 50 000 requests per month
IPStack returns a lot of details, it’s reliable and trusted. It seems to be the primary recommendation for a lot of people. We used FreeGEOIP.net, but churned because of the price and the fact that we didn’t need that many details.
Free requests: 1000 per month First tier: 27.00 USD for 100 000 queries per month
This service doesn’t return as many details as IPStack. But it has a more flexible pricing model, offering one-time purchases as well as monthly and yearly plans.
First tier (Basic API): ~18.70 USD for 10,000–50,000 requests per day Has three different versions of the API: Basic, Core, and Extended. It gets more expensive with the level of details needed.
These are just a few options, but there are other providers. If you need a more in-depth comparison then the following article will be of use: https://ahmadawais.com/best-api-geolocating-an-ip-address/
GeoLocation with Google Cloud Functions — prices and limits
Free requests: First 2 million invocations per Month (combined for all functions) Beyond 2 million: 0.40 USD per million invocations Extra cost: Compute Time
To get more in-depth pricing details visit: https://cloud.google.com/functions/pricing
Here is what Google says about their “Free Tier”:
Cloud Functions provides a perpetual free tier for compute-time resources, which includes an allocation of both GB-seconds and GHz-seconds. In addition to the 2 million invocations, the free tier provides 400,000 GB-seconds, 200,000 GHz-seconds of compute time and 5GB of Internet egress traffic per month. Note that even for free tier usage, we require a valid billing account.
All Google Cloud Functions already contain the IP address and location data, no libraries required. In the following examples we will do optimizations to ensure we are utilizing the free tier as much as possible. The important part is that we will be on the cheap side. If we don’t need extra details we will not use extra compute time.
Get the Google Cloud Function up and running
First of all, you need a Google Cloud account with a billing account setup. If you don’t have one, click here to create one. You will get 300 USD free credit to spend within 12 months.
Once your Google Cloud account is ready, we can log into the console. If you don’t already have a project, create a new one. Inside the selected project navigate to “Cloud Functions”. If you’re in rush simply click this link.
If you are using the service for the first time you will be asked to “Enable API”. Click on the “Enable API” button.
Next, click on the “Create Function”.
Once the form appears you can name the function and for now you can leave the default selections in the other fields.
Next, we will be copying the function code and defining the “Function to execute”. Let’s copy this basic function to the code editor of
We also need to copy the
Now, type in “geolocation” into the “Function to execute” field. This is the name of the function defined in exports. Once a request is made, that particular function will execute. Requests coming from any domain will be accepted. All data comes from the request itself so we will not need extra libraries.
Click “Create”, and that’s it. We are done with the basic example.
Once the function is deployed, let’s test it out. We can enter the function URL into the browser and get a response. In this particular example (your URL will be different), the URL is
This is the expected response:
Works beautifully, but we can do a quick improvement. To restrict requests which do not originate from our apps we can update our code. To edit the source go to your function, click “Edit” and select the “Source” tab.
Let’s paste this code into the
If we save this and redeploy, our whitelist will control which requests are valid. Only those coming from defined origins will be allowed.
Extend the function
The function is working, but let’s say you need more features, like receiving data about the proxy. That’s possible through the “X-Forwarded-For” header. For more details about each header please read: https://cloud.google.com/appengine/docs/flexible/nodejs/reference/request-headers
But what if you need the time zone of a specific city? Since we are using Node.js we can define Node.js dependencies and use that in the function. Let’s add city-timezones to our
Add the following code to
Let’s test this out and make a request. My response now looks like this:
We now have more data, like the time zone, population and other useful information.
Usage and advantages
Our Google Cloud Function will work great with apps written in Vue.js, React, Angular and other frameworks. It is decoupled from any back-end, and its up-time will be exceptional. After all, it’s serverless.
One other advantage is that we can use it across all our environments. Chances are that each environment requires the same data. We can use it to pre-select the app language, display localized content, do form prefilling and much more.
All done on the front-end. Since it’s on Google Cloud the pricing is acceptable and we can extend as we wish.
We published a public repository at https://github.com/ministryofprogramming/gcf-geolocation. The code described in this article is just the starting point. Feel free to clone the repository, implement new features and make a pull request so that others can benefit from your improvements.