Sample code for 30+ languages & platforms
Xbase++

Shopify Create New Product

See more Shopify Examples

Create a new product

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oRest
LOCAL oJsonReq
LOCAL oSbReq
LOCAL oSbJson
LOCAL oJson
LOCAL nProductId
LOCAL cProductTitle
LOCAL cProductBody_html
LOCAL cProductVendor
LOCAL cProductProduct_type
LOCAL cProductCreated_at
LOCAL cProductHandle
LOCAL cProductUpdated_at
LOCAL cProductPublished_at
LOCAL nProductTemplate_suffix
LOCAL cProductPublished_scope
LOCAL cProductTags
LOCAL nProductImageId
LOCAL nProductImageProduct_id
LOCAL nProductImagePosition
LOCAL cProductImageCreated_at
LOCAL cProductImageUpdated_at
LOCAL nProductImageWidth
LOCAL nProductImageHeight
LOCAL cProductImageSrc
LOCAL i
LOCAL nCount_i
LOCAL nId
LOCAL nProduct_id
LOCAL cTitle
LOCAL cPrice
LOCAL cSku
LOCAL nPosition
LOCAL nGrams
LOCAL cInventory_policy
LOCAL nCompare_at_price
LOCAL cFulfillment_service
LOCAL nInventory_management
LOCAL cOption1
LOCAL nOption2
LOCAL nOption3
LOCAL cCreated_at
LOCAL cUpdated_at
LOCAL nTaxable
LOCAL nBarcode
LOCAL nImage_id
LOCAL nInventory_quantity
LOCAL nWeight
LOCAL cWeight_unit
LOCAL nOld_inventory_quantity
LOCAL nRequires_shipping
LOCAL cName
LOCAL j
LOCAL nCount_j
LOCAL cStrVal
LOCAL nWidth
LOCAL nHeight
LOCAL cSrc

nSuccess := 0

oRest := CreateObject("Chilkat.Rest")

oRest:SetAuthBasic("SHOPIFY_PRIVATE_API_KEY", "SHOPIFY_PRIVATE_API_SECRET_KEY")

nSuccess := oRest:Connect("chilkat.myshopify.com", 443, 1, 1)
IF (nSuccess != 1)
    ? oRest:LastErrorText
    oRest:destroy()
    RETURN
ENDIF

//  The following code creates the JSON request body.
//  The JSON created by this code is shown below.
oJsonReq := CreateObject("Chilkat.JsonObject")
oJsonReq:UpdateString("product.title", "Burton Custom Freestyle 151")
oJsonReq:UpdateString("product.body_html", "<strong>Good snowboard!</strong>")
oJsonReq:UpdateString("product.vendor", "Burton")
oJsonReq:UpdateString("product.product_type", "Snowboard")
oJsonReq: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=="
//        }
//      ]
//    }
//  }

oSbReq := CreateObject("Chilkat.StringBuilder")
oJsonReq:EmitSb(oSbReq)

oRest:AddHeader("Content-Type", "application/json")

oSbJson := CreateObject("Chilkat.StringBuilder")
nSuccess := oRest:FullRequestSb("POST", "/admin/products.json", oSbReq, oSbJson)
IF (nSuccess != 1)
    ? oRest:LastErrorText
    oRest:destroy()
    oJsonReq:destroy()
    oSbReq:destroy()
    oSbJson:destroy()
    RETURN
ENDIF

IF (oRest:ResponseStatusCode != 201)
    ? "Received error response code: " + Str(oRest:ResponseStatusCode)
    ? "Response body:"
    ? oSbJson:GetAsString()
    oRest:destroy()
    oJsonReq:destroy()
    oSbReq:destroy()
    oSbJson:destroy()
    RETURN
ENDIF

oJson := CreateObject("Chilkat.JsonObject")
oJson:LoadSb(oSbJson)

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

