Chilkat Online Tools

Xojo / New FreshBooks / Create a Time Entry

Back to Collection Items

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

Dim http As New Chilkat.Http
Dim success As Boolean

// Use this online tool to generate code from sample JSON: Generate Code to Create JSON

// The following JSON is sent in the request body.

// {
//   "time_entry": {
//     "is_logged": true,
//     "duration": 7200,
//     "note": "Stuff",
//     "started_at": "2016-08-16T20:00:00.000Z",
//     "client_id": 123,
//     "project_id": 123,
//     "service_id": 123
//   }
// }

Dim json As New Chilkat.JsonObject
success = json.UpdateBool("time_entry.is_logged",True)
success = json.UpdateInt("time_entry.duration",7200)
success = json.UpdateString("time_entry.note","Stuff")
success = json.UpdateString("time_entry.started_at","2016-08-16T20:00:00.000Z")
success = json.UpdateInt("time_entry.client_id",123)
success = json.UpdateInt("time_entry.project_id",123)
success = json.UpdateInt("time_entry.service_id",123)

// Adds the "Authorization: Bearer <access_token>" header.
http.AuthToken = "<access_token>"
http.SetRequestHeader "Content-Type",""

Dim resp As Chilkat.HttpResponse
resp = http.PostJson3("https://api.freshbooks.com/timetracking/business/{{businessId}}/time_entries","",json)
If (http.LastMethodSuccess = False) Then
    System.DebugLog(http.LastErrorText)
    Return
End If

Dim sbResponseBody As New Chilkat.StringBuilder
success = resp.GetBodySb(sbResponseBody)

Dim jResp As New Chilkat.JsonObject
success = jResp.LoadSb(sbResponseBody)
jResp.EmitCompact = False

System.DebugLog("Response Body:")
System.DebugLog(jResp.Emit())

Dim respStatusCode As Int32
respStatusCode = resp.StatusCode
System.DebugLog("Response Status Code = " + Str(respStatusCode))
If (respStatusCode >= 400) Then
    System.DebugLog("Response Header:")
    System.DebugLog(resp.Header)
    System.DebugLog("Failed.")

    Return
End If

// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)

// {
//   "time_entry": {
//     "id": 47902064,
//     "identity_id": 1882548,
//     "timer": null,
//     "is_logged": true,
//     "started_at": "2016-08-16T20:00:00Z",
//     "created_at": "2019-04-24T18:44:50Z",
//     "duration": 7200,
//     "client_id": 31006,
//     "project_id": 2989082,
//     "pending_client": null,
//     "pending_project": null,
//     "pending_task": null,
//     "task_id": null,
//     "service_id": null,
//     "note": "Stuff",
//     "active": true,
//     "billable": false,
//     "billed": false,
//     "internal": false,
//     "retainer_id": null
//   }
// }

// Sample code for parsing the JSON response...
// Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

Dim Id As Int32
Id = jResp.IntOf("time_entry.id")
Dim Identity_id As Int32
Identity_id = jResp.IntOf("time_entry.identity_id")
Dim Timer As String
Timer = jResp.StringOf("time_entry.timer")
Dim Is_logged As Boolean
Is_logged = jResp.BoolOf("time_entry.is_logged")
Dim Started_at As String
Started_at = jResp.StringOf("time_entry.started_at")
Dim Created_at As String
Created_at = jResp.StringOf("time_entry.created_at")
Dim Duration As Int32
Duration = jResp.IntOf("time_entry.duration")
Dim Client_id As Int32
Client_id = jResp.IntOf("time_entry.client_id")
Dim Project_id As Int32
Project_id = jResp.IntOf("time_entry.project_id")
Dim Pending_client As String
Pending_client = jResp.StringOf("time_entry.pending_client")
Dim Pending_project As String
Pending_project = jResp.StringOf("time_entry.pending_project")
Dim Pending_task As String
Pending_task = jResp.StringOf("time_entry.pending_task")
Dim Task_id As String
Task_id = jResp.StringOf("time_entry.task_id")
Dim Service_id As String
Service_id = jResp.StringOf("time_entry.service_id")
Dim Note As String
Note = jResp.StringOf("time_entry.note")
Dim Active As Boolean
Active = jResp.BoolOf("time_entry.active")
Dim Billable As Boolean
Billable = jResp.BoolOf("time_entry.billable")
Dim Billed As Boolean
Billed = jResp.BoolOf("time_entry.billed")
Dim Internal As Boolean
Internal = jResp.BoolOf("time_entry.internal")
Dim Retainer_id As String
Retainer_id = jResp.StringOf("time_entry.retainer_id")

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	-H "Content-Type: "
	-d '{
    "time_entry": {
        "is_logged": true,
        "duration": 7200,
        "note": "Stuff",
        "started_at": "2016-08-16T20:00:00.000Z",
        "client_id": {{clientId}},
        "project_id": {{projectId}}.
        "service_id": {{serviceid}}
    }
}'
https://api.freshbooks.com/timetracking/business/{{businessId}}/time_entries

