Context Not Switching with API Calls After Organization Switch

Hello Grafana Community,

I’m encountering a persistent issue where the context within Grafana does not seem to update according to the organization context change when making API calls. Specifically, despite switching organizations through the UI and using appropriate service account tokens for each organization, the API consistently returns data from the default organization (e.g., Main.org) and not from the currently active one (e.g., Anonymous).

Details:

Version: Grafana v10.4.2 (but also tested on v11.2.0)

Issue: API responses do not reflect the currently active organization after switching via the UI. This problem persists even when using service account tokens that are organization-specific.

Expected Behavior: API calls should return data corresponding to the active organization based on the service account token provided.

Steps to Reproduce:

  1. Switch organizations in the Grafana UI.

  2. Use an API token associated with the newly selected organization.

  3. Make API calls to fetch resources (e.g., dashboards).

  4. Responses still reflect resources from the initial organization prior to the switch.

Questions for the Community:

  1. Has anyone faced similar issues with organization context not updating in API calls?

  2. Are there known workarounds or configurations that might influence this behavior?

  3. Could this behavior be related to session management or token handling within Grafana’s backend?

Any insights or recommendations would be greatly appreciated as navigating through this has been challenging. Links to documentation or previous discussions on similar topics would also be very helpful.

Thank you in advance for your help and looking forward to your suggestions!

Hello everyone,

I wanted to share a solution and an important clarification regarding the usage of Grafana API for fetching dashboards, which might be particularly useful if you are struggling with similar issues.

For several weeks, I encountered a problem where using a custom API endpoint provided by a plugin (api/plugins/myappplugin/resources/search-dashboards) consistently returned dashboard data from the wrong organization, not reflecting changes even after switching organizations. This was misleading and caused quite a bit of confusion.

The correct approach to fetch all dashboards related to the currently active organization, considering access controls and without crossing organization boundaries, is to use the standard Grafana API endpoint with proper authentication:

GET /api/search?type=dash-db
Authorization: Bearer [YourServiceAccountToken]

This method ensures that you are fetching data pertinent to the current organization context established by the service account’s token, especially important for those managing multiple organizations within Grafana.

I hope this helps anyone who might be facing similar issues and apologies for any confusion caused by previous discussions or examples.

1 Like