0

i have to make an httpRequest with apex to an external site.

Problem:

  • I have to securely store an API Key somewhere in salesforce so that it's possible to use in apex
  • The header of the request must contain the key 'access-token' with the key as the value
  • no CPQ, so Merged Fields from named credentials don't seem to work
  • only administrator right for an enterprise edition.

Thanks for your help!

4
  • Named Credentials allow using custom headers while making API calls from Apex. Are you saying this is not working? Aug 6, 2020 at 11:57
  • There are pre-defined authentication methods and none of them fits the fairly simple one i described above.
    – Eleos91
    Aug 6, 2020 at 12:05
  • Are you using OAuth based or Password based authentication? Aug 6, 2020 at 12:08
  • None of them. I use a simple secret api key that is included in the header as 'access-token: <key>'. And thanks for your help
    – Eleos91
    Aug 6, 2020 at 12:10

1 Answer 1

2

Try this. It is not an elegant way to solve this problem, but perhaps it should work.

  1. Create a Named Credential.
  2. Select Identity Type as "Named Principle".
  3. Authentication Protocol as "Password Authentication".
  4. Write any meaningful Username and paste your access-token in the "Password" field.
  5. Disable"Generate Authorization Header" and enable "Allow Merge Fields in HTTP Header" in the Callout Options

In the Apex class:

req.setEndpoint('callout:namedCredentialName');
req.setHeader('access-token', '{!$Credential.Password}');
4
  • I had the same idea. But i didn't disable Generate Authorization Header, so i give it another shot. Thanks!
    – Eleos91
    Aug 6, 2020 at 12:25
  • Please select it as an answer if it works. Aug 6, 2020 at 13:14
  • Do you know a way to view the request header? debuging in salesforce won't work because merge fields are not yet replaced with the actual values from the named credential in the HTTPRequest object
    – Eleos91
    Aug 6, 2020 at 13:17
  • It worked. The rest API i was calling was buggy
    – Eleos91
    Aug 11, 2020 at 10:19

You must log in to answer this question.

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