Sample code for 30+ languages & platforms
Swift

eBay -- Create or Replace Inventory Item

See more eBay Examples

This example shows how to create a new inventory item record or update an existing inventory item record.

See Create or Replace Inventory Item for more REST API details.

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.

    // This example sends the following sample PUT request to create (or replace) a new inventory item.

    // PUT https://api.ebay.com/sell/inventory/v1/inventory_item/GP-Cam-01
    // { 
    // "availability":
    //     { 
    //     "shipToLocationAvailability":
    //         { 
    //         "quantity": 50
    //         }
    //     },
    // "condition": "NEW",
    // "product":
    //     { 
    //     "title": "GoPro Hero4 Helmet Cam",
    //     "description": "New GoPro Hero4 Helmet Cam. Unopened box.",
    //     "aspects": {
    //         "Brand" :["GoPro"],
    //         "Type" : ["Helmet/Action"],
    //         "Storage Type" : ["Removable"],
    //         "Recording Definition" : ["High Definition"],
    //         "Media Format" : ["Flash Drive (SSD)"],
    //         "Optical Zoom" : ["10x"]
    //       },
    //     "imageUrls": [
    //         "http://i.ebayimg.com/images/i/182196556219-0-1/s-l1000.jpg",
    //         "http://i.ebayimg.com/images/i/182196556219-0-1/s-l1001.jpg",
    //         "http://i.ebayimg.com/images/i/182196556219-0-1/s-l1002.jpg"
    //       ]
    //     }
    // }

    // First, generate the JSON using this code:
    let json = CkoJsonObject()!
    json.emitCompact = false

    json.updateNumber(jsonPath: "availability.shipToLocationAvailability.quantity", numericStr: "50")
    json.updateString(jsonPath: "condition", value: "NEW")
    json.updateString(jsonPath: "product.title", value: "GoPro Hero4 Helmet Cam")
    json.updateString(jsonPath: "product.description", value: "New GoPro Hero4 Helmet Cam. Unopened box.")
    json.updateString(jsonPath: "product.aspects.Brand[0]", value: "GoPro")
    json.updateString(jsonPath: "product.aspects.Type[0]", value: "Helmet/Action")
    json.updateString(jsonPath: "product.aspects.\"Storage Type\"[0]", value: "Removable")
    json.updateString(jsonPath: "product.aspects.\"Recording Definition\"[0]", value: "High Definition")
    json.updateString(jsonPath: "product.aspects.\"Media Format\"[0]", value: "Flash Drive (SSD)")
    json.updateString(jsonPath: "product.aspects.\"Optical Zoom\"[0]", value: "10x")
    json.updateString(jsonPath: "product.imageUrls[0]", value: "http://i.ebayimg.com/images/i/182196556219-0-1/s-l1000.jpg")
    json.updateString(jsonPath: "product.imageUrls[1]", value: "http://i.ebayimg.com/images/i/182196556219-0-1/s-l1001.jpg")
    json.updateString(jsonPath: "product.imageUrls[2]", value: "http://i.ebayimg.com/images/i/182196556219-0-1/s-l1002.jpg")

    // Show the JSON to be sent:
    print("\(json.emit()!)")

    // Use a previously obtained user token.  The token should look something like this:
    // "v^1.1#i^1#r^0#p^3#I^3#f^0#t^H4sIAAAAAAAAAOVXa2wUVRTu9k ... 89xuCWYREAAA=="
    var accessToken: String? = "EBAY_ACCESS_TOKEN"

    let http = CkoHttp()!

    // This example uses the sandbox.  
    // Change "api.sandbox.ebay.com" to "api.ebay.com" to use the production system.
    // Note: The last part of the url is the SKU.  In this URL, the SKU is "GP-Cam-01".
    var url: String? = "https://api.sandbox.ebay.com/sell/inventory/v1/inventory_item/GP-Cam-01"
    json.emitCompact = true

    // Set your Content-Language to whatever is desired.
    http.setRequestHeader(name: "Content-Language", value: "en-US")

    // Add our access token to the request, which is a header
    // having the following format:
    // Authorization: Bearer <userAccessToken>
    let sbAuth = CkoStringBuilder()!
    sbAuth.append(value: "Bearer ")
    sbAuth.append(value: accessToken)
    http.setRequestHeader(name: "Authorization", value: sbAuth.getAsString())

    http.accept = "application/json"
    http.allowGzip = false

    let resp = CkoHttpResponse()!
    success = http.httpStr(verb: "PUT", url: url, bodyStr: json.emit(), charset: "utf-8", contentType: "application/json", response: resp)
    if success == false {
        print("\(http.lastErrorText!)")
        return
    }

    print("Response status code = \(resp.statusCode.intValue)")

    if http.lastStatus.intValue != 204 {
        print("\(resp.bodyStr!)")
        print("Failed")
        return
    }

    // On success (status code = 204), there is no output payload (strResponse will be empty).
    print("Inventory item successfully created.")

}