Search code examples
google-apps-scriptclockify

POST Request to Clockify via Google Scripts


I am attempting to use the Clockify API (documentation here: https://clockify.me/developers-api#tag-Client) to add clients based on the name of a given spreadsheet in Google Sheets. My code is as follows:

function ClockifyManAdd() {
var filedata = {
  'name' : ss.getSheetName()
  };
var headers = {"X-Api-Key" : "[MYAPIKEY]", "content-type" : "application/json"};
var payload = {'name' : JSON.stringify(filedata)};
var clockifyoptions = {
  'method' : 'post',
  'headers' : headers,
  'payload' : payload
  };
UrlFetchApp.fetch('https://api.clockify.me/api/v1/workspaces/[MYWORKSPACEID]/clients/', clockifyoptions);
}

This code returns an Error 400 - Bad Request (particularly around the "name" token; the error claims that it was expecting "True, False, or Null"). Can anyone point me in the direction of what I'm doing wrong?

Thanks a ton.


Solution

  • Issue/Solution:

    • Nested payload object: Your payload has {name:{name: client}}. According to documentation, request body should be {name:client}.

    Snippet:

    var payload = JSON.stringify({'name' : ss.getSheetName()});//Assumin ss is sheet
    

    References: