Creating Datasource via API in Powershell is Failing

Hello!

I’m brand new to Grafana as of this week (as well as interacting with an API in any meaningful manner), and I’m struggling a bit with getting a datasource into my Grafana instance via your API. I was able to sus out the format behind getting a Dashboard created through the API, but in trying to do the same thing for a Datasource, I’m coming up short.

I get hit with the following error in Powershell:

Invoke-RestMethod : [{"fieldNames":["Name"],"classification":"RequiredError","message":"Required"},{"fieldNames":["Type"],"classification":"RequiredError","message":"Required"},{"fieldNames":["Access"],"classificat
ion":"RequiredError","message":"Required"}]

In reading a topic from the Github page, the only real solution that was working for people was when they were missing “application/json” as their content-type, but I’ve got that already. Below is my powershell code, can anyone advise?

$header = @{"Authorization" = "Bearer apikey="}
$createDatasourceUri = "http://localhost:3000/api/datasources"
$createDatasourcejson = @'
{
    "datasource": {
        "name": "prometheusApiTest",
        "type": "prometheus",
        "url": "http://localhost:9090",
        "access": "proxy",
        "basicAuth": false,
        "isDefault": true
    },
    "message": "creating test datasource from api call"
}
'@

$datasourceParameters = @{
    Method = "POST"
    URI = $createDatasourceUri
    Body = $createDatasourcejson
    Headers = $header
    ContentType = "application/json"
}

Invoke-RestMethod @datasourceParameters

I’m not sure what’s wrong, as this block of code is almost character-for-character the same as the Dashboard create, albeit with a different json payload. Any advice would be really appreciated!

I’m a dummy, is the basic answer. There is no need to specify the datasource element in the json payload, I read the docs wrong. My b!