How can I determine the usage type of an IP address in Deno?

To determine the usage type of an IP address using IP2Location.io in Deno, you can use the IP2Location.io API to retrieve geolocation and related data. The usage type field specifies how the IP is being used. For usage types supported, please see API documentation for more info.

  1. We'll assume that you have already installed Deno and we won't cover that here. You will also need a paid subscription to the Starter plan or higher. Subscribe now!
  2. Save the below code into a file called test.js on your computer.
    JavaScript
    
    import * as https from "node:https";
    
    var key = "YOUR_API_KEY";
    var ip = "8.8.8.8";
    
    let url = "https://api.ip2location.io/?key=" + key + "&ip=" + ip +
      "&format=json";
    
    let d = "";
    let req = https.get(url, function (res) {
      res.on("data", (chunk) => (d = d + chunk));
      res.on("end", function () {
        if (res.statusCode == 200) {
          let data = JSON.parse(d);
          if (data.usage_type) {
            console.log(
              "The usage type for IP " + data.ip + " is " + data.usage_type + ".",
            );
          } else {
            console.log(
              "ERROR: The usage_type field requires a paid subscription to the Starter plan or higher.",
            );
          }
        } else if (res.statusCode == 400 || res.statusCode == 401) {
          if (d.includes("error_message")) {
            console.log("ERROR: " + JSON.parse(d).error.error_message);
          } else {
            console.log(d);
          }
        } else {
          console.log(d);
        }
      });
    });
    
    req.on("error", function (e) {
      console.log(e);
    });
    			
  3. In the command line, run the below command.
    Bash
    
    deno --allow-env --allow-net test.js
    			

This script will output the usage type of specified IP address. Make sure to replace 8.8.8.8 with the IP address you want and replace YOUR_API_KEY to your own API key.

Other Languages

Unlock Location Insights For Free

Empower your applications with accurate IP geolocation information now.

Try It for Free