Solved

Can I make API calls from the browser?

  • 28 November 2023
  • 2 replies
  • 59 views

I’d like to use the Core API to generate draft messages. In the docs, I notice that there are examples for use in JavaScript (including fetch and XHR APIs). When I tested this code in the browser console, the request was rejected due to cross-origin headers (specifically the requesting origin was not allowed by Access-Control-Allowed-Origins). Is calling the API from the browser supported? And if so, how would I go about configuring CORS headers?

Thanks!

icon

Best answer by jason 28 November 2023, 17:46

View original

2 replies

Userlevel 4
Badge +7

No, this is not supported for security reasons.

If you need to initiate requests from the browser, we advise issuing a request to your server, and having the server perform the Front REST API request (so no API credentials are exposed to the browser), and return the result to the browser. This also allows you to utilize caching results where appropriate. 

Thanks - I suspected as much. No problem at all, thank you for your help 😀

Reply