If you already have panels inside your dashboard, copy the structure of one of them and inside your code change the value such as "gridPos", "title", etc... anything that you want to change.
The json for a panel look more or less like that from Grafana:
{
"datasource": {...},
"fieldConfig": {...},
"gridPos": {...},
"id": 1,
"options": {...},
"pluginVersion": "9.4.3",
"targets": [...],
"title": "Panel Title",
"type": "stat"
}
For testing, I have used a Python script.
Retrieve your dashboard from the API with UID method.
import json
import requests
endpoint = f'{_url}/dashboards/uid/{uid}'
dashboard = requests.get(endpoint, headers=_headers, verify=False).json()
Now you can notice the structure of the returned type from the API, you have two dict inside it:
Inside the dashboard dict, you can find a list element "panels" who contains all your panels information referenced as dicts.
a screenshot in the debugger
You can now add the new panel to the list of dict ("new_panel" variable is the json formatted panel you want to add):
dashboard['dashboard']['panels'].append(json.loads(new_panel))
And from this point, all you have to do is to update your dashboard with the API.
For doing it, you have to create the expecting json structure:
{
"dashboard": <the json model of your dashboard>,
"message": <a message for the commit version>,
"overwrite": True or False
}
in Python:
json_model = {'dashboard': dashboard['dashboard'], 'message': '', 'overwrite': False}
requests.post(endpoint, data=json.dumps(json_model), headers=headers, verify=False).json()
If your dashboard don't have any panel at the beginning, you have to create the panels list inside the dashboard element first:
dashboard['dashboard']['panels'] = json.loads(new_panel)
json_model = {'dashboard': dashboard['dashboard'], 'message': '', 'overwrite': False}
requests.post(endpoint, data=json.dumps(json_model), headers=headers, verify=False).json()
I hope this helped.