Fetch IP Address with JS using Fetch Function

Fetch IP Address with JS using Fetch Function

I was curious about how I could get my IP Address by using JavaScript so I figured why not try to figure it out. Utilizing the following fetch function we can get an accurate IP Address on the machine we are using. If we use a VPN it will accurately show us our spoofed address as well.

Be sure you have some sort of CORS chrome extension as well otherwise you will get an error. This is the one I use from the chrome web store:

Screen Shot 2022-05-23 at 3.31.53 AM.png

Here is the code:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Get IP Address</title>
</head>

<body>
    <script>
         function getIPFromAmazon() {
            fetch("https://checkip.amazonaws.com/").then(res => res.text()).then(data => alert(`Your IP Address is ${data}`))
        }
        getIPFromAmazon();
    </script>
</body>
</html>

This code will effectively get your IP Address and display it in an alert message. Feel free to play around with the code and try to figure out other ways to use the function.

We can also use the terminal to get the same functionality:

curl ipinfo.io