Outbound REST Integration with API key

tanvikarale
Tera Contributor

Hii,
I’m working on a scoped application where I need to trigger an outbound REST call to a third-party API. The third-party API expects an API key via an HTTP header like this:
x-api-key: <actual_api_key>

I’ve stored this API key securely using Connection & Credential Alias of type Credential, and the Credential is of type API Key Credential.

I have tried below but not working for me:
1. var key = credGR.authentication_key.getDecryptedValue() :  This fails in a scoped application due to cross-scope access restrictions.
2. SecureEncrypter. global.SecureEncrypter() : Not available in PDI

I want to trigger this REST call from a Business Rule using RESTMessageV2.

Is there any supported way to:

  • Get the decrypted API key value in script from the credential alias?

  • Or, better, is there a way to use the credential alias in script-based RESTMessageV2 (similar to how it works in IntegrationHub)?

 

2 REPLIES 2

Bhavya11
Kilo Patron

Hi @tanvikarale ,

 

To access this key programmatically, retrieve it via GlideRecord and then decrypt it to its original text form using GlideEncrypter().decrypt(). If you're working within a scoped application, establish a custom scoped Script Include that can interact with global Script Includes. Once you have the decrypted key, include it in your message header.

 

 

Thanks,

BK

Amit Verma
Kilo Patron
Kilo Patron

Hi @tanvikarale 

 

You can also approach this using a Password2 type system property. You can create a Password2 type System Property in your scoped application and store the API key within it. To decrypt and use it, you can make use of below line of code. Pass apiKey variable to your REST Message header as shown below:

var apiKey = gs.getProperty('name of your system property');
var request = new sn_ws.RESTMessageV2('', ''); // Append your REST Message details here
request.setRequestHeader('',apiKey); // Append your Header here

 

Thanks and Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.