Sample code for 30+ languages & platforms
Dart

WiX Create Product

See more WiX Examples

Creates a new product.

Note: If you get a 403 error response, try refreshing the access token.

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

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

  final http = CkHttp();

  // Implements the following CURL command:

  // curl -X POST \
  //    'https://www.wixapis.com/stores/v1/products' \
  //     --data-binary '{
  //                      "product": {
  //                        "name": "T-shirt",
  //                        "productType": "physical",
  //                        "priceData": {
  //                          "price": 10.5
  //                        },
  //                        "description": "nice summer t-shirt",
  //                        "sku": "123df",
  //                        "visible": false,
  //                        "weight": 0.2,
  //                        "discount": {
  //                        	"type": "AMOUNT",
  //                        	"value": 1
  //                        },
  //                        "manageVariants": true,
  //                        "productOptions": [
  //                          {
  //                            "name": "Size",
  //                            "choices": [
  //                              {
  //                                "value": "S",
  //                                "description": "S"
  //                              },
  //                              {
  //                                "value": "L",
  //                                "description": "L"
  //                              }
  //                            ]
  //                          }
  //                        ]
  //                      }
  //                    }' \
  //    -H 'Content-Type: application/json' \
  //    -H 'Authorization: <AUTH>'

  // Use the following online tool to generate HTTP code from a CURL command
  // Convert a cURL Command to HTTP Source Code

  // 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": {
  //     "name": "T-shirt",
  //     "productType": "physical",
  //     "priceData": {
  //       "price": 10.5
  //     },
  //     "description": "nice summer t-shirt",
  //     "sku": "123df",
  //     "visible": false,
  //     "weight": 0.2,
  //     "discount": {
  //       "type": "AMOUNT",
  //       "value": 1
  //     },
  //     "manageVariants": true,
  //     "productOptions": [
  //       {
  //         "name": "Size",
  //         "choices": [
  //           {
  //             "value": "S",
  //             "description": "S"
  //           },
  //           {
  //             "value": "L",
  //             "description": "L"
  //           }
  //         ]
  //       }
  //     ]
  //   }
  // }

  final json = CkJsonObject();
  json.updateString('product.name', 'T-shirt');
  json.updateString('product.productType', 'physical');
  json.updateNumber('product.priceData.price', '10.5');
  json.updateString('product.description', 'nice summer t-shirt');
  json.updateString('product.sku', '123df');
  json.updateBool('product.visible', false);
  json.updateNumber('product.weight', '0.2');
  json.updateString('product.discount.type', 'AMOUNT');
  json.updateInt('product.discount.value', 1);
  json.updateBool('product.manageVariants', true);
  json.updateString('product.productOptions[0].name', 'Size');
  json.updateString('product.productOptions[0].choices[0].value', 'S');
  json.updateString('product.productOptions[0].choices[0].description', 'S');
  json.updateString('product.productOptions[0].choices[1].value', 'L');
  json.updateString('product.productOptions[0].choices[1].description', 'L');

  http.authToken = 'ACCESS_TOKEN';

  final resp = CkHttpResponse();
  try {
    http.httpJson('POST', 'https://www.wixapis.com/stores/v1/products', json, 'application/json', resp);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final sbResponseBody = CkStringBuilder();
  resp.getBodySb(sbResponseBody);
  final jResp = CkJsonObject();
  jResp.loadSb(sbResponseBody);
  jResp.emitCompact = false;

  print('Response Body:');
  print(jResp.emit());

  final respStatusCode = resp.statusCode;
  print('Response Status Code = $respStatusCode');
  if (respStatusCode >= 400) {
    print('Response Header:');
    print(resp.header);
    print('Failed.');
    return;
  }

  // Sample JSON response:
  // (Sample code for parsing the JSON response is shown below)

  // {
  //   "product": {
  //     "id": "e28e4ddd-6ed0-4098-a5e5-cf4bd9c4f7b5",
  //     "name": "T-shirt",
  //     "slug": "t-shirt-1",
  //     "visible": false,
  //     "productType": "physical",
  //     "description": "nice summer t-shirt",
  //     "stock": {
  //       "trackInventory": false,
  //       "inStock": true
  //     },
  //     "price": {
  //       "currency": "ILS",
  //       "price": 10.5,
  //       "discountedPrice": 9.5,
  //       "formatted": {
  //         "price": "10.50",
  //         "discountedPrice": "9.50"
  //       }
  //     },
  //     "priceData": {
  //       "currency": "ILS",
  //       "price": 10.5,
  //       "discountedPrice": 9.5,
  //       "formatted": {
  //         "price": "10.50",
  //         "discountedPrice": "9.50"
  //       }
  //     },
  //     "additionalInfoSections": [
  //     ],
  //     "ribbons": [
  //     ],
  //     "media": {
  //       "items": [
  //       ]
  //     },
  //     "customTextFields": [
  //     ],
  //     "manageVariants": true,
  //     "productOptions": [
  //       {
  //         "optionType": "drop_down",
  //         "name": "Size",
  //         "choices": [
  //           {
  //             "value": "S",
  //             "description": "S",
  //             "inStock": true,
  //             "visible": true
  //           },
  //           {
  //             "value": "L",
  //             "description": "L",
  //             "inStock": true,
  //             "visible": true
  //           }
  //         ]
  //       }
  //     ],
  //     "productPageUrl": {
  //       "base": "https://www.itsjusttooeasy123.com/",
  //       "path": "/product-page/t-shirt-1"
  //     },
  //     "numericId": "1567588455405000",
  //     "inventoryItemId": "1d71b222-912f-bf67-5a1a-30b4263b084a",
  //     "discount": {
  //       "type": "AMOUNT",
  //       "value": 1
  //     },
  //     "collectionIds": [
  //     ],
  //     "variants": [
  //       {
  //         "id": "00000000-0000-0001-0005-93fc95e0514a",
  //         "choices": {
  //           "Size": "S"
  //         },
  //         "variant": {
  //           "priceData": {
  //             "currency": "ILS",
  //             "price": 10.5,
  //             "discountedPrice": 9.5,
  //             "formatted": {
  //               "price": "10.50",
  //               "discountedPrice": "9.50"
  //             }
  //           },
  //           "weight": 10,
  //           "visible": true
  //         }
  //       },
  //       {
  //         "id": "00000000-0000-0002-0005-93fc95e0514a",
  //         "choices": {
  //           "Size": "L"
  //         },
  //         "variant": {
  //           "priceData": {
  //             "currency": "ILS",
  //             "price": 10.5,
  //             "discountedPrice": 9.5,
  //             "formatted": {
  //               "price": "10.50",
  //               "discountedPrice": "9.50"
  //             }
  //           },
  //           "visible": true
  //         }
  //       }
  //     ]
  //   }
  // }

  // Sample code for parsing the JSON response...
  // Use the following online tool to generate parsing code from sample JSON:
  // Generate Parsing Code from JSON

  var optionType = '';
  var name = '';
  var j = 0;
  var countJ = 0;
  var value = '';
  var description = '';
  var id = '';
  var choicesSize = '';
  var variantPriceDataCurrency = '';
  var variantPriceDataPrice = '';
  var variantPriceDataDiscountedPrice = '';
  var variantPriceDataFormattedPrice = '';
  var variantPriceDataFormattedDiscountedPrice = '';
  var variantWeight = 0;

  final productId = jResp.stringOf('product.id');
  final productName = jResp.stringOf('product.name');
  final productSlug = jResp.stringOf('product.slug');
  final productProductType = jResp.stringOf('product.productType');
  final productDescription = jResp.stringOf('product.description');
  final productPriceCurrency = jResp.stringOf('product.price.currency');
  final productPricePrice = jResp.stringOf('product.price.price');
  final productPriceDiscountedPrice = jResp.stringOf('product.price.discountedPrice');
  final productPriceFormattedPrice = jResp.stringOf('product.price.formatted.price');
  final productPriceFormattedDiscountedPrice = jResp.stringOf('product.price.formatted.discountedPrice');
  final productPriceDataCurrency = jResp.stringOf('product.priceData.currency');
  final productPriceDataPrice = jResp.stringOf('product.priceData.price');
  final productPriceDataDiscountedPrice = jResp.stringOf('product.priceData.discountedPrice');
  final productPriceDataFormattedPrice = jResp.stringOf('product.priceData.formatted.price');
  final productPriceDataFormattedDiscountedPrice = jResp.stringOf('product.priceData.formatted.discountedPrice');
  final productProductPageUrlBase = jResp.stringOf('product.productPageUrl.base');
  final productProductPageUrlPath = jResp.stringOf('product.productPageUrl.path');
  final productNumericId = jResp.stringOf('product.numericId');
  final productInventoryItemId = jResp.stringOf('product.inventoryItemId');
  final productDiscountType = jResp.stringOf('product.discount.type');
  final productDiscountValue = jResp.intOf('product.discount.value');
  var i = 0;
  var countI = jResp.sizeOfArray('product.additionalInfoSections');
  while (i < countI) {
    jResp.i = i;
    i++;
  }

  i = 0;
  countI = jResp.sizeOfArray('product.ribbons');
  while (i < countI) {
    jResp.i = i;
    i++;
  }

  i = 0;
  countI = jResp.sizeOfArray('product.media.items');
  while (i < countI) {
    jResp.i = i;
    i++;
  }

  i = 0;
  countI = jResp.sizeOfArray('product.customTextFields');
  while (i < countI) {
    jResp.i = i;
    i++;
  }

  i = 0;
  countI = jResp.sizeOfArray('product.productOptions');
  while (i < countI) {
    jResp.i = i;
    optionType = jResp.stringOf('product.productOptions[i].optionType');
    name = jResp.stringOf('product.productOptions[i].name');
    j = 0;
    countJ = jResp.sizeOfArray('product.productOptions[i].choices');
    while (j < countJ) {
      jResp.j = j;
      value = jResp.stringOf('product.productOptions[i].choices[j].value');
      description = jResp.stringOf('product.productOptions[i].choices[j].description');
      jResp.boolOf('product.productOptions[i].choices[j].inStock');
      jResp.boolOf('product.productOptions[i].choices[j].visible');
      j++;
    }

    i++;
  }

  i = 0;
  countI = jResp.sizeOfArray('product.collectionIds');
  while (i < countI) {
    jResp.i = i;
    i++;
  }

  i = 0;
  countI = jResp.sizeOfArray('product.variants');
  while (i < countI) {
    jResp.i = i;
    id = jResp.stringOf('product.variants[i].id');
    choicesSize = jResp.stringOf('product.variants[i].choices.Size');
    variantPriceDataCurrency = jResp.stringOf('product.variants[i].variant.priceData.currency');
    variantPriceDataPrice = jResp.stringOf('product.variants[i].variant.priceData.price');
    variantPriceDataDiscountedPrice = jResp.stringOf('product.variants[i].variant.priceData.discountedPrice');
    variantPriceDataFormattedPrice = jResp.stringOf('product.variants[i].variant.priceData.formatted.price');
    variantPriceDataFormattedDiscountedPrice = jResp.stringOf('product.variants[i].variant.priceData.formatted.discountedPrice');
    variantWeight = jResp.intOf('product.variants[i].variant.weight');
    jResp.boolOf('product.variants[i].variant.visible');
    i++;
  }
}