I wrote my own apex class with the following code:

    public static String getSessionId(){
        return UserInfo.getSessionId();

This code works when I import it into my Lightning component. I retrieves a session id but it is invalid. If i try to do the same thing inside an apex page it works perfectly and it gives me a working session id. How can I retrieve a working session id?

  • 1
    What are you trying to do with that session ID? Lightning session IDs cannot be used to authenticate to SFDC to, for example, make metadata API calls. Commented Mar 31, 2019 at 18:56
  • Custom app at my company requires the session id for authentication with our services. Commented Mar 31, 2019 at 18:57
  • The session you obtain from lightning cannot be used to authenticate against any Salesforce org. The session in itself is otherwise valid. Commented Mar 31, 2019 at 22:11

4 Answers 4


From this Article I was able to extract this method here:

First create a VisualForce page with the name: currentUserInfoCtrl and then insert into it:

<apex:page >

Then create a new Apex class that will reference the Apex Page

    public static String fetchUserSessionId(){
         String sessionId = '';
         // Refer to the Page
         PageReference reportPage = Page.GetSessionIdVF;
         // Get the content of the VF page
         String vfContent = reportPage.getContent().toString();
         System.debug('vfContent '+vfContent);
         // Find the position of Start_Of_Session_Id and End_Of_Session_Id
         Integer startP = vfContent.indexOf('Start_Of_Session_Id') + 'Start_Of_Session_Id'.length(),
         endP = vfContent.indexOf('End_Of_Session_Id');
         // Get the Session Id
         sessionId = vfContent.substring(startP, endP);
         System.debug('sessionId '+sessionId);
         // Return Session Id
         return sessionId;

I was able to get a valid session token using this method, and then attaching this apex class to my Lightning Component.


The sessionId obtained from auraEnabled cannot be used to make metadata api calls or the tooling api callouts .

Here are couple of techniques that you can use

  1. Set up Named Credential and Auth Provider to obtain sessionId . This is documented in the blog here


2.Use a proxy visualforce to obtain the SessionId using the global variable .It is documented here.

You can also use a service component that encapsulates the proxy vf page .This blog can help you further .

  • I tried to use technique 2 (using proxy visualforce page and it worked fine in unmanaged package, but got (org.apache.commons.httpclient.RedirectException: Maximum redirects (100) exceeded) error in managed package. Any idea, why
    – lambad
    Commented Jun 19, 2019 at 9:28

I followed the steps mentioned here - https://www.gscloudsolutions.com/blogpost/Using-Named-Credentials-with-the-Apex-Wrapper-Salesforce-Metadata-API-apex-mdapi?blogpost=true

and It worked for me. One modification - While creating connected app, in "Selected OAuth Scopes" select all the options shown in the image here - enter image description here


There is a better version of VF to fetch Session Id without Apex controller

VF Page: UserSessionPage.page

<apex:page contentType="application/json">

Apex Method:

public static String getSessionId() {        
    return  Page.UserSessionPage.getContent().toString();
  • omfg this worked!! 😂 You saved me!! 🙏🙌 Commented Oct 27, 2023 at 20:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .