Hi everyone,
I am trying to use request template to fetch some data from freshsales API but somehow it seems that an auth issue occurs…
here is where I call invokeTemplate function
_client.request.invokeTemplate("searchContact", {
context: {
phone: '+966566297375',
},
}).then(res => {
console.log('request response', res);
console.log(res.response);
}).catch(err => {
console.log('request error', err)
});
Here is the result I get :
<!doctype html>
<html lang="en">
<head>
<title>Freshchat</title>
<meta charset="utf-8" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="apple-itunes-app" content="app-id=1073125057">
<link rel="manifest" href="https://assets.freshsales.io/assets/mobile/manifest.json">
<link href="https://assets.freshsales.io/assets/images/favicon_fdm.ico" rel="shortcut icon" type="image/vnd.microsoft.icon" />
<link rel="stylesheet" media="all" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:200,300,400,700,900" />
<link rel="stylesheet" media="all" href="https://assets.freshsales.io/stylesheets/public.css" />
<script type="text/javascript">
var TRANSLATIONS = {
"email": {
"required": "Enter your email address.",
"properEmail": "Enter a valid email address."
},
"password": {
"required": "Enter your password.",
"minLength": "Your password must be at least {0} characters long.",
"maxLength": "Your password cannot exceed {0} characters."
},
"password_confirmation": {
"equalTo": "Passwords do not match. Please try again."
}
};
</script>
<script src="https://assets.freshsales.io/javascripts/authentications/app/script.js"></script>
</head>
<body class="auth-pages">
<div class="wrapper-container">
<div class="viewport">
<div class="single-view-wrap">
<div class="single-view-container">
<div class="centre-column">
<div class="text-center">
<a href="/" class="logo-link">
<img alt="Freshsales" class="logo-public" src= https://assets.freshsales.io/assets/images/logo_freshchat_full.png />
</a>
</div>
<!-- This is a temporary file for bootstraping ember -->
</div>
</div>
</div>
</div>
</div>
</body>
</html>
and the request template
{
"searchContact": {
"schema": {
"method": "GET",
"host": "<%= iparam.domainName %>.myfreshworks.com",
"path": "/crm/sales/search",
"headers": {
"Authorization": "Token token=encode(<%= iparam.api_key %>)",
"Content-Type": "application/json"
},
"query": {
"q": "<%= context.phone %>",
"include": "contact"
}
}
}
}
and the iparams definition
{
"api_key": {
"display_name": "Your freshsales API",
"description": "Please enter your API key",
"type": "api_key",
"type_attributes": {
"product": "freshworks_crm"
},
"required": true,
"secure": true
},
"domainName": {
"display_name": "Domain Name",
"description": "Please enter your domain name",
"type": "domain",
"type_attributes": {
"product": "freshworks_crm"
},
"required": true
}
}
Doing this request I receive a response with status 200 but HTML instead of JSON. However doing the same request with fetch I get the expected result :
fetch("https://mydomain.myfreshworks.com/crm/sales/search?q=%2B966566297375&include=contact", {
"method": "GET",
"headers": {
"Authorization": "Token token=apitoken"
}
})
.then(response => {
console.log(response);
})
.catch(err => {
console.error(err);
});
getting the following result
[
{
"id": "27038041522",
"name": "camille test",
"owner": {
"id": 27000112800,
"name": "Camille Hayot"
},
"updated_at": "2024-01-19T09:05:29Z",
"type": "contact"
}
]