Import dashboard from file via API

Good Day

I am trying to import a dashboard exported from the Grafana GUI via a Curl.

The command I am using for import is:

curl -X POST --insecure -H "Authorization: Bearer $apikey" -H "Content-Type: application/json" -d @hi.json http://192.168.8.14:3000/api/dashboards/import

The dashboard json content:

{
  "annotations": {
    "list": [
      {
        "builtIn": 1,
        "datasource": "-- Grafana --",
        "enable": true,
        "hide": true,
        "iconColor": "rgba(0, 211, 255, 1)",
        "name": "Annotations & Alerts",
        "type": "dashboard"
      }
    ]
  },
  "editable": true,
  "gnetId": null,
  "graphTooltip": 0,
  "id": 3371,
  "links": [],
  "panels": [
    {
      "columns": [],
      "datasource": "rt",
      "fontSize": "100%",
      "gridPos": {
        "h": 9,
        "w": 12,
        "x": 0,
        "y": 0
      },
      "id": 2,
      "links": [],
      "pageSize": null,
      "scroll": true,
      "showHeader": true,
      "sort": {
        "col": 0,
        "desc": true
      },
      "styles": [
        {
          "alias": "Time",
          "dateFormat": "YYYY-MM-DD HH:mm:ss",
          "pattern": "Time",
          "type": "date"
        },
        {
          "alias": "",
          "colorMode": null,
          "colors": [
            "rgba(245, 54, 54, 0.9)",
            "rgba(237, 129, 40, 0.89)",
            "rgba(50, 172, 45, 0.97)"
          ],
          "decimals": 2,
          "pattern": "/.*/",
          "thresholds": [],
          "type": "number",
          "unit": "short"
        }
      ],
      "targets": [
        {
          "alias": "",
          "format": "table",
          "rawSql": "SELECT ticket from `sanlam-gbe`;",
          "refId": "A"
        }
      ],
      "title": "Panel Title",
      "transform": "table",
      "type": "table"
    }
  ],
  "schemaVersion": 16,
  "style": "dark",
  "tags": [],
  "templating": {
    "list": []
  },
  "time": {
    "from": "now-6h",
    "to": "now"
  },
  "timepicker": {
    "refresh_intervals": [
      "5s",
      "10s",
      "30s",
      "1m",
      "5m",
      "15m",
      "30m",
      "1h",
      "2h",
      "1d"
    ],
    "time_options": [
      "5m",
      "15m",
      "1h",
      "6h",
      "12h",
      "24h",
      "2d",
      "7d",
      "30d"
    ]
  },
  "timezone": "",
  "title": "dhyata test",
  "uid": "fSXsFG0Wk",
  "version": 2
}

The import is failing with error:

{"error":"Server Error","message":"Internal Server Error - Check the Grafana server logs for the detailed error message."}

Log file output:

t=2019-10-25T14:01:07+0000 lvl=eror msg="Request error" logger=context userId=0 orgId=8 uname= error="runtime error: invalid memory address or nil pointer dereference" stack="/usr/local/go/src/runtime/panic.go:82 (0x44e220)\n/usr/local/go/src/runtime/signal_unix.go:390 (0x44e04f)\n/go/src/github.com/grafana/grafana/pkg/components/simplejson/simplejson.go:142 (0xa58391)\n/go/src/github.com/grafana/grafana/pkg/components/simplejson/simplejson.go:142 (0xa58391)\n/go/src/github.com/grafana/grafana/pkg/models/dashboards.go:130 (0xa58390)\n/go/src/github.com/grafana/grafana/pkg/plugins/dashboard_importer.go:55 (0xcc1d84)\n/usr/local/go/src/reflect/value.go:447 (0x492e20)\n/usr/local/go/src/reflect/value.go:308 (0x4928a3)\n/go/src/github.com/grafana/grafana/pkg/bus/bus.go:131 (0xa4c3f1)\n/go/src/github.com/grafana/grafana/pkg/bus/bus.go:216 (0x1295813)\n/go/src/github.com/grafana/grafana/pkg/api/plugins.go:194 (0x12957f1)\n/usr/local/go/src/reflect/value.go:447 (0x492e20)\n/usr/local/go/src/reflect/value.go:308 (0x4928a3)\n/go/src/github.com/grafana/grafana/vendor/github.com/go-macaron/inject/inject.go:177 (0x8ea1a6)\n/go/src/github.com/grafana/grafana/vendor/github.com/go-macaron/inject/inject.go:137 (0x8e9a9b)\n/go/src/github.com/grafana/grafana/pkg/api/common.go:37 (0x12ad5cd)\n/usr/local/go/src/reflect/value.go:447 (0x492e20)\n/usr/local/go/src/reflect/value.go:308 (0x4928a3)\n/go/src/github.com/grafana/grafana/vendor/github.com/go-macaron/inject/inject.go:177 (0x8ea1a6)\n/go/src/github.com/grafana/grafana/vendor/github.com/go-macaron/inject/inject.go:137 (0x8e9a9b)\n/go/src/github.com/grafana/grafana/vendor/gopkg.in/macaron.v1/context.go:121 (0x8eb259)\n/go/src/github.com/grafana/grafana/vendor/gopkg.in/macaron.v1/context.go:112 (0xbf030e)\n/go/src/github.com/grafana/grafana/pkg/middleware/request_tracing.go:25 (0xbf0301)\n/usr/local/go/src/reflect/value.go:447 (0x492e20)\n/usr/local/go/src/reflect/value.go:308 (0x4928a3)\n/go/src/github.com/grafana/grafana/vendor/github.com/go-macaron/inject/inject.go:177 (0x8ea1a6)\n/go/src/github.com/grafana/grafana/vendor/github.com/go-macaron/inject/inject.go:137 (0x8e9a9b)\n/go/src/github.com/grafana/grafana/vendor/gopkg.in/macaron.v1/context.go:121 (0x8eb259)\n/go/src/github.com/grafana/grafana/vendor/gopkg.in/macaron.v1/context.go:112 (0xbefb2c)\n/go/src/github.com/grafana/grafana/pkg/middleware/request_metrics.go:17 (0xbefb17)\n/usr/local/go/src/reflect/value.go:447 (0x492e20)\n/usr/local/go/src/reflect/value.go:308 (0x4928a3)\n/go/src/github.com/grafana/grafana/vendor/github.com/go-macaron/inject/inject.go:177 (0x8ea1a6)\n/go/src/github.com/grafana/grafana/vendor/github.com/go-macaron/inject/inject.go:137 (0x8e9a9b)\n/go/src/github.com/grafana/grafana/vendor/gopkg.in/macaron.v1/context.go:121 (0x8eb259)\n/go/src/github.com/grafana/grafana/vendor/gopkg.in/macaron.v1/context.go:112 (0xbefa45)\n/go/src/github.com/grafana/grafana/pkg/middleware/recovery.go:147 (0xbefa33)\n/go/src/github.com/grafana/grafana/vendor/gopkg.in/macaron.v1/context.go:79 (0x8eb100)\n/go/src/github.com/grafana/grafana/vendor/github.com/go-macaron/inject/inject.go:157 (0x8e9e60)\n/go/src/github.com/grafana/grafana/vendor/github.com/go-macaron/inject/inject.go:135 (0x8e9b8a)\n/go/src/github.com/grafana/grafana/vendor/gopkg.in/macaron.v1/context.go:121 (0x8eb259)\n/go/src/github.com/grafana/grafana/vendor/gopkg.in/macaron.v1/context.go:112 (0xbecf5b)\n/go/src/github.com/grafana/grafana/pkg/middleware/logger.go:34 (0xbecf46)\n/usr/local/go/src/reflect/value.go:447 (0x492e20)\n/usr/local/go/src/reflect/value.go:308 (0x4928a3)\n/go/src/github.com/grafana/grafana/vendor/github.com/go-macaron/inject/inject.go:177 (0x8ea1a6)\n/go/src/github.com/grafana/grafana/vendor/github.com/go-macaron/inject/inject.go:137 (0x8e9a9b)\n/go/src/github.com/grafana/grafana/vendor/gopkg.in/macaron.v1/context.go:121 (0x8eb259)\n/go/src/github.com/grafana/grafana/vendor/gopkg.in/macaron.v1/router.go:187 (0x8fc606)\n/go/src/github.com/grafana/grafana/vendor/gopkg.in/macaron.v1/router.go:294 (0x8f701d)\n/go/src/github.com/grafana/grafana/vendor/gopkg.in/macaron.v1/macaron.go:220 (0x8f02ac)\n/usr/local/go/src/net/http/server.go:2774 (0x6f2ca7)\n/usr/local/go/src/net/http/server.go:1878 (0x6ee990)\n/usr/local/go/src/runtime/asm_amd64.s:1337 (0x468150)\n"

POST data are not OK. Try&debug&improve:

-d "{\"dashboard\":$(cat hi.json)}"
1 Like

Hi, I have done it with Python, I suppose you can adapt with curl. The json code you need to send must have a specific structure. The annotations etc that you get seem like they come from an export in json and that is not the outer structure. For example, use something like:
{
“dashboard”:
{
Here is where you have to include your own structure exported via json from somewhere else
},
“folderId”: 0,
“overwrite”: true
}
Be careful about one thing: notice the “id” field you put in your data. If I didn’t set this to null, it would complain about dashboard not found or something like that. If you don’t manage to get it done, please respond, I will try to be more helpful. The point is the structure you are providing is missing some fields and should be included as the dashboard field value, along with some necessary other ones in an outer structure.
Also make sure you have done all that was necessary to create the api key etc. Again let me know if you need more guidance.
btw I also think your url is wrong, the path I used (got it from the doc) is this: /api/dashboards/db, of course I could be wrong and the change in url (using an import function) might mean you don’t need that structure. I may try your approach to see what happens.

2 Likes

Curl script is running but not able to import dashboard, its showing following error
if (!checkBrowserCompatibility()) {
alert(‘Your browser is not fully supported, please try newer version.’);