Sample code for 30+ languages & platforms
Zig

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 Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

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

    const http = try chilkat.Http.init();
    defer http.deinit();

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

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("product.name", "T-shirt") catch {};
    json.updateString("product.productType", "physical") catch {};
    json.updateNumber("product.priceData.price", "10.5") catch {};
    json.updateString("product.description", "nice summer t-shirt") catch {};
    json.updateString("product.sku", "123df") catch {};
    json.updateBool("product.visible", false) catch {};
    json.updateNumber("product.weight", "0.2") catch {};
    json.updateString("product.discount.type", "AMOUNT") catch {};
    json.updateInt("product.discount.value", 1) catch {};
    json.updateBool("product.manageVariants", true) catch {};
    json.updateString("product.productOptions[0].name", "Size") catch {};
    json.updateString("product.productOptions[0].choices[0].value", "S") catch {};
    json.updateString("product.productOptions[0].choices[0].description", "S") catch {};
    json.updateString("product.productOptions[0].choices[1].value", "L") catch {};
    json.updateString("product.productOptions[0].choices[1].description", "L") catch {};

    http.setAuthToken("ACCESS_TOKEN");

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpJson("POST", "https://www.wixapis.com/stores/v1/products", json, "application/json", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    resp.getBodySb(sb_response_body) catch {};
    const j_resp = try chilkat.JsonObject.init();
    defer j_resp.deinit();
    j_resp.loadSb(sb_response_body) catch {};
    j_resp.setEmitCompact(false);

    std.debug.print("Response Body:\n", .{});
    std.debug.print("{s}\n", .{try j_resp.emit(alloc)});

    const resp_status_code = resp.getStatusCode();
    std.debug.print("Response Status Code = {d}\n", .{resp_status_code});
    if (resp_status_code >= 400) {
        std.debug.print("Response Header:\n", .{});
        std.debug.print("{s}\n", .{try resp.getHeader(alloc)});
        std.debug.print("Failed.\n", .{});
        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 option_type: [:0]const u8 = "";
    var name: [:0]const u8 = "";
    var j: i32 = 0;
    var count_j: i32 = 0;
    var value: [:0]const u8 = "";
    var description: [:0]const u8 = "";
    var in_stock: bool = false;
    var visible: bool = false;
    var id: [:0]const u8 = "";
    var choices_size: [:0]const u8 = "";
    var variant_price_data_currency: [:0]const u8 = "";
    var variant_price_data_price: [:0]const u8 = "";
    var variant_price_data_discounted_price: [:0]const u8 = "";
    var variant_price_data_formatted_price: [:0]const u8 = "";
    var variant_price_data_formatted_discounted_price: [:0]const u8 = "";
    var variant_weight: i32 = 0;
    var variant_visible: bool = false;

    _ = try j_resp.stringOf(alloc, "product.id");
    _ = try j_resp.stringOf(alloc, "product.name");
    _ = try j_resp.stringOf(alloc, "product.slug");
    _ = j_resp.boolOf("product.visible");
    _ = try j_resp.stringOf(alloc, "product.productType");
    _ = try j_resp.stringOf(alloc, "product.description");
    _ = j_resp.boolOf("product.stock.trackInventory");
    _ = j_resp.boolOf("product.stock.inStock");
    _ = try j_resp.stringOf(alloc, "product.price.currency");
    _ = try j_resp.stringOf(alloc, "product.price.price");
    _ = try j_resp.stringOf(alloc, "product.price.discountedPrice");
    _ = try j_resp.stringOf(alloc, "product.price.formatted.price");
    _ = try j_resp.stringOf(alloc, "product.price.formatted.discountedPrice");
    _ = try j_resp.stringOf(alloc, "product.priceData.currency");
    _ = try j_resp.stringOf(alloc, "product.priceData.price");
    _ = try j_resp.stringOf(alloc, "product.priceData.discountedPrice");
    _ = try j_resp.stringOf(alloc, "product.priceData.formatted.price");
    _ = try j_resp.stringOf(alloc, "product.priceData.formatted.discountedPrice");
    _ = j_resp.boolOf("product.manageVariants");
    _ = try j_resp.stringOf(alloc, "product.productPageUrl.base");
    _ = try j_resp.stringOf(alloc, "product.productPageUrl.path");
    _ = try j_resp.stringOf(alloc, "product.numericId");
    _ = try j_resp.stringOf(alloc, "product.inventoryItemId");
    _ = try j_resp.stringOf(alloc, "product.discount.type");
    _ = j_resp.intOf("product.discount.value");
    var i: i32 = 0;
    var count_i: i32 = j_resp.sizeOfArray("product.additionalInfoSections");
    while (i < count_i) {
        j_resp.setI(i);
        i = i + 1;
    }

    i = 0;
    count_i = j_resp.sizeOfArray("product.ribbons");
    while (i < count_i) {
        j_resp.setI(i);
        i = i + 1;
    }

    i = 0;
    count_i = j_resp.sizeOfArray("product.media.items");
    while (i < count_i) {
        j_resp.setI(i);
        i = i + 1;
    }

    i = 0;
    count_i = j_resp.sizeOfArray("product.customTextFields");
    while (i < count_i) {
        j_resp.setI(i);
        i = i + 1;
    }

    i = 0;
    count_i = j_resp.sizeOfArray("product.productOptions");
    while (i < count_i) {
        j_resp.setI(i);
        option_type = try j_resp.stringOf(alloc, "product.productOptions[i].optionType");
        name = try j_resp.stringOf(alloc, "product.productOptions[i].name");
        j = 0;
        count_j = j_resp.sizeOfArray("product.productOptions[i].choices");
        while (j < count_j) {
            j_resp.setJ(j);
            value = try j_resp.stringOf(alloc, "product.productOptions[i].choices[j].value");
            description = try j_resp.stringOf(alloc, "product.productOptions[i].choices[j].description");
            in_stock = j_resp.boolOf("product.productOptions[i].choices[j].inStock");
            visible = j_resp.boolOf("product.productOptions[i].choices[j].visible");
            j = j + 1;
        }

        i = i + 1;
    }

    i = 0;
    count_i = j_resp.sizeOfArray("product.collectionIds");
    while (i < count_i) {
        j_resp.setI(i);
        i = i + 1;
    }

    i = 0;
    count_i = j_resp.sizeOfArray("product.variants");
    while (i < count_i) {
        j_resp.setI(i);
        id = try j_resp.stringOf(alloc, "product.variants[i].id");
        choices_size = try j_resp.stringOf(alloc, "product.variants[i].choices.Size");
        variant_price_data_currency = try j_resp.stringOf(alloc, "product.variants[i].variant.priceData.currency");
        variant_price_data_price = try j_resp.stringOf(alloc, "product.variants[i].variant.priceData.price");
        variant_price_data_discounted_price = try j_resp.stringOf(alloc, "product.variants[i].variant.priceData.discountedPrice");
        variant_price_data_formatted_price = try j_resp.stringOf(alloc, "product.variants[i].variant.priceData.formatted.price");
        variant_price_data_formatted_discounted_price = try j_resp.stringOf(alloc, "product.variants[i].variant.priceData.formatted.discountedPrice");
        variant_weight = j_resp.intOf("product.variants[i].variant.weight");
        variant_visible = j_resp.boolOf("product.variants[i].variant.visible");
        i = i + 1;
    }
}