Retrieving the client request ip address Retrieving the client request ip address express express

Retrieving the client request ip address


You can get this information by fetching it from an open IP

https://api.ipdata.co/

fetch("https://api.ipdata.co")  .then(response => {    return response.json();   }, "jsonp")  .then(res => {    console.log(res.ip)  })  .catch(err => console.log(err))


It seems like https://api.ipdata.co doesn't work anymore, even when specifying a key. I ended up using (typescript):

private getMyIp() {  fetch('https://api.ipify.org?format=json').then(response => {    return response.json();  }).then((res: any) => {    this.myIp = _.get(res, 'ip');  }).catch((err: any) => console.error('Problem fetching my IP', err))}

This is a good reference for other services: https://ourcodeworld.com/articles/read/257/how-to-get-the-client-ip-address-with-javascript-only


This works!

async componentDidMount() {          const response = await fetch('https://geolocation-db.com/json/');    const data = await response.json();    this.setState({ ip: data.IPv4 })    alert(this.state.ip)}

use it in jsx as

{this.state.ip}