How To Update Price On Amazon Using Amazon MWS API In C#

Introduction 

 
In this tutorial, we will learn about updating the price for a 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 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 repricer.
  1. public class AmazonRepricerViewModel  
  2.     {  
  3.         public string Sku { getset; }  
  4.         public decimal Price { getset; }  
  5.         public bool isLive { getset; }  
  6.     } 
Now create a method as RepriceParticularAmazonProducts() or name it as you like.
  1. public void RepriceParticularAmazonProducts(AmazonRepricerViewModel vm)  
  2.         {  
  3.             StreamWriter InputFile, OutputFile, InventoryFile;  
  4.             var guid = Guid.NewGuid();  
  5.             var guid1 = Guid.NewGuid();  
  6.             var guid2 = Guid.NewGuid();  
  7.             //Input files  
  8.             var uploadRootFolderInput = AppDomain.CurrentDomain.BaseDirectory + "\\RepricerLogs\\Input Files";  
  9.             Directory.CreateDirectory(uploadRootFolderInput);  
  10.             var directoryFullPathInput = uploadRootFolderInput;  
  11.             if (directoryFullPathInput == null)  
  12.             {  
  13.                 CustomErrorRepricer("Can not find 'RepricerLogs' folder to save new Report!");  
  14.                 return;  
  15.             }  
  16.             string path = Path.Combine(directoryFullPathInput, "PriceUpdateAmazonItem_" + guid + ".txt");  
  17.             //Output files  
  18.             var uploadRootFolderOutput = AppDomain.CurrentDomain.BaseDirectory + "\\RepricerLogs\\Output Files";  
  19.             Directory.CreateDirectory(uploadRootFolderOutput);  
  20.             var directoryFullPathOutput = uploadRootFolderOutput;  
  21.             if (directoryFullPathOutput == null)  
  22.             {  
  23.                 CustomErrorRepricer("Can not find 'RepricerLogs' folder to save new Report!");  
  24.                 return;  
  25.             }  
  26.             string path1 = Path.Combine(directoryFullPathOutput, "PriceUpdateAmazonItem_" + guid1 + ".txt");  
  27.             //feed files  
  28.             var uploadRootFolderFeed = AppDomain.CurrentDomain.BaseDirectory + "\\RepricerLogs\\Feed Files";  
  29.             Directory.CreateDirectory(uploadRootFolderFeed);  
  30.             var directoryFullPathFeed = uploadRootFolderFeed;  
  31.             if (directoryFullPathFeed == null)  
  32.             {  
  33.                 CustomErrorRepricer("Can not find 'RepricerLogs' folder to save new Report!");  
  34.                 return;  
  35.             }  
  36.             string path2 = Path.Combine(directoryFullPathFeed, "PriceUpdateAmazonItem_" + guid2 + ".txt");  
  37.             InputFile = File.CreateText(path);  
  38.             OutputFile = File.CreateText(path1);  
  39.             InventoryFile = File.CreateText(path2);  
  40.             try  
  41.             {  
  42.                 StringBuilder str1 = new StringBuilder();  
  43.                 str1.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\t");  
  44.                 str1.AppendLine();  
  45.                 str1.Append("<AmazonItemRequest xmlns=\"urn: Amazon:apis: eBLBaseComponents\">\t");  
  46.                 str1.AppendLine();  
  47.                 str1.Append("<AmazonUser>" + "Do Not Use" + "</AmazonUser>\t");  
  48.                 str1.AppendLine();  
  49.                 str1.Append("<SKU>" + vm.Sku + "</SKU>\t");  
  50.                 str1.AppendLine();  
  51.                 str1.Append("<Price>" + vm.Price + "</Price>\t");  
  52.                 str1.AppendLine();  
  53.                 str1.Append("<Date>" + DateTime.Now.ToString() + "</Date>\t");  
  54.                 str1.AppendLine();  
  55.                 str1.Append("</AmazonItemRequest>\t");  
  56.                 str1.AppendLine();  
  57.                 InputFile.Write(str1.ToString());  
  58.                 InputFile.Close();  
  59.                 StringBuilder str = new StringBuilder();  
  60.                 str.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\t");  
  61.                 str.AppendLine();  
  62.                 str.Append("<AmazonEnvelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"amzn-envelope.xsd\">\t");  
  63.                 str.AppendLine();  
  64.                 str.Append("<Header>\t");  
  65.                 str.AppendLine();  
  66.                 str.Append("<DocumentVersion>1.01</DocumentVersion>\t");  
  67.                 str.AppendLine();  
  68.                 str.Append("<MerchantIdentifier>" + _accountId + "</MerchantIdentifier>\t");  
  69.                 str.AppendLine();  
  70.                 str.Append("</Header>\t");  
  71.                 str.AppendLine();  
  72.                 str.Append("<MessageType>Price</MessageType>\t");  
  73.                 str.AppendLine();  
  74.                 str.Append("<Message>\t");  
  75.                 str.AppendLine();  
  76.                 str.Append("<MessageID>1</MessageID>\t");  
  77.                 str.AppendLine();  
  78.                 str.Append("<OperationType>Update</OperationType>\t");  
  79.                 str.AppendLine();  
  80.                 str.Append("<Price>\t");  
  81.                 str.AppendLine();  
  82.                 str.Append("<SKU>" + vm.Sku + "</SKU>\t");  
  83.                 str.AppendLine();  
  84.                 str.Append("<StandardPrice currency=\"USD\">" + vm.Price + "</StandardPrice>\t");  
  85.                 str.AppendLine();  
  86.                 str.Append("</Price>\t");  
  87.                 str.AppendLine();  
  88.                 str.Append("</Message>\t");  
  89.                 str.AppendLine();  
  90.                 str.Append("</AmazonEnvelope>\t");  
  91.                 str.AppendLine();  
  92.                 InventoryFile.Write(str.ToString());  
  93.                 InventoryFile.Close();  
  94.                 var response = AmazonPriceUpdate(path2);  
  95.                 OutputFile.Write("Feed Submitted Successfully, Your submission id is " + response);  
  96.                 OutputFile.Close();  
  97.             }  
  98.             catch (WebException ex)  
  99.             {  
  100.                 string exMessage = ex.Message;  
  101.                 if (ex.Response != null)  
  102.                 {  
  103.                     using (var responseReader = new StreamReader(ex.Response.GetResponseStream()))  
  104.                     {  
  105.                         exMessage = responseReader.ReadToEnd();  
  106.                     }  
  107.                 }  
  108.                 OutputFile.Write(exMessage.ToString());  
  109.                 OutputFile.Close();  
  110.             }  
  111.         } 
