Chilkat Online Tools

VB.NET / New FreshBooks / Create Single Project

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.

' {
'   "project": {
'     "title": "Titikaka Hiking Guide",
'     "client_id": 123,
'     "project_type": "fixed_price",
'     "fixed_price": "3000",
'     "due_date": "2019-04-18",
'     "services": [
'       {
'         "id": 123,
'         "billable": true,
'         "vis_state": 0
'       },
'       {
'         "id": 123,
'         "billable": true,
'         "vis_state": 0
'       },
'       {
'         "id": 123,
'         "billable": true,
'         "vis_state": 0
'       },
'       {
'         "id": 123,
'         "billable": true,
'         "vis_state": 0
'       }
'     ]
'   }
' }

Dim json As New Chilkat.JsonObject
json.UpdateString("project.title","Titikaka Hiking Guide")
json.UpdateInt("project.client_id",123)
json.UpdateString("project.project_type","fixed_price")
json.UpdateString("project.fixed_price","3000")
json.UpdateString("project.due_date","2019-04-18")
json.UpdateInt("project.services[0].id",123)
json.UpdateBool("project.services[0].billable",True)
json.UpdateInt("project.services[0].vis_state",0)
json.UpdateInt("project.services[1].id",123)
json.UpdateBool("project.services[1].billable",True)
json.UpdateInt("project.services[1].vis_state",0)
json.UpdateInt("project.services[2].id",123)
json.UpdateBool("project.services[2].billable",True)
json.UpdateInt("project.services[2].vis_state",0)
json.UpdateInt("project.services[3].id",123)
json.UpdateBool("project.services[3].billable",True)
json.UpdateInt("project.services[3].vis_state",0)

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

Dim resp As Chilkat.HttpResponse = http.PostJson3("https://api.freshbooks.com/projects/business/{{businessId}}/projects","application/json",json)
If (http.LastMethodSuccess = False) Then
    Debug.WriteLine(http.LastErrorText)
    Exit Sub
End If


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

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

Debug.WriteLine("Response Body:")
Debug.WriteLine(jResp.Emit())

Dim respStatusCode As Integer = resp.StatusCode
Debug.WriteLine("Response Status Code = " & respStatusCode)
If (respStatusCode >= 400) Then
    Debug.WriteLine("Response Header:")
    Debug.WriteLine(resp.Header)
    Debug.WriteLine("Failed.")

    Exit Sub
End If



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

' {
'   "project": {
'     "id": 3018266,
'     "title": "Titikaka Hiking Guide",
'     "description": null,
'     "due_date": "2019-04-18",
'     "client_id": 31006,
'     "internal": false,
'     "budget": null,
'     "fixed_price": "3000.00",
'     "rate": null,
'     "billing_method": null,
'     "project_type": "fixed_price",
'     "active": true,
'     "complete": false,
'     "sample": false,
'     "created_at": "2019-05-08T20:06:55",
'     "updated_at": "2019-05-08T20:06:55",
'     "logged_duration": null,
'     "services": [
'     ],
'     "billed_amount": "0.00",
'     "billed_status": "unbilled",
'     "retainer_id": null,
'     "group": {
'       "id": 5049390,
'       "members": [
'         {
'           "id": 6578346,
'           "identity_id": 37256,
'           "role": "owner",
'           "first_name": "Marshall",
'           "last_name": "Johnston",
'           "email": "api.freshbooks@freshbooks.com",
'           "company": "Postman Sandbox",
'           "active": true
'         }
'       ],
'       "pending_invitations": 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 Integer
Dim identity_id As Integer
Dim role As String
Dim first_name As String
Dim last_name As String
Dim email As String
Dim company As String
Dim active As Boolean

