高木のブログ

Toggl APIで前日の総作業時間を取得する

2020/07/25

APIで取得できる項目はいろいろあるけど、前日の作業時間だけ欲しかったのでそれだけ調べたメモ

手順

API Tokenの取得

プロフィールページの下の方に表示されている
https://toggl.com/app/profile

curlで叩く

api_tokenworkspace_idはドキュメントに載ってたダミー

curl -u 1971800d4d82861d8f2c1651fea4d212:api_token -X GET "https://toggl.com/reports/api/v2/summary?user_agent=api_test&workspace_id=123&since=2020-07-24&until=2020-07-24"
  • user_agent: 必須。アプリケーション名かメールアドレス
  • workspace_id: 必須。ReportsページのURLに含まれている
  • since: 開始日(Defaults to today - 6 days.)
  • until: 終了日(Defaults to today, unless since is in future or more than year ago, in this case until is since + 6 days.)

結果

{
  "total_grand": 6188000,
  "total_billable": null,
  "total_currencies": [
    {
      "currency": null,
      "amount": null
    }
  ],
  "data": [
    {
      "id": 162312870,
      "title": {
        "project": "ブログ活動",
        "client": null,
        "color": "0",
        "hex_color": "#d92b2b"
      },
      "time": 3777000,
      "total_currencies": [
        {
          "currency": null,
          "amount": null
        }
      ],
      "items": [
        {
          "title": {
            "time_entry": ""
          },
          "time": 3777000,
          "cur": null,
          "sum": null,
          "rate": null
        }
      ]
    },
    {
      "id": 162307142,
      "title": {
        "project": "英語学習",
        "client": null,
        "color": "0",
        "hex_color": "#c9806b"
      },
      "time": 428000,
      "total_currencies": [
        {
          "currency": null,
          "amount": null
        }
      ],
      "items": [
        {
          "title": {
            "time_entry": ""
          },
          "time": 428000,
          "cur": null,
          "sum": null,
          "rate": null
        }
      ]
    },
    {
      "id": 162354173,
      "title": {
        "project": "読書",
        "client": null,
        "color": "0",
        "hex_color": "#0b83d9"
      },
      "time": 1983000,
      "total_currencies": [
        {
          "currency": null,
          "amount": null
        }
      ],
      "items": [
        {
          "title": {
            "time_entry": ""
          },
          "time": 1983000,
          "cur": null,
          "sum": null,
          "rate": null
        }
      ]
    }
  ]
}

jqで総作業時間を取得

total_grandが総作業時間(単位はms)

curl -u 1971800d4d82861d8f2c1651fea4d212:api_token -X GET "https://toggl.com/reports/api/v2/summary?user_agent=api_test&workspace_id=123&since=2020-07-24&until=2020-07-24" | jq .total_grand
6188000

参考

togglapidocs/reports.md at master · toggl/togglapidocs
togglapidocs/summary.md at master · toggl/togglapidocs


Pixela