Access localStorage from Rest API Resource

Hello!

  • I have an API call.
  • I have user information that is currently saved in localStorage (userId etc) from my app.
  • I would like to be able to access that in all my API calls.

Currently my workaround is in every API call I set the url params from
{{ localStorage.values.userId }}. However, it is tedious to have to add this to every API call I want to make but I can't seem to find a way to access localStorage from the base Resource API request.

  • Steps:
  1. So far I have tried setting the auth type to Custom and setting the variable using Javascript and the Define variable.
  2. I also have tried accessing {{ localStorage.values.userId }} directly in the URL parameters field.

Please let me know if there is any way to access localStorage from the Resource, or if there is an alternate method to pass information from the app to the base Resource that I am not thinking of.

Hey @Tim_Hodgson, unfortunately what you're trying to do is not logically possible. localStorage exists in the context of a user using the app via a browser.

When defining a resource, there is no user -> no browser -> no localStorage. Fun fact: That's exactly why Retool workflows also cannot access localStorage.

Workaround

  1. In your app, specify one REST API query of your API, for each HTTP method that you are using (GET, POST) etc. Call them httpGetMyApi, httpPostMyApi.
  2. In each of these, supply the common url parameter that you currently have to specify each time.
  3. In each of these, leave the endpoint URL part dynamic by defining a parameter, such as {{param_endpoint}}
  4. Now every time you want to make an endpoint query, instead of defining 'REST API', define a JS block in which you call the appropriate REST API query by supplying the param_endpoint as additionalScope.

Obviously this is not as beautiful as you'd like, and there is some dirty boilerplate code involved, but it will solve the exact problem you mentioned.

Hope this helps.

Abdul Wasae
Toolshed