Sample code for 30+ languages & platforms
Lua

Shopify Create a new unpublished product

See more Shopify Examples

Create a new unpublished product

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

local rest = chilkat.Rest.new()

rest:set_auth_basic("SHOPIFY_PRIVATE_API_KEY", "SHOPIFY_PRIVATE_API_KEY")

local success = rest:connect("chilkat.myshopify.com", 443, true, true)
if not success then
    print(rest.last_error_text)
    return
end

-- The following code creates the JSON request body.
-- The JSON created by this code is shown below.
local jsonReq = chilkat.JsonObject.new()
jsonReq:update_string("product.title", "Burton Custom Freestyle 151")
jsonReq:update_string("product.body_html", "<strong>Good snowboard!</strong>")
jsonReq:update_string("product.vendor", "Burton")
jsonReq:update_string("product.product_type", "Snowboard")
jsonReq:update_bool("product.published", false)

-- 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",
--     "published": false
--   }
-- }

local sbReq = chilkat.StringBuilder.new()
jsonReq:emit_sb(sbReq)

rest:add_header("Content-Type", "application/json")

local sbJson = chilkat.StringBuilder.new()
success = rest:full_request_sb("POST", "/admin/products.json ", sbReq, sbJson)
if not success then
    print(rest.last_error_text)
    return
end

if rest.response_status_code ~= 201 then
    print("Received error response code: " .. rest.response_status_code)
    print("Response body:")
    print(sbJson:get_as_string())
    return
end

local json = chilkat.JsonObject.new()
json:load_sb(sbJson)

-- The following code parses the JSON response.
-- A sample JSON response is shown below the sample code.
local id = 0
local product_id = 0
local title = ""
local price = ""
local sku = ""
local position = 0
local grams = 0
local inventory_policy = ""
local compare_at_price = false
local fulfillment_service = ""
local inventory_management = false
local option1 = ""
local option2 = false
local option3 = false
local created_at = ""
local updated_at = ""
local taxable = false
local barcode = false
local image_id = false
local inventory_quantity = 0
local weight = 0
local weight_unit = ""
local old_inventory_quantity = 0
local requires_shipping = false
local name = ""
local j = 0
local count_j = 0
local strVal = ""

local productId = json:int_of("product.id")
local productTitle = json:string_of("product.title")
local productBody_html = json:string_of("product.body_html")
local productVendor = json:string_of("product.vendor")
local productProduct_type = json:string_of("product.product_type")
local productCreated_at = json:string_of("product.created_at")
local productHandle = json:string_of("product.handle")
local productUpdated_at = json:string_of("product.updated_at")
local productPublished_at = json:is_null_of("product.published_at")
local productTemplate_suffix = json:is_null_of("product.template_suffix")
local productPublished_scope = json:string_of("product.published_scope")
local productTags = json:string_of("product.tags")
local productImage = json:is_null_of("product.image")
local i = 0
local count_i = json:size_of_array("product.variants")
while i < count_i do
    json.i = i
    id = json:int_of("product.variants[i].id")
    product_id = json:int_of("product.variants[i].product_id")
    title = json:string_of("product.variants[i].title")
    price = json:string_of("product.variants[i].price")
    sku = json:string_of("product.variants[i].sku")
    position = json:int_of("product.variants[i].position")
    grams = json:int_of("product.variants[i].grams")
    inventory_policy = json:string_of("product.variants[i].inventory_policy")
    compare_at_price = json:is_null_of("product.variants[i].compare_at_price")
    fulfillment_service = json:string_of("product.variants[i].fulfillment_service")
    inventory_management = json:is_null_of("product.variants[i].inventory_management")
    option1 = json:string_of("product.variants[i].option1")
    option2 = json:is_null_of("product.variants[i].option2")
    option3 = json:is_null_of("product.variants[i].option3")
    created_at = json:string_of("product.variants[i].created_at")
    updated_at = json:string_of("product.variants[i].updated_at")
    taxable = json:bool_of("product.variants[i].taxable")
    barcode = json:is_null_of("product.variants[i].barcode")
    image_id = json:is_null_of("product.variants[i].image_id")
    inventory_quantity = json:int_of("product.variants[i].inventory_quantity")
    weight = json:int_of("product.variants[i].weight")
    weight_unit = json:string_of("product.variants[i].weight_unit")
    old_inventory_quantity = json:int_of("product.variants[i].old_inventory_quantity")
    requires_shipping = json:bool_of("product.variants[i].requires_shipping")
    i = i + 1
end

i = 0
count_i = json:size_of_array("product.options")
while i < count_i do
    json.i = i
    id = json:int_of("product.options[i].id")
    product_id = json:int_of("product.options[i].product_id")
    name = json:string_of("product.options[i].name")
    position = json:int_of("product.options[i].position")
    j = 0
    count_j = json:size_of_array("product.options[i].values")
    while j < count_j do
        json.j = j
        strVal = json:string_of("product.options[i].values[j]")
        j = j + 1
    end

    i = i + 1
end

i = 0
count_i = json:size_of_array("product.images")
while i < count_i do
    json.i = i
    i = i + 1
end

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

-- {
--   "product": {
--     "id": 1071559753,
--     "title": "Burton Custom Freestyle 151",
--     "body_html": "<strong>Good snowboard!<\/strong>",
--     "vendor": "Burton",
--     "product_type": "Snowboard",
--     "created_at": "2017-09-22T14:48:52-04:00",
--     "handle": "burton-custom-freestyle-151",
--     "updated_at": "2017-09-22T14:48:52-04:00",
--     "published_at": null,
--     "template_suffix": null,
--     "published_scope": "global",
--     "tags": "",
--     "variants": [
--       {
--         "id": 1070325224,
--         "product_id": 1071559753,
--         "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:52-04:00",
--         "updated_at": "2017-09-22T14:48:52-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": 1022828912,
--         "product_id": 1071559753,
--         "name": "Title",
--         "position": 1,
--         "values": [
--           "Default Title"
--         ]
--       }
--     ],
--     "images": [
--     ],
--     "image": null
--   }
-- }

print("Example Completed.")