Sample code for 30+ languages & platforms
Swift

Etsy: Get the Inventory for a Listing

See more Etsy Examples

Gets the inventory for a listing.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

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

    let http = CkoHttp()!

    // Implements the following CURL command:

    // curl -X GET \
    //   https://openapi.etsy.com/v2/listings/listing_id/inventory?api_key=MY_ETSY_KEYSTRING

    let sbResponseBody = CkoStringBuilder()!
    success = http.quickGetSb(url: "https://openapi.etsy.com/v2/listings/listing_id/inventory?api_key=MY_ETSY_KEYSTRING", sbContent: sbResponseBody)
    if success == false {
        print("\(http.lastErrorText!)")
        return
    }

    let jResp = CkoJsonObject()!
    jResp.loadSb(sb: sbResponseBody)
    jResp.emitCompact = false

    print("Response Body:")
    print("\(jResp.emit()!)")

    var respStatusCode: Int = http.lastStatus.intValue
    print("Response Status Code = \(respStatusCode)")
    if respStatusCode >= 400 {
        print("Response Header:")
        print("\(http.lastHeader!)")
        print("Failed.")
        return
    }

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

    // {
    //   "count": 1,
    //   "results": {
    //     "products": [
    //       {
    //         "product_id": 3361120103,
    //         "property_values": [
    //         ],
    //         "offerings": [
    //           {
    //             "offering_id": 3579642570,
    //             "price": {
    //               "amount": 16000,
    //               "divisor": 100,
    //               "currency_code": "USD",
    //               "currency_formatted_short": "$160.00",
    //               "currency_formatted_long": "$160.00 USD",
    //               "currency_formatted_raw": "160.00"
    //             },
    //             "quantity": 1
    //           }
    //         ]
    //       }
    //     ]
    //   },
    //   "params": {
    //     "listing_id": "720138253",
    //     "write_missing_inventory": false
    //   },
    //   "type": "ListingInventory",
    //   "pagination": {}
    // }

    // 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 product_id: Int
    var j: Int
    var count_j: Int
    var offering_id: Int
    var priceAmount: Int
    var priceDivisor: Int
    var priceCurrency_code: String?
    var priceCurrency_formatted_short: String?
    var priceCurrency_formatted_long: String?
    var priceCurrency_formatted_raw: String?
    var quantity: Int

    var count: Int = jResp.int(of: "count").intValue
    var paramsListing_id: String? = jResp.string(of: "params.listing_id")
    var paramsWrite_missing_inventory: Bool = jResp.bool(of: "params.write_missing_inventory")
    var v_type: String? = jResp.string(of: "type")
    var i: Int = 0
    var count_i: Int = jResp.size(ofArray: "results.products").intValue
    while i < count_i {
        jResp.i = i
        product_id = jResp.int(of: "results.products[i].product_id").intValue
        j = 0
        count_j = jResp.size(ofArray: "results.products[i].property_values").intValue
        while j < count_j {
            jResp.j = j
            j = j + 1
        }

        j = 0
        count_j = jResp.size(ofArray: "results.products[i].offerings").intValue
        while j < count_j {
            jResp.j = j
            offering_id = jResp.int(of: "results.products[i].offerings[j].offering_id").intValue
            priceAmount = jResp.int(of: "results.products[i].offerings[j].price.amount").intValue
            priceDivisor = jResp.int(of: "results.products[i].offerings[j].price.divisor").intValue
            priceCurrency_code = jResp.string(of: "results.products[i].offerings[j].price.currency_code")
            priceCurrency_formatted_short = jResp.string(of: "results.products[i].offerings[j].price.currency_formatted_short")
            priceCurrency_formatted_long = jResp.string(of: "results.products[i].offerings[j].price.currency_formatted_long")
            priceCurrency_formatted_raw = jResp.string(of: "results.products[i].offerings[j].price.currency_formatted_raw")
            quantity = jResp.int(of: "results.products[i].offerings[j].quantity").intValue
            j = j + 1
        }

        i = i + 1
    }


}