cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Looking for a working example of C# HttpWebRequest for createOrReplaceInventoryItemInventory

Hi, 

I have had great success using the REST API to download order details, greatly reducing the workflow between getting an Order and processing the details into my accounting platform. I use a couple of recursive functions to capture any OAuth Token refreshment but the crux of the API interaction is achieved with the following code snippet; again this is just an example of how my Order Download function works... 

try
{
  string apiEndpoint = "https://api.ebay.com/sell/fulfillment/v1/order/" + orderNumber;
  HttpWebRequest request = (HttpWebRequest)WebRequest.Create(apiEndpoint);
  request.Method = "GET";
  request.Accept = "application/json";
  request.Headers.Add("Authorization", "Bearer " + _oauthUserToken);
  request.ContentType = "application/json";
  HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  Stream responseStream = response.GetResponseStream();
  StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
  string responseString = reader.ReadToEnd();

  // Deserialize the response string to EbayOrder object.
     ...
}

 

BUT, when I look for code examples to apply what I assume is a correctly formatted PUT request...

string apiEndpoint = "https://api.ebay.com/sell/inventory/v1/inventory_item/" + sku;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(apiEndpoint);
request.Method = "PUT"; 
request.Accept = "application/json";
request.Headers.Add("Authorization", "Bearer " + _oauthUserToken);
request.Headers.Add("Content-Language", "en-US");
request.ContentType = "application/json";

 ... I just get a 500 Error. ChatGPT has tried to give me some examples but none are giving me the expected 204 response. Such as...

// Create the HttpClient
using (HttpClient client = new HttpClient())
{
  client.DefaultRequestHeaders.Add("Authorization", $"Bearer {_oauthUserToken}");
  // Construct the API endpoint URL
  string url = "https://api.ebay.com/sell/inventory/v1/inventory_item/" + sku;
  // Send the PUT request to add the inventory item
  HttpResponseMessage response = client.PutAsync(url, null).GetAwaiter().GetResult();


But this doesn't work either. 

 

Once the above is answered I still need to work out the remaining 3 steps of:

  1. Create your inventory item using createOrReplaceInventoryItem call
  2. Create an inventory location for the created item through the createInventoryLocation API call. Your offer won't publish without this.
  3. Create an offer for the inventory item. Specify the offer details, such as fulfillment, payment, return policy IDs, category ID, inventory location ID, etc
  4. Publish the offer.

... As hard as all this is, it will still beat the hell out of trying to list 100 new items using eBays latest awful Web Portal for creating new listing... 

 

Fingers crossed there's a benevolent developer out there !!

Message 1 of 2
latest reply
1 REPLY 1

Looking for a working example of C# HttpWebRequest for createOrReplaceInventoryItemInventory

Answered my own question : ) ChatGPT and I finally worked out you can't apply a null value in client.PutAsync. Modified code snippet reads:

using (HttpClient client = new HttpClient())
{
  client.DefaultRequestHeaders.Add("Authorization", $"Bearer {_oauthUserToken}");
  // Construct the API endpoint URL
  string url = "https://api.ebay.com/sell/inventory/v1/inventory_item/" + sku;
  // Serialize the inventory item to JSON
  string serializedInventoryItem = JsonConvert.SerializeObject(_inventoryItem);
  // Create the HttpContent with the serialized inventory item as JSON
  HttpContent content = new StringContent(serializedInventoryItem, Encoding.UTF8, "application/json");
  // Set the "Content-Language" header on the HttpRequestMessage
  content.Headers.Add("Content-Language", "en-US");
  // Send the PUT request with the inventory item as the content
  HttpResponseMessage response = client.PutAsync(url, content).GetAwaiter().GetResult();
  // Check the response status
 if (!response.IsSuccessStatusCode)
 {
    // Error occurred
    string errorMessage = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
    MessageBox.Show($"Failed to add inventory item. Error: {errorMessage}", "in createOrReplaceInventoryItem_execute");
  }
}

 

Hope this helps someone. The _inventoryItem is a simple class built initally from the demo item you can create in the API Explorer, an Apple watch or something... The Class is defined below but the actual properties will change depending on your inventory item... 

    public class InventoryItem
    {
        public string sku { get; set; }
        public string locale { get; set; }
        public Product product { get; set; }
        public string condition { get; set; }
        public PackageWeightAndSize packageWeightAndSize { get; set; }
        public Availability availability { get; set; }
    }

    public class Product
    {
        public string title { get; set; }
        public Aspects aspects { get; set; }
        public string description { get; set; }
        public List<string> upc { get; set; }
        public List<string> imageUrls { get; set; }
    }

    public class Aspects
    {
        public List<string> CPU { get; set; }
        public List<string> Feature { get; set; }
    }

    public class PackageWeightAndSize
    {
        public Dimensions dimensions { get; set; }
        public string packageType { get; set; }
        public Weight weight { get; set; }
    }

    public class Dimensions
    {
        public double width { get; set; }
        public double length { get; set; }
        public double height { get; set; }
        public string unit { get; set; }
    }

    public class Weight
    {
        public double value { get; set; }
        public string unit { get; set; }
    }

    public class Availability
    {
        public ShipToLocationAvailability shipToLocationAvailability { get; set; }
    }

    public class ShipToLocationAvailability
    {
        public int quantity { get; set; }
        public AllocationByFormat allocationByFormat { get; set; }
    }

    public class AllocationByFormat
    {
        public int auction { get; set; }
        public int fixedPrice { get; set; }
    }

 

Message 2 of 2
latest reply