Programmatically Create Questions

`{
  "name": "My Card Name",
  "description value": "My Card Description",
  "collection_position": null,
  "visualization_settings": {
    "table.pivot_column": "PIVOT_COLUMN",
    "table.cell_column": "CELL_COLUMN"
  },
  "dataset_query": {
    "database": 5,
    "query": {
      "native": {
           "query":"select * from my_table limit 5"
       }
    },
    "type": "query"
  },
  "display": "table"
}`

I’m working on programmatically creating questions. I’m working in Postman to get a feel for how the API works. I pulled the above payload from the network tab and ran it. A question was created, but when I clicked into the question, I was prompted to select my data source and there was no query to run or data to view.

I’m assuming I need to swap out “native” for something like “api”?

I basically need to create a script that will generate a question and then generate a pulse as we have a few canned reports that I’d like to be able to programmatically send to customers etc.

Hi @DATA_GATA
I would highly recommend that you read this:
https://www.metabase.com/blog/metabase-api/index.html
The best way to learn the API, is to just use Metabase while having your browser developer network-tab open and looking at the request, and what data is being send/received.

That was the first place I looked and then came here after a sufficient number of failures to drive one mad. ha hoping someone can help relieve some of the frustration.

Seems that I just need to crack the code of what the dataset_query object needs to be populated with but haven't found a great example as what I saw in the network tab of developer tools didn't seem to work.

Any Blues Clues would be greatly appreciated!

@DATA_GATA The requests you see in the browser developer Network-tab works - since almost all of Metabase just uses the API to do everything.

Progress! “type” needed to be “native” but now I’m getting an infinite loading animation. Wish me luck!

{
  "name": "My Card Name",
  "description value": "My Card Description",
  "collection_position": null,
  "visualization_settings": {
    "table.pivot_column": "PIVOT_COLUMN",
    "table.cell_column": "CELL_COLUMN"
  },
  "dataset_query": {
    "database": 5,
    "query": {
      "native": {
           "query":"select * from my_table limit 5"
       }
    },
    "type": "native"
  },
  "display": "table"
}
1 Like

@DATA_GATA Think you got too many query. This works on Sample Dataset:

{
  "name": "Forum 12109",
  "dataset_query": {
    "type": "native",
    "native": {
      "query": "SELECT ID, CATEGORY, RATING FROM PRODUCTS LIMIT 5"
    },
    "database": 1
  },
  "display": "table",
  "visualization_settings": {
    "table.pivot": true,
    "table.cell_column": "RATING",
    "table.pivot_column": "CATEGORY"
  }
}
1 Like

Hi @DATA_GATA.

Let us know if there is anything we could add to (or clarify in) the Working with the Metabase API article that would’ve helped you out (and help out future readers).