Get API key from Grafana API

Hi all,

i want to use Ansible to deploy Grafana, Grafana Reporter (https://github.com/IzakMarais/reporter) and a couple of dashboards.

In those dashboards, i want to add a PDF exporter button through a Grafana link. In the URL, i need the API key value.

With the following curl command i have the key’s id, name and role but not the value…

curl http://admin:admin@community.grafana.com/api/auth/keys
[{"id":1,"name":"grafana-reporter","role":"Viewer"}]

The key value is displayed only when i create the a new key:

curl 'http://admin:admin@community.grafana.com/api/auth/keys' -XPOST -H 'Content-Type: application/json' -d '{"role":"Viewer","name":"grafana-reporter2"}'
{"name":"grafana-reporter2","key":"eyJrIjoieE5TQ210TG9YMXd5U2U2Q1dwZnB5ZkdZVFR4OHJNS1ciLCJuIjoiZ3JhZmFuYS1yZXBvcnRlcjIiLCJpZCI6MX0="}

Is there a way to get the key ?

Hi @nicolargo,

This is a security feature to prevent unintended leaking of sensitive token information. There is no way to read an API token after its creation. It is assumed the administrator will copy it in a password manager at the time the token is created if retention is needed.

If you need to update an API token, the procedure is to delete the old one and create a new one.

Ok, i copy that.

Thanks !

@adeverteuil sorry I know this thread is old but I’m looking for an automated way to create api key upon deployment to Kubernetes and I would like to save that key if possible to AWS Secrets Manager (could be Vault) but just need way to get the Key itself upon creation so it can be saved. Either that it some way to create a random key and add that API key to the deployment. I have some connections from tools that require a grafana API key but since I don’t know that unless I manually generate one it’s kind of a chicken and egg situation. Since I have the same GitOps deployment model to all Kubernetes clusters doing this “manual” process just doesn’t work for us because of scale. Is there a CRD that we could use here?

You can generate API key with Ansible:

I don’t know if that suit you.