I have to provide OAuth access token to a JS SDK, The JS is uploaded in Static resource and referenced in visualforce

JS_SDK.accessToken = '{!my_access_token}';

From Named credential documentation, I understand that in apex we can construct custom headers by doing

req.setHeader('Authorization', 'access_token {!$Credential.OAuthToken}');

I have defined the named credential, authenticated and checked: Allow Merge Fields in HTTP Header & Allow Merge Fields in HTTP Body fields.

Is there a way to refer the {!$Credential.OAuthToken} in apex as a variable and then refer in visualforce.

Does the merge field resolution only happen when the request is sent out ?

1 Answer 1


Yes, the merge field is resolved only upon callout. This is an intentional design choice to avoid leaking the credentials unnecessarily. You would need to make the callout in Apex directly in order to take advantage of this feature.

  • Thanks sfdcfox! In that case I have to handle the OAuth flow on my own, to get hold of the access token,
    – Pavan
    Apr 12, 2020 at 5:14

You must log in to answer this question.

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