Clockify - How to get all time entries (not just first page)

863 views Asked by At

I'm trying to get all time entries from my Clockify workspace using the following line of code in Python:

import boto3
import json
import requests
import pprint

pp = pprint.PrettyPrinter(indent=2)

url = "https://reports.api.clockify.me/v1/workspaces/{workspaceID}/reports/detailed/"

x=list(range(1,5373,1))
for i in x:
  pagenumber=i
  payload1="{\r\n  \"dateRangeStart\": \"2020-12-10T00:00:00.000\",\r\n  \"dateRangeEnd\": \"2021-03-31T23:59:59.000\",\r\n  \"detailedFilter\": {\r\n    \"page\": "
  payload2=f"{pagenumber}"
  payload3=",\r\n    \"pageSize\": 200}\r\n}"
  payload = payload1+payload2+payload3
  headers = {
    'X-Api-Key': {APIKEY},
    'Content-Type': 'application/json'}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)

It works, but I only get info for the first page out of the 5373 pages. Is there any way I can loop through them in a better way? The for loop I have there doesn't seem to be working properly. Or maybe Clockify is stopping my requests due to their API limits? I'd appreciate any help with this, I'm not an experienced developer by any means!

1

There are 1 answers

0
Adam DS On

It looks like your response object and print command are outside the for loop. This will result in only the first page being returned and then the script stopping instead of looping through all the pages.

for i in x:
  pagenumber=i
  payload1="{\r\n  \"dateRangeStart\": \"2020-12-10T00:00:00.000\",\r\n  \"dateRangeEnd\": \"2021-03-31T23:59:59.000\",\r\n  \"detailedFilter\": {\r\n    \"page\": "
  payload2=f"{pagenumber}"
  payload3=",\r\n    \"pageSize\": 200}\r\n}"
  payload = payload1+payload2+payload3
  headers = {
    'X-Api-Key': {APIKEY},
    'Content-Type': 'application/json'}

  response = requests.request("POST", url, headers=headers, data=payload)

  print(response.text)

Clockify returns an "entriesCount" field within the "totals" object of the JSON response. I would suggest using that to calculate your pages instead of hardcoding the pages as timesheet entries are created quickly so while you may have 5373 pages right now, it will most likely be more even as I type this.