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 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.


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:

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.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 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");

let app = express();


  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 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:

  • 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.

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:

If you have any questions, please email us!

Have questions?


Or shoot us an email to