Lua
Lua
Lightspeed - Create a Product
See more Lightspeed Examples
Create a new product based on the given parameters.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local http = chilkat.Http.new()
-- Implements the following CURL command:
-- curl -u API_KEY:API_SECRET \
-- -H "Content-Type: application/json" \
-- -X POST \
-- -d '{
-- "product": {
-- "visibility": "visible",
-- "data01": "",
-- "data02": "",
-- "data03": "",
-- "title": "Lookin Sharp T-Shirt",
-- "fulltitle": "Lookin Sharp T-Shirt",
-- "description": "Description of the Lookin Sharp T-Shirt",
-- "content": "Long Description of the Lookin Sharp T-Shirt",
-- "deliverydate": 6488,
-- "supplier": 78794,
-- "brand": 1171202
-- }
-- }' \
-- "https://api.webshopapp.com/en/products.json"
-- Use the following online tool to generate HTTP code from a CURL command
-- Convert a cURL Command to HTTP Source Code
http.login = "API_KEY"
http.password = "API_SECRET"
-- Use this online tool to generate code from sample JSON:
-- Generate Code to Create JSON
-- The following JSON is sent in the request body.
-- {
-- "product": {
-- "visibility": "visible",
-- "data01": "",
-- "data02": "",
-- "data03": "",
-- "title": "Lookin Sharp T-Shirt",
-- "fulltitle": "Lookin Sharp T-Shirt",
-- "description": "Description of the Lookin Sharp T-Shirt",
-- "content": "Long Description of the Lookin Sharp T-Shirt",
-- "deliverydate": 6488,
-- "supplier": 78794,
-- "brand": 1171202
-- }
-- }
local json = chilkat.JsonObject.new()
json:update_string("product.visibility", "visible")
json:update_string("product.data01", "")
json:update_string("product.data02", "")
json:update_string("product.data03", "")
json:update_string("product.title", "Lookin Sharp T-Shirt")
json:update_string("product.fulltitle", "Lookin Sharp T-Shirt")
json:update_string("product.description", "Description of the Lookin Sharp T-Shirt")
json:update_string("product.content", "Long Description of the Lookin Sharp T-Shirt")
json:update_int("product.deliverydate", 6488)
json:update_int("product.supplier", 78794)
json:update_int("product.brand", 1171202)
http:set_request_header("Content-Type", "application/json")
-- Use the correct cluster for your shop. Here are the choices:
-- eu1 https://api.webshopapp.com/en/
-- us1 https://api.shoplightspeed.com/en/
local resp = chilkat.HttpResponse.new()
local success = http:http_json("POST", "https://api.webshopapp.com/en/products.json", json, "application/json", resp)
if not success then
print(http.last_error_text)
return
end
local sbResponseBody = chilkat.StringBuilder.new()
resp:get_body_sb(sbResponseBody)
local jResp = chilkat.JsonObject.new()
jResp:load_sb(sbResponseBody)
jResp.emit_compact = false
print("Response Body:")
print(jResp:emit())
local respStatusCode = resp.status_code
print("Response Status Code = " .. respStatusCode)
if respStatusCode >= 400 then
print("Response Header:")
print(resp.header)
print("Failed.")
return
end
-- Sample JSON response:
-- (Sample code for parsing the JSON response is shown below)
-- {
-- "product": {
-- "id": 20974460,
-- "createdAt": "2019-05-28T20:14:26+00:00",
-- "updatedAt": "2019-05-28T20:14:26+00:00",
-- "isVisible": true,
-- "visibility": "visible",
-- "hasMatrix": false,
-- "data01": "",
-- "data02": "",
-- "data03": "",
-- "url": "lookin-sharp-t-shirt",
-- "title": "Lookin' Sharp T-Shirt",
-- "fulltitle": "Lookin' Sharp T-Shirt",
-- "description": "Description of the Lookin' Sharp T-Shirt",
-- "content": "<p>Long Description of the Lookin' Sharp T-Shirt</p>",
-- "set": false,
-- "brand": {
-- "resource": {
-- "id": 1171202,
-- "url": "brands/1171202",
-- "link": "https://api.shoplightspeed.com/us/brands/1171202.json"
-- }
-- },
-- "categories": {
-- "resource": {
-- "id": false,
-- "url": "categories/products?product=20974460",
-- "link": "https://api.shoplightspeed.com/us/categories/products.json?product=20974460"
-- }
-- },
-- "deliverydate": {
-- "resource": {
-- "id": 6488,
-- "url": "deliverydates/6488",
-- "link": "https://api.shoplightspeed.com/us/deliverydates/6488.json"
-- }
-- },
-- "image": false,
-- "images": false,
-- "relations": {
-- "resource": {
-- "id": false,
-- "url": "products/20974460/relations",
-- "link": "https://api.shoplightspeed.com/us/products/20974460/relations.json"
-- }
-- },
-- "metafields": {
-- "resource": {
-- "id": false,
-- "url": "products/20974460/metafields",
-- "link": "https://api.shoplightspeed.com/us/products/20974460/metafields.json"
-- }
-- },
-- "reviews": {
-- "resource": {
-- "id": false,
-- "url": "reviews?product=20974460",
-- "link": "https://api.shoplightspeed.com/us/reviews.json?product=20974460"
-- }
-- },
-- "type": false,
-- "attributes": {
-- "resource": {
-- "id": false,
-- "url": "products/20974460/attributes",
-- "link": "https://api.shoplightspeed.com/us/products/20974460/attributes.json"
-- }
-- },
-- "supplier": {
-- "resource": {
-- "id": 78794,
-- "url": "suppliers/78794",
-- "link": "https://api.shoplightspeed.com/us/suppliers/78794.json"
-- }
-- },
-- "tags": {
-- "resource": {
-- "id": false,
-- "url": "tags/products?product=20974460",
-- "link": "https://api.shoplightspeed.com/us/tags/products.json?product=20974460"
-- }
-- },
-- "variants": {
-- "resource": {
-- "id": false,
-- "url": "variants?product=20974460",
-- "link": "https://api.shoplightspeed.com/us/variants.json?product=20974460"
-- }
-- },
-- "movements": {
-- "resource": {
-- "id": false,
-- "url": "variants/movements?product=20974460",
-- "link": "https://api.shoplightspeed.com/us/variants/movements.json?product=20974460"
-- }
-- }
-- }
-- }
-- Sample code for parsing the JSON response...
-- Use the following online tool to generate parsing code from sample JSON:
-- Generate Parsing Code from JSON
local productId = jResp:int_of("product.id")
local productCreatedAt = jResp:string_of("product.createdAt")
local productUpdatedAt = jResp:string_of("product.updatedAt")
local productIsVisible = jResp:bool_of("product.isVisible")
local productVisibility = jResp:string_of("product.visibility")
local productHasMatrix = jResp:bool_of("product.hasMatrix")
local productData01 = jResp:string_of("product.data01")
local productData02 = jResp:string_of("product.data02")
local productData03 = jResp:string_of("product.data03")
local productUrl = jResp:string_of("product.url")
local productTitle = jResp:string_of("product.title")
local productFulltitle = jResp:string_of("product.fulltitle")
local productDescription = jResp:string_of("product.description")
local productContent = jResp:string_of("product.content")
local productSet = jResp:bool_of("product.set")
local productBrandResourceId = jResp:int_of("product.brand.resource.id")
local productBrandResourceUrl = jResp:string_of("product.brand.resource.url")
local productBrandResourceLink = jResp:string_of("product.brand.resource.link")
local productCategoriesResourceId = jResp:bool_of("product.categories.resource.id")
local productCategoriesResourceUrl = jResp:string_of("product.categories.resource.url")
local productCategoriesResourceLink = jResp:string_of("product.categories.resource.link")
local productDeliverydateResourceId = jResp:int_of("product.deliverydate.resource.id")
local productDeliverydateResourceUrl = jResp:string_of("product.deliverydate.resource.url")
local productDeliverydateResourceLink = jResp:string_of("product.deliverydate.resource.link")
local productImage = jResp:bool_of("product.image")
local productImages = jResp:bool_of("product.images")
local productRelationsResourceId = jResp:bool_of("product.relations.resource.id")
local productRelationsResourceUrl = jResp:string_of("product.relations.resource.url")
local productRelationsResourceLink = jResp:string_of("product.relations.resource.link")
local productMetafieldsResourceId = jResp:bool_of("product.metafields.resource.id")
local productMetafieldsResourceUrl = jResp:string_of("product.metafields.resource.url")
local productMetafieldsResourceLink = jResp:string_of("product.metafields.resource.link")
local productReviewsResourceId = jResp:bool_of("product.reviews.resource.id")
local productReviewsResourceUrl = jResp:string_of("product.reviews.resource.url")
local productReviewsResourceLink = jResp:string_of("product.reviews.resource.link")
local productType = jResp:bool_of("product.type")
local productAttributesResourceId = jResp:bool_of("product.attributes.resource.id")
local productAttributesResourceUrl = jResp:string_of("product.attributes.resource.url")
local productAttributesResourceLink = jResp:string_of("product.attributes.resource.link")
local productSupplierResourceId = jResp:int_of("product.supplier.resource.id")
local productSupplierResourceUrl = jResp:string_of("product.supplier.resource.url")
local productSupplierResourceLink = jResp:string_of("product.supplier.resource.link")
local productTagsResourceId = jResp:bool_of("product.tags.resource.id")
local productTagsResourceUrl = jResp:string_of("product.tags.resource.url")
local productTagsResourceLink = jResp:string_of("product.tags.resource.link")
local productVariantsResourceId = jResp:bool_of("product.variants.resource.id")
local productVariantsResourceUrl = jResp:string_of("product.variants.resource.url")
local productVariantsResourceLink = jResp:string_of("product.variants.resource.link")
local productMovementsResourceId = jResp:bool_of("product.movements.resource.id")
local productMovementsResourceUrl = jResp:string_of("product.movements.resource.url")
local productMovementsResourceLink = jResp:string_of("product.movements.resource.link")