nProductId := oJson:IntOf("product.id")
cProductTitle := oJson:StringOf("product.title")
cProductBody_html := oJson:StringOf("product.body_html")
cProductVendor := oJson:StringOf("product.vendor")
cProductProduct_type := oJson:StringOf("product.product_type")
cProductCreated_at := oJson:StringOf("product.created_at")
cProductHandle := oJson:StringOf("product.handle")
cProductUpdated_at := oJson:StringOf("product.updated_at")
cProductPublished_at := oJson:StringOf("product.published_at")
nProductTemplate_suffix := oJson:IsNullOf("product.template_suffix")
cProductPublished_scope := oJson:StringOf("product.published_scope")
cProductTags := oJson:StringOf("product.tags")
nProductImageId := oJson:IntOf("product.image.id")
nProductImageProduct_id := oJson:IntOf("product.image.product_id")
nProductImagePosition := oJson:IntOf("product.image.position")
cProductImageCreated_at := oJson:StringOf("product.image.created_at")
cProductImageUpdated_at := oJson:StringOf("product.image.updated_at")
nProductImageWidth := oJson:IntOf("product.image.width")
nProductImageHeight := oJson:IntOf("product.image.height")
cProductImageSrc := oJson:StringOf("product.image.src")
i := 0
nCount_i := oJson:SizeOfArray("product.variants")
DO WHILE i < nCount_i
    oJson:I := i
    nId := oJson:IntOf("product.variants[i].id")
    nProduct_id := oJson:IntOf("product.variants[i].product_id")
    cTitle := oJson:StringOf("product.variants[i].title")
    cPrice := oJson:StringOf("product.variants[i].price")
    cSku := oJson:StringOf("product.variants[i].sku")
    nPosition := oJson:IntOf("product.variants[i].position")
    nGrams := oJson:IntOf("product.variants[i].grams")
    cInventory_policy := oJson:StringOf("product.variants[i].inventory_policy")
    nCompare_at_price := oJson:IsNullOf("product.variants[i].compare_at_price")
    cFulfillment_service := oJson:StringOf("product.variants[i].fulfillment_service")
    nInventory_management := oJson:IsNullOf("product.variants[i].inventory_management")
    cOption1 := oJson:StringOf("product.variants[i].option1")
    nOption2 := oJson:IsNullOf("product.variants[i].option2")
    nOption3 := oJson:IsNullOf("product.variants[i].option3")
    cCreated_at := oJson:StringOf("product.variants[i].created_at")
    cUpdated_at := oJson:StringOf("product.variants[i].updated_at")
    nTaxable := oJson:BoolOf("product.variants[i].taxable")
    nBarcode := oJson:IsNullOf("product.variants[i].barcode")
    nImage_id := oJson:IsNullOf("product.variants[i].image_id")
    nInventory_quantity := oJson:IntOf("product.variants[i].inventory_quantity")
    nWeight := oJson:IntOf("product.variants[i].weight")
    cWeight_unit := oJson:StringOf("product.variants[i].weight_unit")
    nOld_inventory_quantity := oJson:IntOf("product.variants[i].old_inventory_quantity")
    nRequires_shipping := oJson:BoolOf("product.variants[i].requires_shipping")
    i := i + 1
ENDDO
i := 0
nCount_i := oJson:SizeOfArray("product.options")
DO WHILE i < nCount_i
    oJson:I := i
    nId := oJson:IntOf("product.options[i].id")
    nProduct_id := oJson:IntOf("product.options[i].product_id")
    cName := oJson:StringOf("product.options[i].name")
    nPosition := oJson:IntOf("product.options[i].position")
    j := 0
    nCount_j := oJson:SizeOfArray("product.options[i].values")
    DO WHILE j < nCount_j
        oJson:J := j
        cStrVal := oJson:StringOf("product.options[i].values[j]")
        j := j + 1
    ENDDO
    i := i + 1
ENDDO
i := 0
nCount_i := oJson:SizeOfArray("product.images")
DO WHILE i < nCount_i
    oJson:I := i
    nId := oJson:IntOf("product.images[i].id")
    nProduct_id := oJson:IntOf("product.images[i].product_id")
    nPosition := oJson:IntOf("product.images[i].position")
    cCreated_at := oJson:StringOf("product.images[i].created_at")
    cUpdated_at := oJson:StringOf("product.images[i].updated_at")
    nWidth := oJson:IntOf("product.images[i].width")
    nHeight := oJson:IntOf("product.images[i].height")
    cSrc := oJson:StringOf("product.images[i].src")
    j := 0
    nCount_j := oJson:SizeOfArray("product.images[i].variant_ids")
    DO WHILE j < nCount_j
        oJson:J := j
        j := j + 1
    ENDDO
    i := i + 1
ENDDO
i := 0
nCount_i := oJson:SizeOfArray("image.product.variant_ids")
DO WHILE i < nCount_i
    oJson:I := i
    i := i + 1
ENDDO

//  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": [
//        ]
//      }
//    }
//  }

? "Example Completed."

oRest:destroy()
oJsonReq:destroy()
oSbReq:destroy()
oSbJson:destroy()
oJson:destroy()