I am using as reference this documentation: https://developer.salesforce.com/docs/atlas.en-us.uiapi.meta/uiapi/ui_api_resources_favorites.htm And it says that by doing an HTTP GET request to this url "/ui-api/favorites" it should retrieve "all of a user’s favorites.". But it seems it doesn't work. I tried the following code in Execute Anonymous and received the error "System.HttpResponse[Status=Unauthorized, StatusCode=401]"

This is the code I tried in the sandbox, by the way I included some favorites to my user so it should retrieve something.

HttpRequest req = new HttpRequest();
string host = 'https://AAAAA.my.salesforce.com';

String endpoint = host +'/services/data/v45.0/ui-api/favorites';

Http http = new Http();
HTTPResponse res = http.send(req);


Does anyone know if the Favorite API is still in use? Thanks!

1 Answer 1


In order to call standard API you have to authenticate yourself, indeed the HTTP Status Code 401

indicates that the client request has not been completed because it lacks valid authentication credentials for the requested resource

Since you're trying it in Anonymous console, you could use your own session id and you can get it calling UserInfo.getSessionId().
Once you retrieved it, you must set the Authorization header in the HTTPRequest: req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());

By the way, instead of hardcoding your host, you can call URL.getSalesforceBaseUrl().toExternalForm()

The whole script will look like this:

String host = URL.getSalesforceBaseUrl().toExternalForm(); // https://yourdomain.salesforce.com
String path = '/services/data/v55.0/ui-api/favorites';
String endpoint = host + path;

HttpRequest req = new HttpRequest();
req.setHeader('Content-Type', 'application/json');
req.setHeader('Accept', 'application/json');
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());

Http http = new Http();
HTTPResponse res = http.send(req);

Integer statusCode = res.getStatusCode();
String responseBody = res.getBody();

The expected status code is 200 and the body will hold a list of Favorite

You must log in to answer this question.

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