Hello, I am experiencing an issue with API secrets...
# suitescript
j
Hello, I am experiencing an issue with API secrets. I am aiming to utilize a securely tokenized API secret within SuiteScript. Could someone kindly provide me with guidance on this matter? Below is the code in question. Thanks in advance.
Copy code
define(['N/https'], (https) => {     
    const key = https.createSecureString({
        input: 'custsecret_test_key'
    });

    const header = https.createSecureString({
        input: 'Bearer '
    });

    header.appendSecureString({
        secureString: key
    });

    <http://https.post|https.post>({
        url: <https://api.xxx.com/>,
        headers: {
            'Authorization': header,
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    });

});
Error
Copy code
{
"error": {
"message": "Invalid API Key provided: custsecr**************_key",
"type": "invalid_request_error"
}
👀 1
m
Have you tested by logging
header
variable to see what it's creating?
j
As you know, we cant log the secret key
m
Sorry I wasn't aware. Makes sense though.
j
Thanks
m
When I look at
https.createSecureString
spec it doesn't seem to allow for loading directly a value from a secret. It seems for that portion you might want to use
https.createSecretKey
Something like.
Copy code
const key = https.createSecretKey({
        secret: 'custsecret_test_key'
    });
Also it looks like appending should look like.
Copy code
header.appendSecureString({
        input: key
    });
j
Oh, maybe, I will try
But what I share is the answer in suiteanswer and stackoverflow
You can try the following it looks like from suiteanswers. You might need the curly braces for it to fetch the secret.
Copy code
const header = https.createSecureString({
    input: 'Bearer {custsecret_test_key}'
});
m
Yep. curly braces are required.
m
I missed this part of the input.
j
Works well
Thanks very much
223 Views