function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ashwin KcAshwin Kc 

How to capture IP Address in lightning Component

Hi 

I have scnerio where I need to capture IP address of current User in Lightning Community, is any way standard way  where I can capture IP address inside Lightning Components.
I have seen third party tools like - https://www.ipify.org/ to get Ip address 
Any recommendations are appreciated 

Thanks 
Shiva RajendranShiva Rajendran
Hi Aswin,
I tried with your api but i didnt response so used "https://www.trackip.net/ip?json" api instead
Make sure before you execute ,create a remote site for the above url

Here is the code

Lightning component :
<aura:component controller="demoCalloutController">
    <aura:attribute name="responseTextValue" type="String" default="hello" />
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    {!v.responseTextValue}
</aura:component>

Lightning Js:
 
({
	doInit : function(component, event, helper) {
	helper.makeCallout(component);
    }
})

Lightning Js Helper :
({
	makeCallout : function(component) {
			var action = component.get("c.getIpValue");
        action.setParams({});

        // Create a callback that is executed after 
        // the server-side action returns
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                // Alert the user with the value returned 
                // from the server
            //    alert("From server: " + response.getReturnValue());
				component.set("v.responseTextValue",response.getReturnValue());
                // You would typically fire a event here to trigger 
                // client-side notification that the server-side 
                // action is complete
            }
            else if (state === "INCOMPLETE") {
                // do something
            }
            else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " + 
                                 errors[0].message);
                    }
                } else {
                    console.log("Unknown error");
                }
            }
        });

        // optionally set storable, abortable, background flag here

        // A client-side action could cause multiple events, 
        // which could trigger other events and 
        // other server-side action calls.
        // $A.enqueueAction adds the server-side action to the queue.
        $A.enqueueAction(action);
    }
})

Controller :
 
public class demoCalloutController {

    
    @AuraEnabled
public static String getIpValue() {
HttpRequest req = new HttpRequest();

    req.setMethod('GET');

  //Set HTTPRequest header properties
  req.setHeader('content-type', 'application/json');
 
  req.setEndpoint( 'https://www.trackip.net/ip?json');

  //Set the HTTPRequest body	
   // req.setBody(); 	

  Http http = new Http();
   HTTPResponse res=null;
   try {
 
        //Execute web service call here		
       res = http.send(req);	

        //Helpful debug messages
        
        System.debug(res.getBody());
        System.debug('STATUS:'+res.getStatus());
        System.debug('STATUS_CODE:'+res.getStatusCode());
		
			}
    catch(System.CalloutException e) {
	//Exception handling goes here....
		}		
    return res.getBody();
	}

}

Lightning App:
<aura:application >
    	<!-- <shivaKalinga:LightningImageComponent ></shivaKalinga:LightningImageComponent>
-->
	<shivaKalinga:ApiAccess ></shivaKalinga:ApiAccess>
</aura:application>
Let me know if you need further help.
Thanks and Regards,
Shiva RV
Admin DNAAdmin DNA
Hi Shiva, It returns ip of salesforce server, is there a way to get public
ip of a guest user?