Getting IP address of user

Hello everyone, I want to show the IP address of the user on a page.
I tried everything to get it, but when I ask for it with an external api, the ip is undefined:

My code (on the page code):

The function in public files (I also tried on the page code, it’s the same result):

If you want to try it by yourself: team-hc. com/vote
I tried many api, but the result is the same.

Why I get “Undefined” ?

You are getting undefined because:

  1. You are not properly returning the results from the getIP() function. You need to add return before the fetch .

  2. When you call getIP() , you need to handle the returned Promise using either await or the .then() function.

See the following for more information about Promises:

I would suggest trying out Find Visitor Location to see a fully working example of what you are trying to accomplish.

Thanks, it’s working, but now I want to substract the ’ " ’ character of my ipadress variable


How can I do it ?

My code:

Not really sure what’s in the string, but if you want to get rid of the quotes, then something like this might help:

ipaddress.replace('"', "");

There are lots of other ways such as regex, substr, etc.

@yisrael-wix That removes only one quote, I want to get rid of the quotes in the ipaddress variable, so I can use it in string without quotes.

import { fetch } from ‘wix-fetch’ ;

$w ( “#button3” ). onClick ( ( event ) => {
$w ( “#button3” ). label = “Chargement” ;
[fetch](fetch('https://extreme-ip-lookup.) [(](fetch('https://extreme-ip-lookup.) ['https://extreme-ip-lookup.](fetch(‘https://extreme-ip-lookup.) com/json’ , {
method : ‘get’
})
. then (( httpResponse ) => {
if ( httpResponse . ok ) {
return httpResponse . json ();
}
})
. then (( json ) => {
const ipaddress = JSON . stringify ( json . query );

// What can I put put here to remove quotes of {ipaddress} ?

$w ( ‘#text156’ ). text = Controle de l'adresse IP: ${ ipaddress };
$w ( “#button3” ). label = "IP: " + ( ipaddress );
console . log ( ipaddress )
return ipaddress ;

});
});

//...
.then((json)=>{
const ipaddress= json.query;
//

Ok found it:

var ip = ipaddress . replace ( /"/ g , ‘’ );

By the way, I think you can do it with pure Wix w/o calling this external service, like this:

//backend/http-functions.js
import {ok} from 'wix-http-functions';
export const get_ip = (req) => ok({body: req.ip});
//frontend:
$w("#button3").onClick( (event) => {
 $w("#button3").label = "Chargement";
 fetch('https://my-domain.com/_functions/ip', {
 method: 'get'
 })
 .then((httpResponse) => {
 if (httpResponse.ok) {
 return httpResponse.text();
 }
 })
 .then(ip => {
//etc...
})
1 Like

Hello - using extreme-ip-lookup, our site is able to display the visitor’s city name but that text disappears immediately. This didn’t used to happen before, and I haven’t touched the code from last time. Any change in wix code causing this?
this is my code:
// For full API documentation, including code examples, visit Velo API Reference - Wix.com

import {

fetch 

}
from ‘wix-fetch’ ;

$w . onReady ( function () {

fetch ( 'https://extreme-ip-lookup.com/json' , { 

        method :  'get' 

    }) 

    . then (( httpResponse ) => { 

        if  ( httpResponse . ok ) { 

            **return**  httpResponse . json (); 

        } 

    }) 

    . then (( json ) => { 
        
        const  ipaddress  =  json . query ; 
        let  city  =  json . city ; 
        if  ( city  !==  **null**  &&   city  !==  '' ) 
        { 
            $w ( "#text43" ). text  =  city ;  //set city name for text27 in frontend. 
        
        } 
        **else** 
        { 
            //return ''; 
            $w ( "#text43" ). text  =  "" ;  //set city name is blank or null then it will show blank. 
        } 
        // return ipaddress; 
    }); 

});

export function text43_viewportEnter ( event ) {
//Add your code for this event here:
}

You can retrieve the IP address of a user using the request object, which contains information about the current HTTP request.

Dealing with APIs can be tricky sometimes, especially when it comes to getting user IP addresses.
Reminds me of a similar situation I faced when I was setting up a website. I struggled to retrieve user IPs until I found a reliable booter service that provided accurate data without any hiccups.
So, don’t give up just yet! Keep exploring different approaches, and you’ll eventually find the solution that works best for you. Good luck!