Almost free location API
1 min read

Almost free location API

In this blog post i try to get around pricey User IP location services using a cloud function and with the help of tracked data by our friends at Google.

Below is a nodejs script to deploy on google cloud functions.

const cors = require('cors')
const cityTimezones = require('city-timezones');

// Set `useWhitelist` to `false` if you want to accept all requests.
const config = {
  useWhitelist: false
}

// Define from which origins requests are allowed.
const whitelist = [
  'https://YOURWEBSITE.com'
];

// Parse the whitelist and decide if the request is allowed.
const corsOptionsWhitelist = function (req, callback) {
  var corsOptions;

  if (whitelist.indexOf(req.header('Origin')) !== -1) {
    corsOptions = { origin: true }
  } else {
    corsOptions = { origin: false }
  }

  callback(null, corsOptions);
}

// Options when not using the whitelist.
const corsOptions = {
  origin: true
}

// Handle the response within this function. It can be extended to include more data.
function _geolocation(req, res) {
  // res.header('Cache-Control','no-cache');

  const data = {
    country: req.headers["x-appengine-country"],
    region: req.headers["x-appengine-region"],
    city: req.headers["x-appengine-city"],
    cityLatLong: req.headers["x-appengine-citylatlong"],
    userIP: req.headers["x-appengine-user-ip"],
    cityData: cityTimezones.lookupViaCity(req.headers["x-appengine-city"])
  }

  res.json(data)
};

// Export the cloud function.
exports.geolocation = (req, res) => {
  const corsHandler = config.useWhitelist ? cors(corsOptionsWhitelist) : cors(corsOptions);

  return corsHandler(req, res, function() {
    return _geolocation(req, res);
  });
};

This is the package.json contents

{
  "name": "gfc-geolocation",
  "version": "0.0.1",
  "dependencies": {
    "cors": "^2.8.4",
    "city-timezones": "^1.0.5"
    }
}

Google provides 2 million cloud functions invocation free per month. So effectively you are getting user location tracking at fraction of a price.

Disclaimer: This wonderful hack was contributed by Amit Kumar

Enjoying these posts? Subscribe for more