Express.js IP Geolocation client library Express.js IP Geolocation client library

How to Perform a GeoIP Lookup in Express.js

If you’re building a website (or API) using Express.js, it’s often useful to know where your visitors are coming from: the US, the EU, someplace else? The process of locating a web user is typically referred to IP geolocation, and unfortunately, it isn’t simple.

The reason it isn’t easy to get IP geolocation data is that there is no standard mapping of IPs -> location data. Most companies get this data by purchasing it from GeoIP aggregators that piece together lots of different bits of information to build an accurate database of IP geolocation data.

GeoIP data is typically comprised of:

In short: it’s basically impossible to get IP geolocation data without going through a GeoIP aggregator.

Today, I’ll walk you through using the incredibly simple express-simple-geoip developer library I created which makes performing GeoIP lookups a breeze in Express.js.

Create a IP Geolocation API Lookup Account

The first thing you'll need to do to use the express-simple-geoip library is go create a free IP Geolocation API account: https://geoipify.whoisxmlapi.com/signup.

IP Geolocation API is one of the largest and least expensive GeoIP providers. You can use the IP Geolocation API service to perform 1,000 free GeoIP queries each month, or you can pay them a flat fee of $27 per month for 100,000 queries. Extra tariff plans available here.

Once you've created and logged into your IP Geolocation API account, you'll need to view your account's products page and copy your API key — you will need this later to make GeoIP queries.

Install the express-simple-geoip Package

Now that your account is setup, the next thing you need to do is install the express-simple-geoip NPM library. From the command line, run the following command:

            
$ npm install express-simple-geoip
            
            

This will download and install the latest release of the express-simple-geoip package from NPM.

Perform a GeoIP Lookup Using express-simple-geoip

Now that you have both an account and the express-simple-geoip package installed, let’s take a look at some code you can run to lookup the physical address of any IP address you want.

Here’s a simple Express.js app that only contains a single endpoint, `/test`, which returns a simple hello world response:

            
"use strict";

const express = require("express");
const GeoIP = require("express-simple-geoip");

let app = express();

app.use(GeoIP("<your-api-key-here>"));

app.get("/test", (req, res) => {
  res.json({ hello: "world" });
});

app.listen(3000);
            
            

If you put this code into a `server.js` file and run it, you should see a hello world response when you visit the `/test` endpoint in your browser.

The `app.use` line initializes the GeoIP middleware so that every time a new request comes into your Express.js server, a GeoIP lookup will be performed and a new `req.geoip` object will be tacked onto the request.

To access the GeoIP data in your code, you can simply reference the `req.geoip` object like so:

            
"use strict";

const express = require("express");
const GeoIP = require("express-simple-geoip");

let app = express();

app.use(GeoIP("<your-api-key-here>"));

app.get("/test", (req, res) => {
  res.json({ hello: "world", geoip: req.geoip });
});

app.listen(3000);
            
            

If you run this new server and visit the `/test` URL in your browser, you should see a new response that looks something like this:

            
{
  "hello": "world",
  "geoip": {
    "country": "US",
    "region": "California",
    "city": "Mountain View",
    "lat": 37.40599,
    "lng": -122.078514,
    "postalCode": "94043",
    "timezone": "-08:00"
  }
}
            
            

As you can see, the `req.geoip` data object contains all of the requester’s GeoIP data! Pretty neat, right? The `req.geoip` object contains everything you need to know about the requester’s physical location.

How to Use GeoIP Data

Now that you’ve seen how simple it can be to get GeoIP lookup functionality working in your Express.js apps, here are some ideas of ways you can use GeoIP data in your projects and services:

  • Detect a user’s country when they visit your site and provide a customized experience for them (change the language of the page, show certain ads, types of currency, etc.)
  • Block users from certain locations from visiting your website. Let’s say you are building a video streaming service like YouTube and you only have the rights to show certain videos to users in the US. In this case, having GeoIP data could help you detect a user’s location so you could filter any non-US users.
  • Reduce fraud and risk. If you notice a lot of malicious traffic coming from a specific country, temporarily blocking visitors from that location can be a quick way to avoid fraud and other issues.

Wrap Up express-simple-geoip

Performing GeoIP lookups can be tricky, but express-simple-geoip in conjunction with the IP Geolocation service makes it simple and cheap. By using the new express-simple-geoip library you can easily build and manage GeoIP lookups for even the largest enterprise sites.

To learn more, go check out the express-simple-geoip library on GitHub where you can find all the docs and more in-depth information: https://github.com/whois-api-llc/express-simple-geoip

If you have any questions, please email us!

Have questions?

We work hard to improve our services for you. As part of that, we welcome your feedback, questions and suggestions. Please let us know your thoughts and feelings, and any way in which you think we can improve our product.

For a quick response, please select the request type that best suits your needs. For more info regarding the request types, see the Contact us page.

Or shoot us an email to