Chilkat Online Tools

PureBasic / Zoho CRM REST APIs / Products

Back to Collection Items

IncludeFile "CkJsonObject.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkHttpResponse.pb"

Procedure ChilkatExample()

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

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success.i

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

    ; The following JSON is sent in the request body.

    ; {
    ;   "data": [
    ;     {
    ;       "Product_Category": "Hardware",
    ;       "Description": "This is a sample product",
    ;       "Vendor_Name": {
    ;         "name": "Vendor2",
    ;         "id": "{{vendor_id}}"
    ;       },
    ;       "Tax": [
    ;         "Vat - 5.0 %"
    ;       ],
    ;       "Sales_Start_Date": "2021-01-22",
    ;       "Subproducts": [
    ;         {
    ;           "Discount": null,
    ;           "Rate": 20,
    ;           "Amount": 20,
    ;           "Parent_Id": {
    ;             "name": "Sample Product1",
    ;             "id": "{{product_id}}"
    ;           },
    ;           "Quantity": 1,
    ;           "id": "4150868000004382023",
    ;           "Vendor": {
    ;             "name": "Zylker",
    ;             "id": "{{vendor_id}}"
    ;           },
    ;           "Add_on_name": {
    ;             "name": "Sample 2",
    ;             "id": "4150868000003278032"
    ;           }
    ;         }
    ;       ],
    ;       "Manufacturer": "AltvetPet Inc.",
    ;       "Support_Expiry_Date": "2021-01-31",
    ;       "Product_Name": "Sample Product1",
    ;       "Support_Start_Date": "2021-01-23",
    ;       "Usage_Unit": "Box",
    ;       "Qty_Ordered": 12,
    ;       "Qty_in_Stock": 20,
    ;       "Sales_End_Date": "2021-01-31",
    ;       "Unit_Price": 100
    ;     }
    ;   ]
    ; }

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(json,"data[0].Product_Category","Hardware")
    CkJsonObject::ckUpdateString(json,"data[0].Description","This is a sample product")
    CkJsonObject::ckUpdateString(json,"data[0].Vendor_Name.name","Vendor2")
    CkJsonObject::ckUpdateString(json,"data[0].Vendor_Name.id","{{vendor_id}}")
    CkJsonObject::ckUpdateString(json,"data[0].Tax[0]","Vat - 5.0 %")
    CkJsonObject::ckUpdateString(json,"data[0].Sales_Start_Date","2021-01-22")
    CkJsonObject::ckUpdateNull(json,"data[0].Subproducts[0].Discount")
    CkJsonObject::ckUpdateInt(json,"data[0].Subproducts[0].Rate",20)
    CkJsonObject::ckUpdateInt(json,"data[0].Subproducts[0].Amount",20)
    CkJsonObject::ckUpdateString(json,"data[0].Subproducts[0].Parent_Id.name","Sample Product1")
    CkJsonObject::ckUpdateString(json,"data[0].Subproducts[0].Parent_Id.id","{{product_id}}")
    CkJsonObject::ckUpdateInt(json,"data[0].Subproducts[0].Quantity",1)
    CkJsonObject::ckUpdateString(json,"data[0].Subproducts[0].id","4150868000004382023")
    CkJsonObject::ckUpdateString(json,"data[0].Subproducts[0].Vendor.name","Zylker")
    CkJsonObject::ckUpdateString(json,"data[0].Subproducts[0].Vendor.id","{{vendor_id}}")
    CkJsonObject::ckUpdateString(json,"data[0].Subproducts[0].Add_on_name.name","Sample 2")
    CkJsonObject::ckUpdateString(json,"data[0].Subproducts[0].Add_on_name.id","4150868000003278032")
    CkJsonObject::ckUpdateString(json,"data[0].Manufacturer","AltvetPet Inc.")
    CkJsonObject::ckUpdateString(json,"data[0].Support_Expiry_Date","2021-01-31")
    CkJsonObject::ckUpdateString(json,"data[0].Product_Name","Sample Product1")
    CkJsonObject::ckUpdateString(json,"data[0].Support_Start_Date","2021-01-23")
    CkJsonObject::ckUpdateString(json,"data[0].Usage_Unit","Box")
    CkJsonObject::ckUpdateInt(json,"data[0].Qty_Ordered",12)
    CkJsonObject::ckUpdateInt(json,"data[0].Qty_in_Stock",20)
    CkJsonObject::ckUpdateString(json,"data[0].Sales_End_Date","2021-01-31")
    CkJsonObject::ckUpdateInt(json,"data[0].Unit_Price",100)

    ; Adds the "Authorization: Bearer <access_token>" header.
    CkHttp::setCkAuthToken(http, "<access_token>")

    resp.i = CkHttp::ckPostJson3(http,"https://domain.com/crm/v2/Products/upsert","application/json",json)
    If CkHttp::ckLastMethodSuccess(http) = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

    Debug Str(CkHttpResponse::ckStatusCode(resp))
    Debug CkHttpResponse::ckBodyStr(resp)
    CkHttpResponse::ckDispose(resp)



    CkHttp::ckDispose(http)
    CkJsonObject::ckDispose(json)


    ProcedureReturn
EndProcedure

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	-d '{
    "data": [
        {
            "Product_Category": "Hardware",
            "Description": "This is a sample product",
            "Vendor_Name": {
                "name": "Vendor2",
                "id": "{{vendor_id}}"
            },
            "Tax": [
                "Vat - 5.0 %"
            ],
            "Sales_Start_Date": "2021-01-22",
            "Subproducts": [
                {
                    "Discount": null,
                    "Rate": 20,
                    "Amount": 20,
                    "Parent_Id": {
                        "name": "Sample Product1",
                        "id": "{{product_id}}"
                    },
                    "Quantity": 1,
                    "id": "4150868000004382023",
                    "Vendor": {
                        "name": "Zylker",
                        "id": "{{vendor_id}}"
                    },
                    "Add_on_name": {
                        "name": "Sample 2",
                        "id": "4150868000003278032"
                    }
                }
            ],
            "Manufacturer": "AltvetPet Inc.",
            "Support_Expiry_Date": "2021-01-31",
            "Product_Name": "Sample Product1",
            "Support_Start_Date": "2021-01-23",
            "Usage_Unit": "Box",
            "Qty_Ordered": 12,
            "Qty_in_Stock": 20,
            "Sales_End_Date": "2021-01-31",
            "Unit_Price": 100
        }
    ]
}'
https://domain.com/crm/v2/Products/upsert

Postman Collection Item JSON

{
  "name": "Products",
  "request": {
    "auth": {
      "type": "bearer",
      "bearer": [
        {
          "key": "token",
          "value": "{{access-token}}",
          "type": "string"
        }
      ]
    },
    "method": "POST",
    "header": [
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"data\": [\n        {\n            \"Product_Category\": \"Hardware\",\n            \"Description\": \"This is a sample product\",\n            \"Vendor_Name\": {\n                \"name\": \"Vendor2\",\n                \"id\": \"{{vendor_id}}\"\n            },\n            \"Tax\": [\n                \"Vat - 5.0 %\"\n            ],\n            \"Sales_Start_Date\": \"2021-01-22\",\n            \"Subproducts\": [\n                {\n                    \"Discount\": null,\n                    \"Rate\": 20,\n                    \"Amount\": 20,\n                    \"Parent_Id\": {\n                        \"name\": \"Sample Product1\",\n                        \"id\": \"{{product_id}}\"\n                    },\n                    \"Quantity\": 1,\n                    \"id\": \"4150868000004382023\",\n                    \"Vendor\": {\n                        \"name\": \"Zylker\",\n                        \"id\": \"{{vendor_id}}\"\n                    },\n                    \"Add_on_name\": {\n                        \"name\": \"Sample 2\",\n                        \"id\": \"4150868000003278032\"\n                    }\n                }\n            ],\n            \"Manufacturer\": \"AltvetPet Inc.\",\n            \"Support_Expiry_Date\": \"2021-01-31\",\n            \"Product_Name\": \"Sample Product1\",\n            \"Support_Start_Date\": \"2021-01-23\",\n            \"Usage_Unit\": \"Box\",\n            \"Qty_Ordered\": 12,\n            \"Qty_in_Stock\": 20,\n            \"Sales_End_Date\": \"2021-01-31\",\n            \"Unit_Price\": 100\n        }\n    ]\n}",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{api-domain}}/crm/v2/Products/upsert",
      "host": [
        "{{api-domain}}"
      ],
      "path": [
        "crm",
        "v2",
        "Products",
        "upsert"
      ]
    },
    "description": "To upsert records in the Products module. "
  },
  "response": [
  ]
}