Dim Id As Integer = jResp.IntOf("project.id")
Dim Title As String = jResp.StringOf("project.title")
Dim Description As String = jResp.StringOf("project.description")
Dim Due_date As String = jResp.StringOf("project.due_date")
Dim Client_id As Integer = jResp.IntOf("project.client_id")
Dim Internal As Boolean = jResp.BoolOf("project.internal")
Dim Budget As String = jResp.StringOf("project.budget")
Dim Fixed_price As String = jResp.StringOf("project.fixed_price")
Dim Rate As String = jResp.StringOf("project.rate")
Dim Billing_method As String = jResp.StringOf("project.billing_method")
Dim Project_type As String = jResp.StringOf("project.project_type")
Dim Active As Boolean = jResp.BoolOf("project.active")
Dim Complete As Boolean = jResp.BoolOf("project.complete")
Dim Sample As Boolean = jResp.BoolOf("project.sample")
Dim Created_at As String = jResp.StringOf("project.created_at")
Dim Updated_at As String = jResp.StringOf("project.updated_at")
Dim Logged_duration As String = jResp.StringOf("project.logged_duration")
Dim Billed_amount As String = jResp.StringOf("project.billed_amount")
Dim Billed_status As String = jResp.StringOf("project.billed_status")
Dim Retainer_id As String = jResp.StringOf("project.retainer_id")
Dim GroupId As Integer = jResp.IntOf("project.group.id")
Dim Pending_invitations As String = jResp.StringOf("project.group.pending_invitations")
Dim i As Integer = 0
Dim count_i As Integer = jResp.SizeOfArray("project.services")
While i < count_i
    jResp.I = i
    i = i + 1
End While
i = 0
count_i = jResp.SizeOfArray("project.group.members")
While i < count_i
    jResp.I = i
    id = jResp.IntOf("project.group.members[i].id")
    identity_id = jResp.IntOf("project.group.members[i].identity_id")
    role = jResp.StringOf("project.group.members[i].role")
    first_name = jResp.StringOf("project.group.members[i].first_name")
    last_name = jResp.StringOf("project.group.members[i].last_name")
    email = jResp.StringOf("project.group.members[i].email")
    company = jResp.StringOf("project.group.members[i].company")
    active = jResp.BoolOf("project.group.members[i].active")
    i = i + 1
End While

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	-H "Content-Type: application/json"
	-d '{
  "project": {
    "title": "Titikaka Hiking Guide",
    "client_id": {{customerId}},
    "project_type": "fixed_price",
    "fixed_price": "3000",
    "due_date": "2019-04-18",
    "services": 
    [
    	{"id": {{serviceid}}, "billable": true, "vis_state": 0}, {"id": {{serviceid}},"billable": true, "vis_state": 0}, {"id": {{serviceid}}, "billable": true, "vis_state": 0}, {"id": {{serviceid}}, "billable": true, "vis_state": 0}
    	]
  }
}'
https://api.freshbooks.com/projects/business/{{businessId}}/projects

Postman Collection Item JSON

