!!!参考 https://www.metabase.com/docs/latest/api-documentation.html https://discourse.metabase.com/t/where-to-get-api-query-schema-specification/5483/5 https://qiita.com/macoril/items/2e3fff2785a9d3b80ad8 https://www.cdatablog.jp/entry/postmanaccesstokenrefresh !!!概要 postman等で色々なDBに対してAPI経由でDBアクセスしたいことがある。 MySQLならXmysqlがあるけど、SQL Serverとかだとなかなか良いのがない。 metabaseには、API経由でDBアクセスしたり、SQLクエリを実行したいする機能があるのでそれを使うと便利なときがある。 !!!基本的な使い方 !まずは普通にmetabaseを使えるようにする !session-idの発行 curl -X POST \ -H "Content-Type: application/json" \ -d '{"username": "user@example.com", "password": "metapass"}' \ http://localhost:3000/api/session # -> {"id":"3e7b88f0-49db-44ac-bc09-b48679d60d06"} !SQLを発行する curl -X POST \ http://localhost:3000/api/dataset \ -H "Content-Type: application/json" \ -H "X-Metabase-Session: 3e7b88f0-49db-44ac-bc09-b48679d60d06" \ -d '{ "database": 1, "native": { "query": "SELECT COUNT(*) FROM Orders" }, "type": "native" }' databaseは、metabaseに登録されているデータベースのID。 metabaseの画面で確認するかAPIでdatabaseを取得すれば確認できる。 !SQLを発行する(JSON形式で受け取る) curl -X POST \ http://localhost:3000/api/dataset/json \ -H "Content-Type: application/x-www-form-urlencoded" \ -H "X-Metabase-Session: 3e7b88f0-49db-44ac-bc09-b48679d60d06" \ -d 'query={ "database": 1, "native": { "query": "SELECT COUNT(*) FROM Orders" }, "type": "native" }' 形式は、JSONやCSV等を指定できる。 Content-Typeには、application/x-www-form-urlencoded を指定する。 !!!Postmanで使う場合 !session_idの発行 pre-requestで以下のような処理をする pm.sendRequest({ url: 'http://dev.xxx.com:4001/api/v1/portal/account/tax-login', method: "post", body: { "username": "user@example.com", "password": "metapass" }, header: { 'Content-Type': 'application/json' } },function (err, res) { if(pm.response.to.be.ok && pm.response.to.have.jsonBody("id")){ let json = JSON.parse(responseBody) pm.globals.set('metabase-session-id', json.id) } }); {{category2 データベース}}