I'm trying to use a Google Script to start a timer in Clockify. The script does not give an error (such as 400 Bad Request), but no timer is ever started.
API Documentation at https://clockify.me/developers-api#operation--v1-workspaces--workspaceId--time-entries-post
function StartClockifyTimer() {
// Starts a timer based on the current sheet
// Step 1: Find PID
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var FileNo = sheet.getSheetName();
var PIDheaders = {"X-Api-Key" : "[apikey]", "content-type" : "application/json"};
//var PIDpayload = JSON.stringify({'name' : FileNo});
var PIDoptions = {
'muteHttpExceptions' : true,
'method' : 'get',
'headers' : PIDheaders,
//'params' : PIDpayload
};
var PID = UrlFetchApp.getRequest('https://api.clockify.me/api/v1/workspaces/[wid]/projects/', PIDoptions);
for(i in PID) {
Logger.log(i + ": " + PID[i]);
}
//Step 2: Use PID to start timer
timezone = "GMT+" + new Date().getTimezoneOffset()/60;
var date = Utilities.formatDate(new Date(), timezone, "yyyy-MM-dd'T'HH:mm:ss'Z'");
var headers = {"X-Api-Key" : "[apikey]", "content-type" : "application/json"};
var payload = JSON.stringify({'start' : date, 'projectId' : PID});
var clockifyoptions = {
'muteHttpExceptions' : true,
'method' : 'post',
'headers' : headers,
'payload' : payload
};
var r = UrlFetchApp.fetch('https://api.clockify.me/api/v1/workspaces/[wid]/time-entries/', clockifyoptions);
Logger.log(r);
}
--EDIT-- Since posting, and thanks to the advice received, I've added in a new step in the process to find the PID by name. However, I cannot make the GET request work correctly. I need to query 'name' by FileNo as described in https://clockify.me/developers-api#operation--v1-workspaces--workspaceId--projects-get, but I can't get the GET request down pat. Even the above code with the commented out sections returns:
[19-08-01 11:37:46:024 MDT] getAs: function getAs() {/* */}
[19-08-01 11:37:46:025 MDT] getHeaders: function getHeaders() {/* */}
[19-08-01 11:37:46:025 MDT] getContentText: function getContentText() {/* */}
[19-08-01 11:37:46:026 MDT] getContent: function getContent() {/* */}
[19-08-01 11:37:46:026 MDT] getResponseCode: function getResponseCode() {/* */}
[19-08-01 11:37:46:027 MDT] getAllHeaders: function getAllHeaders() {/* */}
[19-08-01 11:37:46:027 MDT] toString: function toString() {/* */}
[19-08-01 11:37:46:028 MDT] getBlob: function getBlob() {/* */}
I've since confirmed that using a hardcoded ProjectID in Step 2 works, so what I need help with is actually Step 1 - finding the PID. Can anyone help me format the GET request?
Thanks a ton.
GOT IT. Leaving the code here for anyone else trying to hit Clockify's APIs.
function StartClockifyTimer() {
// Starts a timer based on the current sheet
// Step 1: Find PID
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var FileNo = sheet.getSheetName();
var url = 'https://api.clockify.me/api/v1/workspaces/[workspaceid]/projects?name='+FileNo
const header = {
"headers": {
"X-Api-Key" : "[apikey]",
"content-type" : "application/json",
"Accept" : "*/*"
}
};
var response = UrlFetchApp.fetch(url, header)
var json = response.getContentText();
var data = JSON.parse(json);
var PID = data[0]["id"];
Logger.log(PID);
//Step 2: Use PID to start timer
timezone = "GMT+" + new Date().getTimezoneOffset();
var date = Utilities.formatDate(new Date(), timezone, "yyyy-MM-dd'T'HH:mm:ss'Z'");
var headers = {"X-Api-Key" : "[apikey]", "content-type" : "application/json"};
var payload = JSON.stringify({'start' : date, 'projectId' : PID});
var clockifyoptions = {
'muteHttpExceptions' : true,
'method' : 'post',
'headers' : headers,
'payload' : payload
};
UrlFetchApp.fetch('https://api.clockify.me/api/v1/workspaces/[workspaceid]/time-entries/', clockifyoptions);
}