Below is sample code. For clarity error handling has been omitted.
async function getContact() {
var headers = {"Authorization": "Token token=<%= (iparam.crm_api_key) %>"};
var url = "https://<%= (iparam.crm_domain) %>/crm/sales/api/contacts/" + window.contact_id;
var res = await _asyncRequest('get', url, headers )
}
// Common Method to make REST API calls
const _asyncRequest = (method, url, headers, body={}) => {
// Returns promise which can be held until complete with await (async/await model)
return client.request[method](url, { body: JSON.stringify(body), headers: headers } )
}
This code works fine when I’m testing in local dev Env in Chrome using ?dev=true.
But when I publish the app to a custom app, I get 400 error with message about Fully Qualified Domain required.
Then I hard code the domain and then I get error “message”:“Incorrect or expired API key”.
Then I hard code key and it all works fine.
Just before I run the request method I use console.log to make sure values are as expected. headers and url are still as I set them with the unresolved iparam syntax. I guess this is expected since the iparam is not replaced until request hits the server?
What am I doing wrong here? I’m pulling what is left of my hair out over this!
{
"crm_domain": {
"display_name": "FreshCRM Domain",
"description": "Get this from URL field of FreshCRM (domain name only).",
"default_value": "xyz.myfreshworks.com",
"type": "text",
"required": true,
"secure": true
},
"crm_api_key": {
"display_name": "FreshCRM API key",
"description": "Get this from FreshCRM Settings > API Key. This is user-specific and should be changed if user is deactivated",
"type": "text",
"required": true,
"secure": true
}
}