Using The IP API with Node.js

The IP API is a powerful tool for developers looking to enhance their applications with accurate IP geolocation data. In this article, we'll walk through integrating The IP API into a Node.js application, providing a complete example to get you started.

Prerequisites

Before you begin, ensure you have the following:

  • Node.js installed on your system (version 12 or higher is recommended).
  • An API key from The IP API. Sign up if you haven’t already.
  • A basic understanding of JavaScript and Node.js.
  • Easy Integration: Designed for developers, The IP API offers intuitive documentation and straightforward setup.

Set Up Your Project and Create a Simple Script

Create a new directory for your project and navigate into it:

mkdir api-node-example && cd api-node-example

Now, let’s write a script to query The IP API and retrieve geolocation data for a given IP address.
Create a new file named app.js in your project directory. Open app.js and add the following code:

var https = require("https");

var options = {
  host: "theipapi.com",
  port: 443,
  path: "/v1/ip/8.8.8.8?api_key=YOUR_API_KEY",
  method: "GET",
};

https
  .get(options, (resp) => {
    let data = "";

    // A chunk of data has been received.
    resp.on("data", (chunk) => {
      data += chunk;
    });

    // The whole response has been received. Print out the result.
    resp.on("end", () => {
      console.log(JSON.parse(data));
    });
  })
  .on("error", (err) => {
    console.log("Error: " + err.message);
  });

Run Your Script

Save your changes and run the script:

node app.js
If successful, you’ll see geolocation data for the IP address in the console, similar to this Sample Error Response:
{
  "status": "OK",
  "body": {
    "asn": {
      "asn": 15169,
      "asn_description": "GOOGLE, US",
      "country": "US",
      "created": "2005-11-23T02:48:10.000Z",
      "network": "8.8.8.0/24",
      "org_name": "Google LLC",
      "rir": "ARIN",
      "updated": "2019-10-31"
    },
    "company": {
      "address": "1600 Amphitheatre Parkway, Mountain View, CA, US",
      "name": "Google LLC",
      "network": "8.8.8.0 - 8.8.8.255",
      "route": "8.8.8.0/24"
    },
    "ip": "8.8.8.8",
    "location": {
      "city": "Mountain View",
      "country": "United States of America",
      "country_code": "US",
      "latitude": 37.405992,
      "longitude": -122.078515,
      "region": "California"
    }
  },
  "response_time_ms": 1
}

Optimize API Usage

Now that you’ve integrated The IP API into your application, start thinking about:

  • Caching: Store frequent IP lookups locally to reduce API calls and improve performance.
  • Rate Limits: Check your plan’s rate limits and optimize requests accordingly.
  • Batch Requests: Use batch endpoints to process multiple IP addresses in a single request.

For further assistance, visit our support center or contact us directly. We’re here to help you make the most out of The IP API.

Happy coding!

Ready To Start Using The IP API?

Unlock the power of accurate and reliable IP geolocation data with The IP API. Effortlessly integrate our API to enhance personalization, optimize performance, and improve security. Create your account now!