Sample code for 30+ languages & platforms
Unicode C++

Lightspeed - Create a Product

See more Lightspeed Examples

Create a new product based on the given parameters.

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkHttpW.h>
#include <CkJsonObjectW.h>
#include <CkHttpResponseW.h>
#include <CkStringBuilderW.h>

void ChilkatSample(void)
    {
    bool success = false;

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

    CkHttpW http;

    //  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.put_Login(L"API_KEY");
    http.put_Password(L"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
    //    }
    //  }

    CkJsonObjectW json;
    json.UpdateString(L"product.visibility",L"visible");
    json.UpdateString(L"product.data01",L"");
    json.UpdateString(L"product.data02",L"");
    json.UpdateString(L"product.data03",L"");
    json.UpdateString(L"product.title",L"Lookin Sharp T-Shirt");
    json.UpdateString(L"product.fulltitle",L"Lookin Sharp T-Shirt");
    json.UpdateString(L"product.description",L"Description of the Lookin Sharp T-Shirt");
    json.UpdateString(L"product.content",L"Long Description of the Lookin Sharp T-Shirt");
    json.UpdateInt(L"product.deliverydate",6488);
    json.UpdateInt(L"product.supplier",78794);
    json.UpdateInt(L"product.brand",1171202);

    http.SetRequestHeader(L"Content-Type",L"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/
    CkHttpResponseW resp;
    success = http.HttpJson(L"POST",L"https://api.webshopapp.com/en/products.json",json,L"application/json",resp);
    if (success == false) {
        wprintf(L"%s\n",http.lastErrorText());
        return;
    }

    CkStringBuilderW sbResponseBody;
    resp.GetBodySb(sbResponseBody);
    CkJsonObjectW jResp;
    jResp.LoadSb(sbResponseBody);
    jResp.put_EmitCompact(false);

    wprintf(L"Response Body:\n");
    wprintf(L"%s\n",jResp.emit());

    int respStatusCode = resp.get_StatusCode();
    wprintf(L"Response Status Code = %d\n",respStatusCode);
    if (respStatusCode >= 400) {
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",resp.header());
        wprintf(L"Failed.\n");
        return;
    }

    //  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

    //  Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
    //  See this example explaining how this memory should be used: const char * functions.

    int productId = jResp.IntOf(L"product.id");
    const wchar_t *productCreatedAt = jResp.stringOf(L"product.createdAt");
    const wchar_t *productUpdatedAt = jResp.stringOf(L"product.updatedAt");
    bool productIsVisible = jResp.BoolOf(L"product.isVisible");
    const wchar_t *productVisibility = jResp.stringOf(L"product.visibility");
    bool productHasMatrix = jResp.BoolOf(L"product.hasMatrix");
    const wchar_t *productData01 = jResp.stringOf(L"product.data01");
    const wchar_t *productData02 = jResp.stringOf(L"product.data02");
    const wchar_t *productData03 = jResp.stringOf(L"product.data03");
    const wchar_t *productUrl = jResp.stringOf(L"product.url");
    const wchar_t *productTitle = jResp.stringOf(L"product.title");
    const wchar_t *productFulltitle = jResp.stringOf(L"product.fulltitle");
    const wchar_t *productDescription = jResp.stringOf(L"product.description");
    const wchar_t *productContent = jResp.stringOf(L"product.content");
    bool productSet = jResp.BoolOf(L"product.set");
    int productBrandResourceId = jResp.IntOf(L"product.brand.resource.id");
    const wchar_t *productBrandResourceUrl = jResp.stringOf(L"product.brand.resource.url");
    const wchar_t *productBrandResourceLink = jResp.stringOf(L"product.brand.resource.link");
    bool productCategoriesResourceId = jResp.BoolOf(L"product.categories.resource.id");
    const wchar_t *productCategoriesResourceUrl = jResp.stringOf(L"product.categories.resource.url");
    const wchar_t *productCategoriesResourceLink = jResp.stringOf(L"product.categories.resource.link");
    int productDeliverydateResourceId = jResp.IntOf(L"product.deliverydate.resource.id");
    const wchar_t *productDeliverydateResourceUrl = jResp.stringOf(L"product.deliverydate.resource.url");
    const wchar_t *productDeliverydateResourceLink = jResp.stringOf(L"product.deliverydate.resource.link");
    bool productImage = jResp.BoolOf(L"product.image");
    bool productImages = jResp.BoolOf(L"product.images");
    bool productRelationsResourceId = jResp.BoolOf(L"product.relations.resource.id");
    const wchar_t *productRelationsResourceUrl = jResp.stringOf(L"product.relations.resource.url");
    const wchar_t *productRelationsResourceLink = jResp.stringOf(L"product.relations.resource.link");
    bool productMetafieldsResourceId = jResp.BoolOf(L"product.metafields.resource.id");
    const wchar_t *productMetafieldsResourceUrl = jResp.stringOf(L"product.metafields.resource.url");
    const wchar_t *productMetafieldsResourceLink = jResp.stringOf(L"product.metafields.resource.link");
    bool productReviewsResourceId = jResp.BoolOf(L"product.reviews.resource.id");
    const wchar_t *productReviewsResourceUrl = jResp.stringOf(L"product.reviews.resource.url");
    const wchar_t *productReviewsResourceLink = jResp.stringOf(L"product.reviews.resource.link");
    bool productType = jResp.BoolOf(L"product.type");
    bool productAttributesResourceId = jResp.BoolOf(L"product.attributes.resource.id");
    const wchar_t *productAttributesResourceUrl = jResp.stringOf(L"product.attributes.resource.url");
    const wchar_t *productAttributesResourceLink = jResp.stringOf(L"product.attributes.resource.link");
    int productSupplierResourceId = jResp.IntOf(L"product.supplier.resource.id");
    const wchar_t *productSupplierResourceUrl = jResp.stringOf(L"product.supplier.resource.url");
    const wchar_t *productSupplierResourceLink = jResp.stringOf(L"product.supplier.resource.link");
    bool productTagsResourceId = jResp.BoolOf(L"product.tags.resource.id");
    const wchar_t *productTagsResourceUrl = jResp.stringOf(L"product.tags.resource.url");
    const wchar_t *productTagsResourceLink = jResp.stringOf(L"product.tags.resource.link");
    bool productVariantsResourceId = jResp.BoolOf(L"product.variants.resource.id");
    const wchar_t *productVariantsResourceUrl = jResp.stringOf(L"product.variants.resource.url");
    const wchar_t *productVariantsResourceLink = jResp.stringOf(L"product.variants.resource.link");
    bool productMovementsResourceId = jResp.BoolOf(L"product.movements.resource.id");
    const wchar_t *productMovementsResourceUrl = jResp.stringOf(L"product.movements.resource.url");
    const wchar_t *productMovementsResourceLink = jResp.stringOf(L"product.movements.resource.link");
    }