+ Start a Discussion
Ashwin KcAshwin Kc 

How to capture IP Address in lightning Component


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 

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}"/>

Lightning Js:
	doInit : function(component, event, helper) {

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

        // 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());
                // 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: " + 
                } 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.

Controller :
public class demoCalloutController {

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


  //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
    catch(System.CalloutException e) {
	//Exception handling goes here....
    return res.getBody();


Lightning App:
<aura:application >
    	<!-- <shivaKalinga:LightningImageComponent ></shivaKalinga:LightningImageComponent>
	<shivaKalinga:ApiAccess ></shivaKalinga:ApiAccess>
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?