0

In order to do something, we need to make call outs to an external server where the URL takes the format:

http://www.example.com/path/{external_id}/doSomething?api-key={api-key}

The external-id would be specific to and stored in individual records and swapped out for each call.

The api-key would normally be a "fixed" value, although it might (of course) occasionally be updated. And our production org would call a different server instance than our sandboxes (which may or may not share the test server) and (of course) this key would be different than production.

(In the medium/long run, we might change the API since putting the key in the URL doesn't seem like a great idea... but til then...)

1 Answer 1

2

Using a custom authentication scheme negates part of the benefit of using Named Credentials, but you can still do so. Assuming the endpoint is:

http://www.example.com/path/

Your code would look like:

req.setEndpoint(
  String.format('callout:example/{0}/doSomething?api-key={1}',
    new String[] { externalId, apiKey }));

Ideally, you should move to OAuth or even Basic Authentication, which reduces your complexity to just:

req.setEndpoint(
  String.format('callout:example/{0}/doSomething',
    new String[] { externalId }));

And eliminates the need to store the API key in a custom setting/custom label/etc since it will be managed by the system, which is part of the benefit of using Named Credentials.

1
  • How do add the API key from the named credential password?
    – Monish
    Commented Nov 7, 2018 at 10:45

You must log in to answer this question.

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