Hi, how can I get the logged-in user details using the Freshsales API?
Currently, I am using the following API:
https://userXXXXXXXXX.myfreshworks.com/crm/sales/api/selector/owners
I am getting a 200 OK
response with user details in postman
However, when I use the same API inside the FDK, I encounter a CORS error.
Here is the code I am using:
async function getAgentDetails(apiKey, domain) {
const url = https://${domain}/api/v2/agents/me
;
try {
const response = await fetch(url, {
method: ‘GET’,
headers: {
‘Authorization’: 'Basic ’ + btoa(${apiKey}:X
),
‘Content-Type’: ‘application/json’
}
});
if (!response.ok) {
throw new Error(`Error: ${response.status} ${response.statusText}`);
}
const data = await response.json();
console.log("Agent Details:", data);
const execPhoneNumber = data.agent.work_phone_number;
console.log("executivenumber-------->" ,execPhoneNumber);
const event = new CustomEvent('execPhoneSet', { detail: { execPhoneNumber } });
window.dispatchEvent(event);
} catch (error) {
console.error(“Failed to fetch agent details:”, error);
}
}
getting below error:
Access to fetch at ‘https://xxxxxxxxxxxxxxxxxx.myfreshworks.com/api/v2/agents/me’ from origin ‘http://localhost:10001’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. If an opaque response serves your needs, set the request’s mode to ‘no-cors’ to fetch the resource with CORS disabled.
Can anyone provide a solution?