How to get the loggedInUser Details using Freshwork API in freshsales?

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?

Hi @Dhanalakshmi

If you are using FDK please use request method instead of fetch to make API requests.

Let me know if you need further details

Could you please send a demo code for this.
I couldn’t get able to implement request method in my code.
And is there any other way to fetch the loggedInOwner (loggedInuser) phone number.

Hi @Dhanalakshmi

Did you create the requests.json in the config folder in your project directory?
If not please follow the steps in this help documentation.

Hi @Dhanalakshmi

There is a another way to get the LoggedIn User (Agent ) details.
You can use client method to get the current logged In user details.

client.data.get(“loggedInUser”).then((data)=>{
console.log(“this is data”, data);
})