Getting Correct API Endpoint for User's Data Centre

My FreshChat app collects the user’s API key during installation, and uses it to make API calls on their behalf. I recently discovered, however, that now FreshChat accounts hosted in different data centres require different API endpoints to be used. For example, for the Indian data centre the endpoint is https://api.in.freshchat.com/v2/ and for the US it’s https://api.freshchat.com/v2/

Is there a best practice for how to identify the proper data centre for the user so that we can make API calls on their behalf to the correct API endpoint? Or should we just attempt each of the endpoints until one of them works, then use that one going forward for that user?

1 Like

You could ask the customer to provide their domain as well, in addition to the API Token that you are requesting.

In this example, I’m asking for the Freshchat Domain and the API Token.
I use the domain name to construct the endpoint https://jaya.freshchat.com/v2/agents to verify the token.

You won’t need the region if you follow this approach.

3 Likes

If you absolutely do need the region without having to ask the customer for the region or even ask for the domain name, I have another trick.

The region is not available using the client object. You will have to get the region by making an SMI call.

  1. On config load in the iparams page, make an SMI request.
  2. In the SMI handler in server.js, you will receive the region attribute as part of the payload. (Marketplace provides this by default)
  3. Return that property as a response to the SMI in server.js.
  4. Consume the region from the response of the SMI in the iparams page.
  5. Have a local mapping of the regions with their corresponding URLs.
  6. Use the corresponding URL to make a Freshchat API call.

I have used a similar approach in the Advanced Automations app for Freshchat.

Hope this helps.

4 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.