Express.js | Developer libraries | IP Geolocation API | WhoisXML API

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

How to Perform a IP Geolocation 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 IP Geolocation aggregators that piece together lots of different bits of information to build an accurate database of IP geolocation data.

IP Geolocation data is typically comprised of:

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

Today, I’ll walk you through using the incredibly simple express-simple-geoip developer library I created which makes performing IP Geolocation 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:

IP Geolocation API is one of the largest and least expensive IP Geolocation providers. You can use the IP Geolocation API service to perform 1,000 free IP Geolocation 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 IP Geolocation 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 IP Geolocation 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 IpGeolocation = require("express-simple-geoip");

let app = express();


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


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 IpGeolocation middleware so that every time a new request comes into your Express.js server, a IP Geolocation lookup will be performed and a new `req.geoip` object will be tacked onto the request.

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

"use strict";

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

let app = express();


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


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 IP Geolocation data! Pretty neat, right? The `req.geoip` object contains everything you need to know about the requester’s physical location.

How to Use IP Geolocation Data

Now that you’ve seen how simple it can be to get IP Geolocation lookup functionality working in your Express.js apps, here are some ideas of ways you can use IP Geolocation 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 IP Geolocation 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 IP Geolocation 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 IP Geolocation 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:

If you have any questions, please email us!