In this tutorial, we will learn about updating the quantity for the product on Amazon using the Feeds API in C#. Amazon allows us to upload Inventory and order data using the Feeds API. The Feeds API provided by Amazon is very easy to use as we have to upload the XML file in the Feed and proper FeedType for the action we have to do.

Prerequisites

  • Knowledge of C#
  • You must have the Amazon credentials like SellerId, Access Key, etc.

We should have DLL files should be installed using the Nuget package manager.

Now let’s get started…

First, create a view model for the AmazonUpdatePriceViewModel.

public class AmazonUpdatePriceViewModel
    {
        public string Sku { get; set; }
        public decimal Quantity { get; set; }
        public bool isLive { get; set; }
        public string FulfilledBy { get; set; }
    }

Now create a method as UpdateParticularAmazonProductsQuantity() or name it as you like.

public string UpdateParticularAmazonProductsQuantity(AmazonUpdatePriceViewModel vm)
        {
            try
            {
                StreamWriter InputFile;
                var guid = Guid.NewGuid();
                var uploadRootFolderInput = AppDomain.CurrentDomain.BaseDirectory + "\\RepricerLogs\\Input Files";
                Directory.CreateDirectory(uploadRootFolderInput);
                var directoryFullPathInput = uploadRootFolderInput;
                if (directoryFullPathInput == null)
                {
                    CustomErrorRepricer("Can not find 'Update Quantity Logs' folder to save new Report!");
                    return "Cannot find folder to save report";
                }
                string path = Path.Combine(directoryFullPathInput, "QuantityUpdateAmazonItem_" + guid + ".txt");
                InputFile = File.CreateText(path);
                StringBuilder str1 = new StringBuilder();
                str1.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\t");
                str1.AppendLine();
                str1.Append("<AmazonEnvelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"amzn-envelope.xsd\">\t");
                str1.AppendLine();
                str1.Append("<Header>\t");
                str1.AppendLine();
                str1.Append("<DocumentVersion>1.01</DocumentVersion>\t");
                str1.AppendLine();
                str1.Append("<MerchantIdentifier>" + _marketplaceID + "</MerchantIdentifier>\t");
                str1.AppendLine();
                str1.Append("</Header>\t");
                str1.AppendLine();
                str1.Append("<MessageType>Inventory</MessageType>\t");
                str1.AppendLine();
                str1.Append("<Message>\t");
                str1.AppendLine();
                str1.Append("<MessageID>1</MessageID>\t");
                str1.AppendLine();
                str1.Append("<OperationType>Update</OperationType>\t");
                str1.AppendLine();
                str1.Append("<Inventory>\t");
                str1.AppendLine();
                str1.Append("<SKU>" + vm.Sku + "</SKU>\t");
                str1.AppendLine();
                if (vm.FulfilledBy == "MFN")
                {
                    str1.Append("<Quantity>" + vm.Quantity + "</Quantity>\t");
                    str1.AppendLine();
                }
                else
                {
                    str1.Append("<FulfillmentCenterID>AMAZON_NA</FulfillmentCenterID>\t");
                    str1.AppendLine();
                    str1.Append("<Lookup>FulfillmentNetwork</Lookup>\t");
                    str1.AppendLine();
                }
                str1.Append("<SwitchFulfillmentTo>" + vm.FulfilledBy + "</SwitchFulfillmentTo>\t");
                str1.AppendLine();
                str1.Append("</Inventory>\t");
                str1.AppendLine();
                str1.Append("</Message>\t");
                str1.AppendLine();
                str1.Append("</AmazonEnvelope>\t");
                str1.AppendLine();
                InputFile.Write(str1.ToString());
                InputFile.Close();
                var response = AmazonQuantityUpdate(path);
                return response;
            }
            catch (Exception ex)
            {
                return "Something went wrong. Please contact administrator";
            }
        }

As you can see we have created a CustomErrorRepricer method for catching the exception if occurs while creating the folder.

public static void CustomErrorRepricer(string message)
        {
            string lines = "Error occured at " + DateTime.Now.ToString("MM/dd/yyyy h:mm tt") + "\r\n";
            lines += "****************************************************************** \r\n";
            lines += "ErrorMessage: " + message + " \r\n";
            string path = AppDomain.CurrentDomain.BaseDirectory + "\\RepricerLogs\\Error Logs";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            string filePath = AppDomain.CurrentDomain.BaseDirectory + "\\Reports\\Logs\\" + "ReportLog_" + DateTime.Now.Date.ToShortDateString().Replace('/', '_') + ".txt";
            if (!System.IO.File.Exists(filePath))
            {
                using (StreamWriter sw = System.IO.File.CreateText(filePath))
                {
                    sw.WriteLine(lines);
                }
            }
            else
            {
                using (StreamWriter sw = System.IO.File.AppendText(filePath))
                {
                    sw.WriteLine(lines);
                }
            }
        }

Now we have successfully created the XML file for the product we have to update the price on Amazon. And we have got the path for the file in path variable.

Now the function which will actually perform the operation for the updated quantity on Amazon.

public string AmazonQuantityUpdate(string Path)
        {
            SubmitFeedRequest feedRequest = new SubmitFeedRequest();
            feedRequest.Merchant = _accountId;
            feedRequest.FeedContent = File.Open(Path, FileMode.Open, FileAccess.Read);
            feedRequest.ContentMD5 = MarketplaceWebServiceClient.CalculateContentMD5(feedRequest.FeedContent);
            MarketplaceWebServiceConfig config = new MarketplaceWebServiceConfig();
            config.ServiceURL = _domain;
            config.SetUserAgentHeader(_appName, _version, "C Sharp");
            feedRequest.FeedContent.Position = 0;
            feedRequest.FeedType = "_POST_INVENTORY_AVAILABILITY_DATA_";
            MarketplaceWebServiceClient client = new MarketplaceWebServiceClient(_accessKey, _secretKey, config);
            SubmitFeedResponse feedResponse = client.SubmitFeed(feedRequest);
            FeedSubmissionInfo feedInfo = feedResponse.SubmitFeedResult.FeedSubmissionInfo;
            return "Trying to upload on Amazon, please check on Amazon 2 minutes later! Your Feed Submission ID is :" + feedInfo.FeedSubmissionId.ToString();
        }

The SubmitFeedRequest is used while making the API call as we have previously discussed.

When the call is a success we will get the FeedSubmissionId from it and we can check the result on Amazon Scratchpad by submitting the GetFeedSubmissionResult request from the Scratchpad and passing the Feed number as we got from the previous method.

You can also refer the Blog for updating the price for product using Amazon MWS API here.