I want to receive daily limit from apex. There is my code

    public static HttpResponse getLimits() {
    HttpRequest req = new HttpRequest();
    Http http = new Http();
    String url = System.URL.getSalesforceBaseURL().toExternalForm() +'/services/data/v44.0/limits';
    req.setHeader('Authorization', 'OAuth(OR Bearer)  ' + UserInfo.getSessionId());
    HttpResponse ddd=http.send(req);
    return http.send(req);

It doesn't work:

[{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]. Is it possible to do, what I want?

  • Yes, it's possible to make a callout to get limits details provided you have a valid Session Id that's API-enabled. In what context are you making this call? (Note that Workbench is not involved with the Limits API).
    – David Reed
    Dec 13, 2018 at 15:09
  • I executed it in 'execute anonymous window' to test how it's works. Dec 13, 2018 at 15:27

2 Answers 2


Firstly, if you can wait until the Spring '19 release you should be getting native support for this without a callout via OrgLimits. Example from pre-release org. This was also highlighted in the "Everything That's Awesome with Apex" Dreamforce 2018 session.

However, if you need to make the callout with the current release, this works:

HttpRequest req = new HttpRequest();
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
req.setHeader('Content-Type', 'application/json');
// https://fop-dev-ed.my.salesforce.com
req.setEndpoint(System.URL.getSalesforceBaseURL().toExternalForm() +'/services/data/v45.0/limits');
Http h = new Http();
HttpResponse res = h.send(req);

The only catch here would be the context where you are making the callout from. UserInfo.getSessionID() isn't always available, such as in batch or scheduled Apex. In that case you would be better with a named credential to establish a valid session.

  • Thank you for response. Can y tell me, where can I find some information about this feature in Spring '19'? Dec 14, 2018 at 8:13
  • I've added a link to an example and the Dreamforce talk where the feature was mentioned. Dec 14, 2018 at 8:25
  • Thanks a lot. Another question. I added this code into a page controller. And it still doesn't work. I don't understand why( Dec 14, 2018 at 10:19
  • @andreyatake How is the page controller being called? Is it from Visualforce, or something else? Does the user making the call have access to the API? Dec 16, 2018 at 18:45
  • 1
    N.B. sessionId is available as of winter 19 for async transactions including batch except, of course, the dastardly Automated Process user.
    – cropredy
    Dec 18, 2018 at 1:00

I figured out, that the reason of error was setting 'Lock sessions to the IP address from which they originated' in 'Session setting'. You can just turn it off(you have to log out to check result), or do all steps from this article https://help.salesforce.com/articleView?id=000205360&type=1

You must log in to answer this question.

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