{
  "name": "Create Single Project",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "let jsonData = JSON.parse(responseBody);",
          "pm.environment.set(\"projectId\", jsonData.project.id);",
          ""
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "type": "text",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n  \"project\": {\n    \"title\": \"Titikaka Hiking Guide\",\n    \"client_id\": {{customerId}},\n    \"project_type\": \"fixed_price\",\n    \"fixed_price\": \"3000\",\n    \"due_date\": \"2019-04-18\",\n    \"services\": \n    [\n    \t{\"id\": {{serviceid}}, \"billable\": true, \"vis_state\": 0}, {\"id\": {{serviceid}},\"billable\": true, \"vis_state\": 0}, {\"id\": {{serviceid}}, \"billable\": true, \"vis_state\": 0}, {\"id\": {{serviceid}}, \"billable\": true, \"vis_state\": 0}\n    \t]\n  }\n}"
    },
    "url": {
      "raw": "https://api.freshbooks.com/projects/business/{{businessId}}/projects",
      "protocol": "https",
      "host": [
        "api",
        "freshbooks",
        "com"
      ],
      "path": [
        "projects",
        "business",
        "{{businessId}}",
        "projects"
      ]
    }
  },
  "response": [
    {
      "name": "Create Single Project",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "type": "text",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"project\": {\n    \"title\": \"Titikaka Hiking Guide\",\n    \"client_id\": {{customerId}},\n    \"project_type\": \"fixed_price\",\n    \"fixed_price\": \"3000\",\n    \"due_date\": \"2019-04-18\"\n  }\n}"
        },
        "url": {
          "raw": "https://api.freshbooks.com/projects/business/{{businessId}}/projects",
          "protocol": "https",
          "host": [
            "api",
            "freshbooks",
            "com"
          ],
          "path": [
            "projects",
            "business",
            "{{businessId}}",
            "projects"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "nginx"
        },
        {
          "key": "Content-Type",
          "value": "application/vnd.api+json"
        },
        {
          "key": "X-Version",
          "value": "1905.7.0"
        },
        {
          "key": "X-NewRelic-App-Data",
          "value": "PxQBWV5TCBABVllUBAQOU10TGhE1AwE2QgNWEVlbQFtcCxYnRA9QFg1ZWU4FDFZFVRYSBxlDUhULRERQBxVNEkINUgdbTEccQQBKDkQTUAdeRkUODAZbR0NGTVIbARlUVAcEB1VTVVoBUQhWAQsFBB9UDlQdQ1dSVVBSBFFRD1dVVldVAAIVSgJQWkAHOw=="
        },
        {
          "key": "X-RateLimit-Limit",
          "value": "20"
        },
        {
          "key": "X-RateLimit-Remaining",
          "value": "8"
        },
        {
          "key": "X-RateLimit-Reset",
          "value": "1557346076"
        },
        {
          "key": "Retry-After",
          "value": "60"
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "*"
        },
        {
          "key": "Expires",
          "value": "Tue, 08 May 2018 20:06:55 GMT"
        },
        {
          "key": "Cache-Control",
          "value": "no-cache"
        },
        {
          "key": "Via",
          "value": "1.1 google"
        },
        {
          "key": "Via",
          "value": "1.1 varnish"
        },
        {
          "key": "Content-Length",
          "value": "759"
        },
        {
          "key": "Accept-Ranges",
          "value": "bytes"
        },
        {
          "key": "Date",
          "value": "Wed, 08 May 2019 20:06:55 GMT"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "X-Served-By",
          "value": "cache-mdw17361-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    \"project\": {\n        \"id\": 3018266,\n        \"title\": \"Titikaka Hiking Guide\",\n        \"description\": null,\n        \"due_date\": \"2019-04-18\",\n        \"client_id\": 31006,\n        \"internal\": false,\n        \"budget\": null,\n        \"fixed_price\": \"3000.00\",\n        \"rate\": null,\n        \"billing_method\": null,\n        \"project_type\": \"fixed_price\",\n        \"active\": true,\n        \"complete\": false,\n        \"sample\": false,\n        \"created_at\": \"2019-05-08T20:06:55\",\n        \"updated_at\": \"2019-05-08T20:06:55\",\n        \"logged_duration\": null,\n        \"services\": [],\n        \"billed_amount\": \"0.00\",\n        \"billed_status\": \"unbilled\",\n        \"retainer_id\": null,\n        \"group\": {\n            \"id\": 5049390,\n            \"members\": [\n                {\n                    \"id\": 6578346,\n                    \"identity_id\": 37256,\n                    \"role\": \"owner\",\n                    \"first_name\": \"Marshall\",\n                    \"last_name\": \"Johnston\",\n                    \"email\": \"api.freshbooks@freshbooks.com\",\n                    \"company\": \"Postman Sandbox\",\n                    \"active\": true\n                }\n            ],\n            \"pending_invitations\": null\n        }\n    }\n}"
    }
  ]
}