As you can see we have created a CustomErrorRepricer method for catching the exception if it occurs while creating the folder.
  1. public static void CustomErrorRepricer(string message)  
  2.         {  
  3.             string lines = "Error occured at " + DateTime.Now.ToString("MM/dd/yyyy h:mm tt") + "\r\n";  
  4.             lines += "****************************************************************** \r\n";  
  5.             lines += "ErrorMessage: " + message + " \r\n";  
  6.             string path = AppDomain.CurrentDomain.BaseDirectory + "\\RepricerLogs\\Error Logs";  
  7.             if (!Directory.Exists(path))  
  8.             {  
  9.                 Directory.CreateDirectory(path);  
  10.             }  
  11.             string filePath = AppDomain.CurrentDomain.BaseDirectory + "\\Reports\\Logs\\" + "ReportLog_" + DateTime.Now.Date.ToShortDateString().Replace('/', '_') + ".txt";  
  12.             if (!System.IO.File.Exists(filePath))  
  13.             {  
  14.                 using (StreamWriter sw = System.IO.File.CreateText(filePath))  
  15.                 {  
  16.                     sw.WriteLine(lines);  
  17.                 }  
  18.             }  
  19.             else  
  20.             {  
  21.                 using (StreamWriter sw = System.IO.File.AppendText(filePath))  
  22.                 {  
  23.                     sw.WriteLine(lines);  
  24.                 }  
  25.             }  
  26.         }  
Now that 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 a path2 variable.
 
Now the function which will actually perform the operation for the updated price on Amazon:
  1. public string AmazonPriceUpdate(string Path)  
  2.         {  
  3.             SubmitFeedRequest feedRequest = new SubmitFeedRequest();  
  4.             feedRequest.Merchant = _accountId;  
  5.             feedRequest.FeedContent = File.Open(Path, FileMode.Open, FileAccess.Read);  
  6.             feedRequest.ContentMD5 = MarketplaceWebServiceClient.CalculateContentMD5(feedRequest.FeedContent);  
  7.             MarketplaceWebServiceConfig config = new MarketplaceWebServiceConfig();  
  8.             config.ServiceURL = _domain;  
  9.             config.SetUserAgentHeader(_appName, _version, "C Sharp");  
  10.             feedRequest.FeedContent.Position = 0;  
  11.             feedRequest.FeedType = "_POST_PRODUCT_PRICING_DATA_";  
  12.             MarketplaceWebServiceClient client = new MarketplaceWebServiceClient(_accessKey, _secretKey, config);  
  13.             SubmitFeedResponse feedResponse = client.SubmitFeed(feedRequest);  
  14.             FeedSubmissionInfo feedInfo = feedResponse.SubmitFeedResult.FeedSubmissionInfo;  
  15.             return feedInfo.FeedSubmissionId;  
  16.         }  
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 quantity for product using Amazon MWS API here.
 
Please give your valuable feedback/comments/questions about this article. Please let me know how you like and understand this article and how I could improve it.


Similar Articles