Postman Collection Item JSON

{
  "name": "Create a Time Entry",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "var jsonData = JSON.parse(responseBody);",
          "postman.setEnvironmentVariable(\"timeEntryId\", jsonData.time_entry.id);"
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "",
        "name": "Content-Type",
        "value": "",
        "type": "text",
        "disabled": true
      },
      {
        "key": "Content-Type",
        "value": "",
        "disabled": true
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"time_entry\": {\n        \"is_logged\": true,\n        \"duration\": 7200,\n        \"note\": \"Stuff\",\n        \"started_at\": \"2016-08-16T20:00:00.000Z\",\n        \"client_id\": {{clientId}},\n        \"project_id\": {{projectId}}.\n        \"service_id\": {{serviceid}}\n    }\n}"
    },
    "url": {
      "raw": "https://api.freshbooks.com/timetracking/business/{{businessId}}/time_entries",
      "protocol": "https",
      "host": [
        "api",
        "freshbooks",
        "com"
      ],
      "path": [
        "timetracking",
        "business",
        "{{businessId}}",
        "time_entries"
      ]
    }
  },
  "response": [
    {
      "name": "Create a Time Entry",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "name": "Content-Type",
            "value": "application/json",
            "type": "text"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"time_entry\": {\n        \"is_logged\": true,\n        \"duration\": 7200,\n        \"note\": \"Stuff\",\n        \"started_at\": \"2016-08-16T20:00:00.000Z\",\n        \"client_id\": {{clientId}},\n        \"project_id\": {{projectId}}\n    }\n}"
        },
        "url": {
          "raw": "https://api.freshbooks.com/timetracking/business/{{businessId}}/time_entries",
          "protocol": "https",
          "host": [
            "api",
            "freshbooks",
            "com"
          ],
          "path": [
            "timetracking",
            "business",
            "{{businessId}}",
            "time_entries"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "nginx"
        },
        {
          "key": "Content-Type",
          "value": "application/vnd.api+json"
        },
        {
          "key": "X-Version",
          "value": "1904.25.0"
        },
        {
          "key": "X-NewRelic-App-Data",
          "value": "PxQBWV5TCBABVllUBAQOU10TGhE1AwE2QgNWEVlbQFtcCxYnRA9QFg1ZWU4FDFZFVRYSBxlDUhULRERQBxVNFlkPXT1dVkBAWgBKT0MEQA0RRFQEXBdRXlU7BAxDQ14DFxMaA05WTVMEVg1WCA8MBARRClAHVQNUSAQGVEpBWlYHVgQABwEBVQYDUgBUBUFOVgNUEV1l"
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "*"
        },
        {
          "key": "Expires",
          "value": "Tue, 24 Apr 2018 18:44:50 GMT"
        },
        {
          "key": "Cache-Control",
          "value": "no-cache"
        },
        {
          "key": "Via",
          "value": "1.1 google"
        },
        {
          "key": "Via",
          "value": "1.1 varnish"
        },
        {
          "key": "Content-Length",
          "value": "444"
        },
        {
          "key": "Accept-Ranges",
          "value": "bytes"
        },
        {
          "key": "Date",
          "value": "Wed, 24 Apr 2019 18:44:50 GMT"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "X-Served-By",
          "value": "cache-mdw17369-MDW"
        },
        {
          "key": "X-Cache",
          "value": "MISS"
        },
        {
          "key": "X-Cache-Hits",
          "value": "0"
        },
        {
          "key": "Country",
          "value": "CA"
        },
        {
          "key": "Strict-Transport-Security",
          "value": "max-age=31536000; includeSubDomains; preload"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"time_entry\": {\n        \"id\": 47902064,\n        \"identity_id\": 1882548,\n        \"timer\": null,\n        \"is_logged\": true,\n        \"started_at\": \"2016-08-16T20:00:00Z\",\n        \"created_at\": \"2019-04-24T18:44:50Z\",\n        \"duration\": 7200,\n        \"client_id\": 31006,\n        \"project_id\": 2989082,\n        \"pending_client\": null,\n        \"pending_project\": null,\n        \"pending_task\": null,\n        \"task_id\": null,\n        \"service_id\": null,\n        \"note\": \"Stuff\",\n        \"active\": true,\n        \"billable\": false,\n        \"billed\": false,\n        \"internal\": false,\n        \"retainer_id\": null\n    }\n}"
    }
  ]
}