Metabase REST API: Programmatically add cards (questions) to dashboard


#1

I would like to programmatically add a card (question) to a dashboard

I am able to create cards (questions) but I dont see how to add them to the dashboard programmatically.

In the documentation it it says the following:

POST /api/dashboard/
Create a new Dashboard.

PARAMS:
name value must be a non-blank string.

description value may be nil, or if non-nil, value must be a string.

parameters value must be an array. Each value must be a map.

dashboard

Dashboard seems to not have a description. Is this how you add cards?

Also when calling GET /api/dashboards it seems that ordered_cards is where the location and size information is stored for the card (question) visualisation on the dashboard.

How can I add a question to a dashboard with the size and location parameters?
Are there other requirements?

Any minimal example would be extremely helpful.


#2

Howdy!,

No, it just says that the description for a dashboard is optional

Scroll a bit further down to POST /api/dashboard/:id/cards or maybe even
PUT /api/dashboard/:id/cards which seem to describe with a partial example.


#3

I re-read the documentation, and I was able to automatically create cards with the information you provided.

Thank you for your help


#4

@nathan-tr & @jornh can you please tell me how you create api call

{

       "dataset_query": {
        "database": 34,
        "type": "query",
        "query": {
            "source_table": 68,
            "aggregation": [
                [
                    "sum",
                    [
                        "field-id",
                        163
                    ]
                ]
            ],
            "filter": [
                "AND",
                [
                    "=",
                    [
                        "field-id",
                        170
                    ],
                    "2018"
                ],
                [
                    "=",
                    [
                        "field-id",
                        172
                    ],
                    "4 - April",
                    "5 - May",
                    "6 - June",
                    "7 - July"
                ]
            ]
        }
    },
  "description": "Cash Inflow -Company",
"display": "scalar",
 "name": "API PUSH Cash Inflow",
"visualization_settings": {
        "scalar.scale": 0.000001,
        "scalar.decimals": 0,
        "scalar.suffix": " M"
    },
       "result_metadata": [
        {
            "base_type": "type/Float",
            "display_name": "sum",
            "name": "sum"
        }
    ]

}
this is my call it fail

"message": "Query expansion failed: could not find table 33 (FK ID = null). Resolved tables ([fk-id table-id]): ([nil 68])",
"type": "class java.lang.Exception",

Table and database id are correct


#5

Hard to say what goes wrong with your call - apart from what the error message points to (mismatch with some table IDs?).

What API endpoint/http method are you using?