Can't create new user programatically being a Grafana Admin (original) (raw)

As a Grafana Admin

image

I am trying to create a new user from my app (using a Service Account token with an Admin role that I created as Grafana Admin). Here is the code:

      const createUserInGrafana = async (username: string, password: string, email: string) => {
      const grafanaApiUrl = 'https://grafana.mydomain.de';    
      const grafanaApiKey =  'glsa_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; 

      // User creation payload for Grafana
      const userPayload = {
        name: username,
        login: username,
        password: password,
        email: email,
        // orgId: link.orgId,  
        // role: 'Viewer'
      };

      
      try {
        // Sending the POST request to Grafana to create the user
        const response = await axios.post(`${grafanaApiUrl}/api/admin/users`, userPayload, {
          headers: {
            'Authorization': `Bearer ${grafanaApiKey}`,
            'Content-Type': 'application/json',
          },
        });

        console.log('Grafana user created:', response.data);
      } catch (error: unknown) {
        if (error instanceof Error) {
          // Ensure we check for AxiosError to access the `response` property
          if (error instanceof AxiosError) {
            console.error('Error creating user in Grafana:', error.response ? error.response.data : error.message);
          } else {
            console.error('Error creating user in Grafana:', error.message);
          }
        } else {
          console.error('An unknown error occurred:', error);
        }
      }
    };

    // Create the user in Grafana after MongoDB creation
    await createUserInGrafana(username, randomPassword, email);

Running the above I am getting this error:

Error creating user in Grafana: {
accessErrorId: ‘ACE3095890940’,
message: “You’ll need additional permissions to perform this action. Permissions needed: users:create”,
title: ‘Access denied’
}

Additionally, according to some advices from other forums I added in my grafana.ini file:

[rbac]
enabled = true

resources_with_managed_permissions_on_creation = dashboard,folder,service-account,datasource,user

[security]
admin_access_enabled = true

Also, logged in as Grafana Admin when I open https://grafana.mydomain.de/api/admin/users in browser I get {“message”:“Not found”}.

I’ve seen some people writing that adding users like that is only possible for Enterprise version, some said it works for OSS too, so I am not sure, it’s very confusing.
Any ideas on what I may be missing?