Grafana User managment

Hey. I want to automate the user management in my grafana server. Therefore I want to know if it is possible to create and delete users directly on the server and if that is possible, where the user data is located on the server? I am hosting the Grafana server on a windows machine.

You can send http requests to the Grafana API.
Create a user (Admin API)
Delete a user (Admin API)
Get users (User API)

@zuperzee Okay. Follow up question: I found this on the forum:
curl “http://admin:admin@localhost:3000/api/users
To get all the users. But I can not get it running. Can you help me?
Let’s say I have a admin api key:
xyz123
And the domain of my server was:
https://myserver.net

Could you tell me what to run in the terminal?

To use the Grafana admin API you need to use basic auth. As described in the docs.

The Admin HTTP API does not currently work with an API Token. API Tokens are currently only linked to an organization and an organization role. They cannot be given the permission of server admin, only users can be given that permission. So in order to use these API calls you will have to use Basic Auth and the Grafana user must have the Grafana Admin permission. (The default admin user is called admin and has permission to use this API.)

Here is the curl command curl http://admin:admin@localhost:3100/api/users

Here is a simple node js script (needs nodejs and can be executed with node filename.js) that gets the users through the Grafana admin API.
Remember to change the auth section (“admin:admin”) with the actual admin user and admin password.

// The default http package that node uses.
// There are a lot of alternatives to http that might be a bit easier to work with.
const http = require("http"); // The http package doesn't work with https. Switch to the https package for https.

const req = http.request(
  {
    host: "localhost",
    port: 3100,
    path: "/api/users",
    method: "GET",
    auth: "admin:admin", // Change this to the actual admin user and password
  },
  (res) => {
    console.log("Status Code:", res.statusCode);

    res.setEncoding("utf8");
    res.on("data", (data) => {
      console.log("Body: ", JSON.parse(data));
    });
  }
);

req.on("error", (err) => {
  console.log("Error: ", err.message);
});
req.end();

And to create a user

// The default http package that node uses.
// There are a lot of alternatives to http that might be a bit easier to work with.
const http = require("http"); // The http package doesn't work with https. Switch to the https package for https.

const newUserData = {
  name: "New user",
  email: "new@user.com",
  login: "new user",
  password: "password",
};

const req = http.request(
  {
    host: "localhost",
    port: 3100,
    path: "/api/admin/users",
    method: "POST",
    auth: "admin:admin", // Change this to the actual admin user:password
    headers: {
      "Content-Type": "application/json",
      Accept: "application/json",
    },
  },
  (res) => {
    console.log("Status Code:", res.statusCode);

    res.setEncoding("utf8");
    res.on("data", (data) => {
      console.log("Body: ", JSON.parse(data));
    });
  }
);

req.on("error", (err) => {
  console.log("Error: ", err.message);
});
req.write(JSON.stringify(newUserData));
req.end();

A simple screenshot of getting the users

@zuperzee first of all thanks a lot!!!

1 Like

Follow up. Since data might be in chunks the following code would work better

{
    console.log("Status Code:", res.statusCode);
    let data = [];

    res.setEncoding("utf8");
    res.on("data", (chunk) => {
      data.push(chunk);
    });
    res.on("end", () => {
      console.log("Body: ", JSON.parse(data));
    });
  }

Here is also how you would do it against a “real” url (not localhost)

// The default http package that node uses.
// There are a lot of alternatives to http that might be a bit easier to work with.
const http = require("https"); // The http package doesn't work with https. Switch to the https package for https.

const req = http.request(
  {
    host: "random.url.io",
    path: "/api/users",
    method: "GET",
    auth: "mylogin@something.com:MyRandomPassword", // Change this to the actual admin user and password
  },
  (res) => {
    console.log("Status Code:", res.statusCode);
    let data = [];

    res.setEncoding("utf8");
    res.on("data", (chunk) => {
      data.push(chunk);
    });
    res.on("end", () => {
      console.log("Body: ", JSON.parse(data));
    });
  }
);

req.on("error", (err) => {
  console.log("Error: ", err.message);
});
req.end();