title: SP-API PUT Item request from NetSuite
body: <blockquote>
  <p>I am trying to call sp-api put item request and below is my code</p>
  </blockquote>
  <pre><code>var amz_url = &quot;https://api.amazon.com/auth/o2/token&quot;,
                  grant_type = &quot;refresh_token&quot;,
                  refresh_token = &quot;{refreshtoken}&quot;,
                  client_id = &quot;amzn1.application-oa2-client.e87c20f1bc464aad8652******&quot;,
                  client_secret = &quot;46b8fbaef6285e8245b3a99d5a15ac8773e9d775bca44cb2220738a5f1*****&quot;;
  </code></pre>
  <blockquote>
  <p>authorization code</p>
  </blockquote>
  <pre><code>            let headers = ({
                  'Content-Type': 'application/x-www-form-urlencoded',
                  'Accept': '*/*',
                  'Accept-Encoding': 'gzip, deflate, br',
                  'Connection': 'keep-alive',
                  'Cache-Control': 'no-cache',
                  'Host': 'api.amazon.com'
              });
  
              let response = https.post({
                  url: amz_url,
                  body: { grant_type: '' + grant_type + '', refresh_token: '' + refresh_token + '', client_id: '' + client_id + '', client_secret: '' + client_secret + '' },
                  headers: headers
              });
  
              log.debug(&quot;resp-code&quot;, response.code);
              log.debug(&quot;resp-body&quot;, JSON.parse(response.body).refresh_token);
  
              var accessToken = JSON.parse(response.body).access_token;
  
              //log.debug('Response is ', accessToken); //token retrieved successfully
  </code></pre>
  <blockquote>
  <p>All good till here</p>
  </blockquote>
  <pre><code>            // Getting access token - END
  
              // Getting STS token
              var awsMainAccessKey = &quot;AKIA4OOZPXxxxxxxx&quot;,
                  awsMainSecretKey = &quot;QbcC9g9ylrEGoy4aEUJ0uMkyq6xxxxxxx&quot;;
  
              var stsResponse = stsRequest(awsMainAccessKey,awsMainSecretKey);
  
              //Calculating timestamp in ISO 8601  format
              var today = new Date();
  
              var ISO8601Date = getXAmzDate();
              log.debug('ISO 8601 date format ', ISO8601Date);
  
              var bodyObj = {},
                  condArray = [],
                  itemArray = [],
                  attributesObj = {},
                  marketPlaceId = &quot;A1F83G8C2ARO7P&quot;,
                  sellerId = &quot;A3F8NJ8FFYFH4O&quot;,
                  productSKU = &quot;TKR45UVDS&quot;;
  </code></pre>
  <blockquote>
  <p>body hash for this being generated differently than what amazon is expecting</p>
  </blockquote>
  <pre><code>            var bodyObj_final = {
                  &quot;productType&quot;: &quot;LUGGAGE&quot;,
                  &quot;requirements&quot;: &quot;LISTING&quot;,
                  &quot;attributes&quot;: {
                    &quot;condition_type&quot;: [
                      {
                        &quot;value&quot;: &quot;new_new&quot;,
                        &quot;marketplace_id&quot;: marketPlaceId
                      }
                    ],
                    &quot;item_name&quot;: [
                      {
                        &quot;value&quot;: &quot;AmazonBasics 16 Underseat Spinner Carry-On&quot;,
                        &quot;language_tag&quot;: &quot;en_US&quot;,
                        &quot;marketplace_id&quot;: marketPlaceId
                      }
                    ]
                  }
              }
  
              bodyObj.productType = &quot;LUGGAGE&quot;;
              //bodyObj.productType = &quot;LUGGAGE&quot;;
  
              condArray.push({
                  &quot;value&quot;: &quot;new_new&quot;,
                  &quot;marketplace_id&quot;: marketPlaceId
              });
  
              itemArray.push({
                  &quot;value&quot;: &quot;DC Basics 16 Inches rterdfertr&quot;,
                  &quot;language_tag&quot;: &quot;en_UK&quot;,
                  &quot;marketplace_id&quot;: marketPlaceId
              });
  
              attributesObj.condition_type = condArray;
  
              attributesObj.item_name = itemArray;
  
              bodyObj.attributes = attributesObj;
  
              var secToken = stsResponse.Credentials.SessionToken;
  
              method = 'PUT';
  
              var contenthash = &quot;beaead3198f7da1e70d03ab969765e0821b24fc913697e929e726aeaebf0eba3&quot;;
  </code></pre>
  <blockquote>
  <p>Canonical uri and query string</p>
  </blockquote>
  <pre><code>            canonical_uri = encodeURI('/listings/2021-08-01/items/' + sellerId +'/'+productSKU);
  
              //canonical_uri = '';
  
              canonical_querystring = encodeURI('marketplaceIds')+'='+encodeURI('A1F83G8C2ARO7P');
              //canonical_querystring = '';
  
              var hmacsha256Data = CryptoJS.SHA256(JSON.stringify(bodyObj_final));
              var payload_hash = CryptoJS.enc.Hex.stringify(hmacsha256Data);
  
              //canonical_headers = 'host:' + 'sellingpartnerapi-eu.amazon.com' + '\n' + 'x-amz-access-token:' + accessToken + '\n' + 'x-amz-date:' + ISO8601Date + '\n';
  
              canonical_headers = 'host:' + 'sellingpartnerapi-eu.amazon.com' + '\n' + 'x-amz-content-sha256:' + contenthash + '\n'+ 'x-amz-date:' + ISO8601Date + '\n'+ 'x-amz-security-token:' + secToken + '\n';
  
  
              signed_headers = &quot;host;x-amz-content-sha256;x-amz-date;x-amz-security-token&quot;;
  
              //log.debug('Payload hash data ', hmacsha256Data);
              log.debug('Payload payload_hash ', payload_hash);
  
              var paylhash = &quot;31ff10db41d2c210f7beb1adcd5b71c5ab2ec7ed13ad3972be88ff06db177aed&quot;;
  
              //var paylhash = &quot;209c90ceefe76183a6d038fd4474a0d2bc326bfea21b72722474f8e8f395c263&quot;;
  
              //var paylhash = &quot;1782d2d6f688ed659a8a0b511d97525843bc9d727c4d792b37c29e67df57fc8f&quot;;
  
              canonical_request = method + '\n' + canonical_uri + '\n' + canonical_querystring + '\n' + canonical_headers + '\n' + signed_headers + '\n' + paylhash;
  
              log.debug('URI String ', canonical_request);
  
              /*************** TASK 2: CREATE THE STRING TO SIGN************* */
  
              var monthStr = (today.getUTCMonth()+1),
                  dayStr = today.getUTCDate();
  
              var datestamp = today.getUTCFullYear() + '' + (monthStr&gt;9?monthStr:('0'+monthStr)) + '' + (dayStr&gt;9?dayStr:('0'+dayStr));
  
              region = 'eu-west-1';
              service = 'execute-api';
              algorithm = 'AWS4-HMAC-SHA256';
  
              var canonicalsha256Data = CryptoJS.SHA256(canonical_request);
              var canonical_hash = CryptoJS.enc.Hex.stringify(canonicalsha256Data);
  
              //log.debug('canonicalsha256Data is ', canonicalsha256Data);
  
              log.debug('canonical_hash is ', canonical_hash);
  
              credential_scope = datestamp + '/' + region + '/' + 'execute-api' + '/' + 'aws4_request';
              string_to_sign = algorithm + '\n' + ISO8601Date + '\n' + credential_scope + '\n' + canonical_hash;
  
              log.debug('String to Sign ', string_to_sign);
  
              /************* TASK 3: CALCULATE THE SIGNATURE *************/
  
              //stsResponse
  
              secret_key = stsResponse.Credentials.SecretAccessKey; //secret key for iam user 
              access_key = stsResponse.Credentials.AccessKeyId; // access key for iam user
  
              signing_key = getSignatureKey(secret_key, datestamp, region, service);
  
              var signingsha256Data = CryptoJS.HmacSHA256(string_to_sign, signing_key);
  
              var signature = CryptoJS.enc.Hex.stringify(signingsha256Data);
  
              log.debug('Final signature is ', signature);
  
              /************* TASK 4: ADD SIGNING INFORMATION TO THE REQUEST *************/
              authorization_header = algorithm + ' ' + 'Credential=' + access_key + '/' + credential_scope + ', ' + 'SignedHeaders=' + signed_headers + ', ' + 'Signature=' + signature;
  
              
  
              log.debug('authorization_header is ', authorization_header);
  
              headers = { 
                  'host': 'sellingpartnerapi-eu.amazon.com', 
                  'x-amz-access-token': accessToken,
                  'X-Amz-Security-Token': secToken,
                  'x-Amz-Content-Sha256': contenthash,
                  'x-amz-date': ISO8601Date, 
                  'Authorization': authorization_header, 
                  'Content-Type': 'application/json',
                  'ACCEPT': '*/*'
              }
  
              /************** SEND THE REQUEST **************/
              request_url = 'https://sellingpartnerapi-eu.amazon.com'+canonical_uri+'?' + canonical_querystring;
  
              //request_url = 'https://sellingpartnerapi-eu.amazon.com/listings/2021-08-01/items/A3F8NJ8FFYFH4O/ABC123'+'?' + canonical_querystring;
  
              log.debug('Request URL is ', request_url);
  
              var response_end = https.put({
                  url: request_url,
                  headers: headers,
                  body : bodyObj
              });
  
              log.debug('Response Code is ', response_end.code); 
              log.debug('Response is ', response_end.body); 
  </code></pre>
  <blockquote>
  <p>I am getting response as 400 :{ &quot;errors&quot;: [ { &quot;code&quot;: &quot;InvalidInput&quot;, &quot;message&quot;: &quot;Invalid Input&quot;, &quot;details&quot;: &quot;&quot; } ] }</p>
  </blockquote>
  <blockquote>
  <p>Other questions I have,
  I am trying to generate payload hash but amazon is expecting different thing, when I replace amazon expected payload it works(hard coded as paylhash in this script) otherwise 403 error</p>
  </blockquote>
  <blockquote>
  <p>Did anyone came across same issue or if someone can identify what I am doing wrong will be a great help.</p>
  </blockquote>