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

In Apex, is it possible to determine if a User is logged in through the API?

I'm exploring some situations where we might want different actions depending on if a user is logged in through the UI versus connecting via the API. Is there any way in Apex to determine this?

I dont think there is a direct method.
One alternative would be to query the LoginHistory object of the Salesforce filtering by UserID and ordering by the Login Time in Descending order.
This way you can then take the first record as this would be the last login of the user.
Once you get the record you can get the information through various fields - APIType, Browser, Application etc.

More info:

Satish Kumar
Please mark my answer as a solution if it was helpful so it is available to others as a proper solution.
If you felt I went above and beyond, please give me Kudos by clicking on the star icon.



Currently It is not possible to check the user's status weather he is logged in or not. 

You can use the following calls to get other information about the user.



There is a workaround that we have used in the past, but that was for a very specific requirement, 

We created a landing page for the user, which was a visualforce page, and onload of the visualforce page, we made an entry into a custom object to store the last login time : 

Similar to this post here  :



But ultimately you are looking for this object to check when the user has loggedin, (No Logout time available though)


Sorry, could not be of more help!

If you do find a way, do share it with us. 



Gunish Rai Chawla



If this post was helpfull, do remember to give me Kudos!





Anmol Bali 2Anmol Bali 2
HI Guys,

I have found a way to detect impersonated user, please visit the below blog
Please mark the answer if you fill its right.