Basic Auth API Call

Hi, I’m just a beginner on Grafana, I would like some example on how to use API to authenticate as Admin using Basic Auth through API call , and then create viewer users with another API call, not sure how should I pass user and password in request or If somehow I need to get a token first and then use it in subsecuent calls, If you could help me with an example of it that would be awsome,

Please Advise,

Hello,

If you want to use Basic Auth, you need to provide your username and password. The way of providing this information depends on the tool you are using to do the request. Here is an example using cURL (you can also leave the password blank and you will be prompted for the password)

curl --request POST 'http://grafana.staged-by-discourse.com/api/admin/users' --user "admin:admin" --header 'Content-Type: application/json' --data-raw '{"name":"test","email":"test","login":"test","password":"test"}'

If you search on Google the name of your tool and Basic Authentication, you should find more specific examples.

You have all the ways of authenticating to the API here. You can also use a session cookie (that you can retrieve with a login request) or an API Token (that you can generate through Grafana UI).

Maybe I should have started what I want to accomplish here, what I actually want is to embbed grafana dashboards into homemade application, so I need some how send credentials and not showing login page, I hope you can guide me in right direction.

Best Regards,

Hello
if you find the solution, please could u share here too?

I found a solution to use Basic Auth in PowerShell, but the solution should work in any language :slight_smile:

Instead of appending your credentials to the url (user:pass@example.com), you need to base64 encode the user:pass string and use it in the header. My current Powershell Script:

$credentials = "username:password" # Dont forget the : here
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($credentials))

$headersGrafana = @{
    "Authorization" = ("Basic "+$encodedCreds)
    "Content-Type" = "application/json"
}
$urlGrafanaOrganizations = "https://grafana.example.com/api/orgs"

Invoke-RestMethod -Uri $urlGrafanaOrganizations -Method Get -Headers $headersGrafana

I hope this work for you too!