The MailStack Api (Email Testing) is now available!

How to get your public IP address with Node.js

The Problem

In automated test projects, it's not uncommon to need to get your public address in order to execute specific tasks, handle authorization processes, etc.

A Solution

In these tips, I share with you some Node.js code snippets to help you get the public IP address quickly.

Method N°1: Using curl with child_process

const { execSync } = require("child_process");

const cmd = `curl -s http://checkip.amazonaws.com || printf "0.0.0.0"`;
const pubIp = execSync(cmd).toString().trim();

console.log(`My public IP address is: ${pubIp}`);

Method N°2: Using Node.js http client (axios)

const axios = require("axios");

(async () => {
  const url = "https://checkip.amazonaws.com/";
  const response = await axios(url);
  console.log(`My public IP address is: ${response.data.trim()}`);
})();

You can also use our free online tool here