Sample code for 30+ languages & platforms
PowerBuilder

Shopify Create New Product

See more Shopify Examples

Create a new product

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Rest
oleobject loo_JsonReq
oleobject loo_SbReq
oleobject loo_SbJson
oleobject loo_Json
integer li_ProductId
string ls_ProductTitle
string ls_ProductBody_html
string ls_ProductVendor
string ls_ProductProduct_type
string ls_ProductCreated_at
string ls_ProductHandle
string ls_ProductUpdated_at
string ls_ProductPublished_at
integer li_ProductTemplate_suffix
string ls_ProductPublished_scope
string ls_ProductTags
integer li_ProductImageId
integer li_ProductImageProduct_id
integer li_ProductImagePosition
string ls_ProductImageCreated_at
string ls_ProductImageUpdated_at
integer li_ProductImageWidth
integer li_ProductImageHeight
string ls_ProductImageSrc
integer i
integer li_Count_i
integer li_Id
integer li_Product_id
string ls_Title
string ls_Price
string ls_Sku
integer li_Position
integer li_Grams
string ls_Inventory_policy
integer li_Compare_at_price
string ls_Fulfillment_service
integer li_Inventory_management
string ls_Option1
integer li_Option2
integer li_Option3
string ls_Created_at
string ls_Updated_at
integer li_Taxable
integer li_Barcode
integer li_Image_id
integer li_Inventory_quantity
integer li_Weight
string ls_Weight_unit
integer li_Old_inventory_quantity
integer li_Requires_shipping
string ls_Name
integer j
integer li_Count_j
string ls_StrVal
integer li_Width
integer li_Height
string ls_Src

li_Success = 0

loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")
if li_rc < 0 then
    destroy loo_Rest
    MessageBox("Error","Connecting to COM object failed")
    return
end if

loo_Rest.SetAuthBasic("SHOPIFY_PRIVATE_API_KEY","SHOPIFY_PRIVATE_API_SECRET_KEY")

li_Success = loo_Rest.Connect("chilkat.myshopify.com",443,1,1)
if li_Success <> 1 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    return
end if

// The following code creates the JSON request body.
// The JSON created by this code is shown below.
loo_JsonReq = create oleobject
li_rc = loo_JsonReq.ConnectToNewObject("Chilkat.JsonObject")

loo_JsonReq.UpdateString("product.title","Burton Custom Freestyle 151")
loo_JsonReq.UpdateString("product.body_html","<strong>Good snowboard!</strong>")
loo_JsonReq.UpdateString("product.vendor","Burton")
loo_JsonReq.UpdateString("product.product_type","Snowboard")
loo_JsonReq.UpdateString("product.images[0].attachment","R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==")

// The JSON request body created by the above code:

// {
//   "product": {
//     "title": "Burton Custom Freestyle 151",
//     "body_html": "<strong>Good snowboard!<\/strong>",
//     "vendor": "Burton",
//     "product_type": "Snowboard",
//     "images": [
//       {
//         "attachment": "R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="
//       }
//     ]
//   }
// }

loo_SbReq = create oleobject
li_rc = loo_SbReq.ConnectToNewObject("Chilkat.StringBuilder")

loo_JsonReq.EmitSb(loo_SbReq)

loo_Rest.AddHeader("Content-Type","application/json")

loo_SbJson = create oleobject
li_rc = loo_SbJson.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_Rest.FullRequestSb("POST","/admin/products.json",loo_SbReq,loo_SbJson)
if li_Success <> 1 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    destroy loo_JsonReq
    destroy loo_SbReq
    destroy loo_SbJson
    return
end if

if loo_Rest.ResponseStatusCode <> 201 then
    Write-Debug "Received error response code: " + string(loo_Rest.ResponseStatusCode)
    Write-Debug "Response body:"
    Write-Debug loo_SbJson.GetAsString()
    destroy loo_Rest
    destroy loo_JsonReq
    destroy loo_SbReq
    destroy loo_SbJson
    return
end if

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_Json.LoadSb(loo_SbJson)

// The following code parses the JSON response.
// A sample JSON response is shown below the sample code.

li_ProductId = loo_Json.IntOf("product.id")
ls_ProductTitle = loo_Json.StringOf("product.title")
ls_ProductBody_html = loo_Json.StringOf("product.body_html")
ls_ProductVendor = loo_Json.StringOf("product.vendor")
ls_ProductProduct_type = loo_Json.StringOf("product.product_type")
ls_ProductCreated_at = loo_Json.StringOf("product.created_at")
ls_ProductHandle = loo_Json.StringOf("product.handle")
ls_ProductUpdated_at = loo_Json.StringOf("product.updated_at")
ls_ProductPublished_at = loo_Json.StringOf("product.published_at")
li_ProductTemplate_suffix = loo_Json.IsNullOf("product.template_suffix")
ls_ProductPublished_scope = loo_Json.StringOf("product.published_scope")
ls_ProductTags = loo_Json.StringOf("product.tags")
li_ProductImageId = loo_Json.IntOf("product.image.id")
li_ProductImageProduct_id = loo_Json.IntOf("product.image.product_id")
li_ProductImagePosition = loo_Json.IntOf("product.image.position")
ls_ProductImageCreated_at = loo_Json.StringOf("product.image.created_at")
ls_ProductImageUpdated_at = loo_Json.StringOf("product.image.updated_at")
li_ProductImageWidth = loo_Json.IntOf("product.image.width")
li_ProductImageHeight = loo_Json.IntOf("product.image.height")
ls_ProductImageSrc = loo_Json.StringOf("product.image.src")
i = 0
li_Count_i = loo_Json.SizeOfArray("product.variants")
do while i < li_Count_i
    loo_Json.I = i
    li_Id = loo_Json.IntOf("product.variants[i].id")
    li_Product_id = loo_Json.IntOf("product.variants[i].product_id")
    ls_Title = loo_Json.StringOf("product.variants[i].title")
    ls_Price = loo_Json.StringOf("product.variants[i].price")
    ls_Sku = loo_Json.StringOf("product.variants[i].sku")
    li_Position = loo_Json.IntOf("product.variants[i].position")
    li_Grams = loo_Json.IntOf("product.variants[i].grams")
    ls_Inventory_policy = loo_Json.StringOf("product.variants[i].inventory_policy")
    li_Compare_at_price = loo_Json.IsNullOf("product.variants[i].compare_at_price")
    ls_Fulfillment_service = loo_Json.StringOf("product.variants[i].fulfillment_service")
    li_Inventory_management = loo_Json.IsNullOf("product.variants[i].inventory_management")
    ls_Option1 = loo_Json.StringOf("product.variants[i].option1")
    li_Option2 = loo_Json.IsNullOf("product.variants[i].option2")
    li_Option3 = loo_Json.IsNullOf("product.variants[i].option3")
    ls_Created_at = loo_Json.StringOf("product.variants[i].created_at")
    ls_Updated_at = loo_Json.StringOf("product.variants[i].updated_at")
    li_Taxable = loo_Json.BoolOf("product.variants[i].taxable")
    li_Barcode = loo_Json.IsNullOf("product.variants[i].barcode")
    li_Image_id = loo_Json.IsNullOf("product.variants[i].image_id")
    li_Inventory_quantity = loo_Json.IntOf("product.variants[i].inventory_quantity")
    li_Weight = loo_Json.IntOf("product.variants[i].weight")
    ls_Weight_unit = loo_Json.StringOf("product.variants[i].weight_unit")
    li_Old_inventory_quantity = loo_Json.IntOf("product.variants[i].old_inventory_quantity")
    li_Requires_shipping = loo_Json.BoolOf("product.variants[i].requires_shipping")
    i = i + 1
loop
i = 0
li_Count_i = loo_Json.SizeOfArray("product.options")
do while i < li_Count_i
    loo_Json.I = i
    li_Id = loo_Json.IntOf("product.options[i].id")
    li_Product_id = loo_Json.IntOf("product.options[i].product_id")
    ls_Name = loo_Json.StringOf("product.options[i].name")
    li_Position = loo_Json.IntOf("product.options[i].position")
    j = 0
    li_Count_j = loo_Json.SizeOfArray("product.options[i].values")
    do while j < li_Count_j
        loo_Json.J = j
        ls_StrVal = loo_Json.StringOf("product.options[i].values[j]")
        j = j + 1
    loop
    i = i + 1
loop
i = 0
li_Count_i = loo_Json.SizeOfArray("product.images")
do while i < li_Count_i
    loo_Json.I = i
    li_Id = loo_Json.IntOf("product.images[i].id")
    li_Product_id = loo_Json.IntOf("product.images[i].product_id")
    li_Position = loo_Json.IntOf("product.images[i].position")
    ls_Created_at = loo_Json.StringOf("product.images[i].created_at")
    ls_Updated_at = loo_Json.StringOf("product.images[i].updated_at")
    li_Width = loo_Json.IntOf("product.images[i].width")
    li_Height = loo_Json.IntOf("product.images[i].height")
    ls_Src = loo_Json.StringOf("product.images[i].src")
    j = 0
    li_Count_j = loo_Json.SizeOfArray("product.images[i].variant_ids")
    do while j < li_Count_j
        loo_Json.J = j
        j = j + 1
    loop
    i = i + 1
loop
i = 0
li_Count_i = loo_Json.SizeOfArray("image.product.variant_ids")
do while i < li_Count_i
    loo_Json.I = i
    i = i + 1
loop

// A sample JSON response body that is parsed by the above code:

// {
//   "product": {
//     "id": 1071559747,
//     "title": "Burton Custom Freestyle 151",
//     "body_html": "<strong>Good snowboard!<\/strong>",
//     "vendor": "Burton",
//     "product_type": "Snowboard",
//     "created_at": "2017-09-22T14:48:43-04:00",
//     "handle": "burton-custom-freestyle-151",
//     "updated_at": "2017-09-22T14:48:44-04:00",
//     "published_at": "2017-09-22T14:48:43-04:00",
//     "template_suffix": null,
//     "published_scope": "global",
//     "tags": "",
//     "variants": [
//       {
//         "id": 1070325218,
//         "product_id": 1071559747,
//         "title": "Default Title",
//         "price": "0.00",
//         "sku": "",
//         "position": 1,
//         "grams": 0,
//         "inventory_policy": "deny",
//         "compare_at_price": null,
//         "fulfillment_service": "manual",
//         "inventory_management": null,
//         "option1": "Default Title",
//         "option2": null,
//         "option3": null,
//         "created_at": "2017-09-22T14:48:44-04:00",
//         "updated_at": "2017-09-22T14:48:44-04:00",
//         "taxable": true,
//         "barcode": null,
//         "image_id": null,
//         "inventory_quantity": 1,
//         "weight": 0.0,
//         "weight_unit": "lb",
//         "old_inventory_quantity": 1,
//         "requires_shipping": true
//       }
//     ],
//     "options": [
//       {
//         "id": 1022828903,
//         "product_id": 1071559747,
//         "name": "Title",
//         "position": 1,
//         "values": [
//           "Default Title"
//         ]
//       }
//     ],
//     "images": [
//       {
//         "id": 1001473945,
//         "product_id": 1071559747,
//         "position": 1,
//         "created_at": "2017-09-22T14:48:43-04:00",
//         "updated_at": "2017-09-22T14:48:43-04:00",
//         "width": 1,
//         "height": 1,
//         "src": "https:\/\/cdn.shopify.com\/s\/files\/1\/0006\/9093\/3842\/products\/df3e567d6f16d040326c7a0ea29a4f41.gif?v=1506106123",
//         "variant_ids": [
//         ]
//       }
//     ],
//     "image": {
//       "id": 1001473945,
//       "product_id": 1071559747,
//       "position": 1,
//       "created_at": "2017-09-22T14:48:43-04:00",
//       "updated_at": "2017-09-22T14:48:43-04:00",
//       "width": 1,
//       "height": 1,
//       "src": "https:\/\/cdn.shopify.com\/s\/files\/1\/0006\/9093\/3842\/products\/df3e567d6f16d040326c7a0ea29a4f41.gif?v=1506106123",
//       "variant_ids": [
//       ]
//     }
//   }
// }

Write-Debug "Example Completed."


destroy loo_Rest
destroy loo_JsonReq
destroy loo_SbReq
destroy loo_SbJson
destroy loo_Json