Sub topic 8
Authenticating callers and granting class access
This chapter assumes you can write an @RestResource class, know basic HTTP headers and status codes, and understand profiles and permission sets. It covers who can call your service and how they prove their identity. Record-level and field-level security inside the service is covered in the next chapter.
Two separate gates
Every call to an Apex REST service passes two checks before your code runs.
- Authentication: the caller presents a valid OAuth 2.0 access token or session ID.
- Class access: the user behind that token is allowed to run the Apex class.
Passing the first gate does not pass the second. A valid token for a user without class access fails with an error.
Authenticating callers
Apex REST endpoints live under your org's My Domain URL, for example https://MyDomainName.my.salesforce.com/services/apexrest/v1/Accounts/*. Callers send the token in the Authorization header:
Authorization: Bearer <access_token>You do not write any authentication code. Salesforce validates the token before your method is called. How the client obtains a token depends on the client:
| Client type | Common OAuth flow |
|---|---|
| Server-to-server integration with no user present | Client credentials flow, with a designated run-as user |
| Web application acting for a user | Authorization code flow (with PKCE for public clients) |
| Another Salesforce org | Named credential with an external credential |
| Command-line or script work | Salesforce CLI login, then reuse the token |
To use any of these flows, an admin creates a connected app or external client app in the org. This defines the client ID, the allowed OAuth scopes, and the callback URL. The scope api is enough to call Apex REST. The web and full scopes also work.
The client credentials flow deserves attention. Every call runs as the one user you assign to the app, so give that integration user the least permissions the service needs.
Granting class access
A user can call the service only if the Apex class is enabled for them. Grant it in one of two ways:
- Add the class under Enabled Apex Class Access in a permission set, then assign the permission set to the user. This is the preferred approach.
- Add the class to a profile.
Permission sets are easier to audit and let you give access to a single integration user without changing a shared profile.
A user with the Modify All Data or Author Apex permissions may still need explicit class access in some setups, so do not rely on those. Grant access explicitly.
Common failures
| Symptom | Likely cause | Fix |
|---|---|---|
| 401 with `INVALID_SESSION_ID` | Token expired, revoked, or sent to the wrong domain | Request a new token. Check the host in the URL. |
| 403 or an insufficient access message | User lacks class access, or the app's OAuth scope is too narrow | Add the class to a permission set. Check scopes. |
| 404 with `NOT_FOUND` | The `urlMapping` does not match the path, or the class is not deployed | Compare the URL with the mapping, including the `/services/apexrest` prefix. |
Checking the caller in code
The handler runs as the authenticated user. You can read that identity with UserInfo:
@HttpGet
global static String whoAmI() {
return UserInfo.getUserName();
}Use this for logging or auditing. Do not use it to decide access. Class access, sharing rules and permissions already control that.
Exercise
A client receives a valid token through the client credentials flow, but every call to your service fails with an access error. The token was issued without problems. What should you check first, and how do you fix it?
Show answer
Check whether the run-as user assigned to the connected app has access to the Apex class. Authentication succeeded, so the failure is at the class access gate. Add the class under Enabled Apex Class Access in a permission set and assign that permission set to the run-as user.