npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

amazon-sp-api-timeout

v0.7.11

Published

Amazon Selling Partner API client

Downloads

10

Readme

amazon-sp-api (client for the Amazon Selling Partner API)

The client handles calls to the Amazon Selling Partner API. It wraps up all the necessary stuff such as requesting access token, security token and signing requests with AWS4 signature.

! Thank you for using our module!
! Interested in supporting us to build the best Amazon Seller Tool out there?
- We are currently hiring Full Stack Developers (Techstack React/NextJS/MongoDB/Node).
! Just drop us a line at [email protected] or visit us at amz.tools.
! We'll definitely get back to you!

Contents

Prerequisites

Make sure that you followed the Selling Partner API Developer Guide and have successfully completed the steps Registering as a developer, Registering your application and have a valid refresh token (if you use the client only for your own seller account the easiest way is using the self authorization as described in the developer guide).

Installation

npm install amazon-sp-api

Getting Started

Before you can use the client you need to add your app client and aws user credentials.

Setting credentials from environment variables

Setting credentials from file

Instead of setting the credentials via environment variables you may load them from a credentials file. The default path to the file is ~/.amzspapi/credentials (path can be changed when creating a client) and you add the credentials one per line:

SELLING_PARTNER_APP_CLIENT_ID=<YOUR_APP_CLIENT_ID>
SELLING_PARTNER_APP_CLIENT_SECRET=<YOUR_APP_CLIENT_SECRET>
AWS_ACCESS_KEY_ID=<YOUR_AWS_USER_ID>
AWS_SECRET_ACCESS_KEY=<YOUR_AWS_USER_SECRET>
AWS_SESSION_TOKEN=<YOUR_AWS_SESSION_TOKEN> // Only necessary when using temporary AWS STS security credentials
AWS_SELLING_PARTNER_ROLE=<YOUR_AWS_SELLING_PARTNER_API_ROLE>

Setting credentials from constructor config object

Although the most convenient and recommended way of setting the credentials is via environment variables or config file it is also possible to pass the credentials inside the config object when creating an instance of the client (i.e. if you have no means of using env vars or a config file). The structure of the constructor config object will be explained below.

Usage

Require library:

const SellingPartnerAPI = require('amazon-sp-api');

Create client and call API:

(async() => {
  try {
    let sellingPartner = new SellingPartnerAPI({
      region:'eu', // The region to use for the SP-API endpoints ("eu", "na" or "fe")
      refresh_token:'<REFRESH_TOKEN>' // The refresh token of your app user
    });
    let res = await sellingPartner.callAPI({
      operation:'getMarketplaceParticipations',
      endpoint:'sellers'
    });
    console.log(res);
  } catch(e){
    console.log(e);
  }
})();

Config params

The class constructor takes a config object with the following structure as input:

{
  region:'<REGION>',
  refresh_token:'<REFRESH_TOKEN>',
  access_token:'<ACCESS_TOKEN>',
  role_credentials:{
    id:'<TEMPORARY_ROLE_ACCESS_ID>',
    secret:'<TEMPORARY_ROLE_ACCESS_SECRET>',
    security_token:'<TEMPORARY_ROLE_SECURITY_TOKEN>'
  },
  endpoints_versions:{
    ...
  },
  credentials:{
    SELLING_PARTNER_APP_CLIENT_ID:'<APP_CLIENT_ID>',
    SELLING_PARTNER_APP_CLIENT_SECRET:'<APP_CLIENT_SECRET>',
    AWS_ACCESS_KEY_ID:'<AWS_USER_ID>',
    AWS_SECRET_ACCESS_KEY:'<AWS_USER_SECRET>',
    AWS_SESSION_TOKEN: '<AWS_SESSION_TOKEN>',
    AWS_SELLING_PARTNER_ROLE:'<AWS_SELLING_PARTNER_API_ROLE>'
  },
  options:{
    credentials_path:'~/.amzspapi/credentials',
    auto_request_tokens:true,
    auto_request_throttled:true,
    version_fallback:true,
    use_sandbox:false,
    only_grantless_operations:false,
    user_agent:'amazon-sp-api/<CLIENT_VERSION> (Language=Node.js/<NODE_VERSION>; Platform=<OS_PLATFORM>/<OS_RELEASE>)',
    debug_log:false
  }
}

Valid properties of the config object:

| Name | Type | Default | Description | | :----------------------------------- | :----: | :-----: | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | regionrequired | string | - | The region to use for the SP-API endpoints.Must be one of: eu, na or fe | | refresh_tokenoptional | string | - | The refresh token of your app user.Required if only_grantless_operations option is set to false. | | access_tokenoptional | string | - | The temporary access token requested with the refresh token of the app user. | | role_credentialsoptional | object | - | The temporary role credentials for the sellingpartner api role of the iam user. Must include the three properties id, secret and security_token with their corresponding values. | | endpoints_versionsoptional | object | - | Defines the version to use for an endpoint as key/value pairs, i.e. "reports":"2021-06-30". If none given the client is using the first (meaning the oldest) version for an endpoint.Call .endpoints on class instance to retrieve a complete list of all endpoints, versions and operations supported by the client. | | credentialsoptional | object | - | The app client and aws user credentials. Must include the five credentials properties SELLING_PARTNER_APP_CLIENT_ID, SELLING_PARTNER_APP_CLIENT_SECRET, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SELLING_PARTNER_ROLE with their corresponding values. Must also include AWS_SESSION_TOKEN when using temporary AWS STS security credentials.NOTE: Should only be used if you have no means of using environment vars or credentials file! | | optionsoptional | object | - | Additional options, see table below for all possible options properties. |

Valid properties of the config options:

| Name | Type | Default | Description | | :------------------------------------------ | :-----: | :---------------------------------------------------------------------------------------------------: | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | credentials_pathoptional | string | ~/.amzspapi/credentials | A custom absolute path to your credentials file location. | | auto_request_tokensoptional | boolean | true | Whether or not the client should retrieve new access and role credentials if non given or expired. | | auto_request_throttledoptional | boolean | true | Whether or not the client should automatically retry a request when throttled. | | version_fallbackoptional | boolean | true | Whether or not the client should try to use an older version of an endpoint if the operation is not defined for the desired version. | | use_sandboxoptional | boolean | false | Whether or not to use the sandbox endpoint. | | only_grantless_operationsoptional | boolean | false | Whether or not to only use grantless operations. | | user_agentoptional | string | amazon-sp-api/<CLIENT_VERSION> (Language=Node.js/<NODE_VERSION>; Platform=<OS_PLATFORM>/<OS_RELEASE>) | A custom user-agent header (see desired format in docs). | | debug_logoptional | boolean | false | Whether or not the client should print console logs for debugging purposes. |

Exchange an authorization code for a refresh token

If you already have a refresh token you can skip this step. If you only want to use the API for your own seller account you can just use the self authorization to obtain a valid refresh token.

If you want to exchange an authorization code of a seller you can use the .exchange() function of the client. The neccessary authorization code is returned to your callback URI as spapi_oauth_code when a seller authorizes your application (see authorization workflow in docs) or via a call to the getAuthorizationCode operation if you want to authorize a seller for the SP-API who has previously authorized you for the MWS API (the getAuthorizationCode workflow is explained in the Grantless operations section).

Once you have obtained the authorization_code you can exchange it for a refresh token:

let sellingPartner = new SellingPartnerAPI({
  region:'eu',
  options:{
    only_grantless_operations:true
  }
});
let res = await sellingPartner.exchange('<SELLER_AUTHORIZATION_CODE>');
console.log(res.refresh_token);

NOTE: You will have to create a new class instance once you have obtained the refresh_token and pass it inside the constructor in order to make calls to the API.

Request access token and role credentials

If you only provide the region and refresh_token parameters the client will automatically request access_token and role_credentials for you (with a TTL of 1 hour) and reuse these for future api calls for the class instance.

Instead of having the client handle the access_token and role_credentials requests automatically, you may also refresh them manually:

let sellingPartner = new SellingPartnerAPI({
  region:'eu',
  refresh_token:'<REFRESH_TOKEN>',
  options:{
    auto_request_tokens:false
  }
});
await sellingPartner.refreshAccessToken();
await sellingPartner.refreshRoleCredentials();

If you want to use the same credentials for multiple instances you can retrieve them via getters and use them as input for a new instance:

let access_token = sellingPartner.access_token;
let role_credentials = sellingPartner.role_credentials;

let sellingPartnerNewInstance = new SellingPartnerAPI({
  region:'eu',
  refresh_token:'<REFRESH_TOKEN>',
  access_token:access_token,
  role_credentials:role_credentials
});

Call the API

All calls to the SP-API will be triggered by using the .callAPI() function, which takes an object with the following structure as input:

{
  operation:'<OPERATION_TO_CALL>',
  endpoint:'<ENDPOINT_OF_OPERATION>',
  path:{
    ...
  },
  query:{
    ...
  },
  body:{
    ...
  },
  api_path:'<FULL_PATH_OF_OPERATION>',
  method:'GET',
  restricted_data_token:'<RESTRICTED_DATA_TOKEN>',
  options:{
    version:'<OPERATION_ENDPOINT_VERSION>',
    restore_rate:'<RESTORE_RATE_IN_SECONDS>',
    raw_result:false
  }
}

Valid properties of the object:

| Name | Type | Default | Description | | :-------------------------------------- | :----: | :-----: | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | operationoptional | string | - | The operation you want to request, see SP API Developer Guide.May also include endpoint as shorthand dot notation.Call .endpoints on class instance to retrieve a complete list of all endpoints, versions and operations supported by the client.Required if api_path is not defined. | | endpointoptional | string | - | The endpoint of the operation, (see Endpoints).Call .endpoints on class instance to retrieve a complete list of all endpoints, versions and operations supported by the client.Required if endpoint is not part of operation as shorthand dot notation and api_path is not defined. | | pathoptional | object | - | The input paramaters added to the path of the operation. | | queryoptional | object | - | The input paramaters added to the query string of the operation. | | bodyoptional | object | - | The input paramaters added to the body of the operation. | | api_pathoptional | string | - | The full api path of an operation. Can be used to call operations that are not yet supported or have a new version that is not yet supported by the client.Required if operation is not defined. | | methodoptional | string | - | The HTTP method to use.Required only if api_path is defined.Must be one of: GET, POST, PUT,DELETE or PATCH. | | restricted_data_tokenoptional | string | - | A token received from a createRestrictedDataToken operation. Neccessary to include PII (Personally Identifiable Informaton) for some restricted operations, see Tokens API use case guide for a list of restricted operations.NOTE: Your developer account must be approved for PII by Amazon in order to be able to receive PII, otherwise the token will have no effect, meaning the result of restricted operations will not include PII. | | optionsoptional | object | - | Additional options, see table below for all possible options properties. |

Valid properties of the config options:

| Name | Type | Default | Description | | :----------------------------- | :-----: | :-----: | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | versionoptional | string | - | The endpoint's version that should be used when calling the operation. Will be preferred over an endpoints_versions setting.NOTE: The call might still use an older version of the endpoint if the operation is not available for the specified version and version_fallback is set to true. | | restore_rateoptional | number | - | The restore rate (in seconds) that should be used when calling the operation. Will be preferred over the default restore rate of the operation. | | raw_resultoptional | boolean | false | Whether or not the client should return the "raw" result, which will include the raw body, buffer chunks, statuscode and headers of the result. This will skip the internal formatting or error checking, but might be helpful when you need additional information besides the payload or when the client encounters JSON.parse errors such as the ones already encountered with old finance documents (see Known Issues). | | response_timeoutoptional | number | - | Milliseconds; if exceeded request will abort with an API_RESPONSE_TIMEOUT error code. Response timeout is the time between sending request and receiving the first byte of the response. Includes DNS and connection time. | | deadline_timeoutoptional | number | - | Milliseconds; if exceeded request will abort with an API_DEADLINE_TIMEOUT error code. Deadline is the time from start of the request to receiving response body in full. If the deadline is too short large responses may not load at all on slow connections. |

Examples

To call an operation of an API endpoint you pass in the operation and the endpoint it belongs to. See the following example:

let res = await sellingPartner.callAPI({
  operation:'getMarketplaceParticipations',
  endpoint:'sellers'
});

Instead of using the endpoint property you may also prepend the endpoint to the operation as shorthand dot notation:

let res = await sellingPartner.callAPI({
  operation:'sellers.getMarketplaceParticipations'
});

Here are a few examples that use some more properties:

let res = await sellingPartner.callAPI({
  operation:'getOrderMetrics',
  endpoint:'sales',
  query:{
    marketplaceIds:['A1PA6795UKMFR9'],
    interval:'2020-10-01T00:00:00-07:00--2020-10-01T20:00:00-07:00',
    granularity:'Hour'
  }
});
let res = await sellingPartner.callAPI({
  operation:'catalogItems.getCatalogItem',
  path:{
    asin:'B084J4QQFT'
  },
  query:{
    MarketplaceId:'A1PA6795UKMFR9'
  },
  options:{
    version:'v0'
  }
});
let res = await sellingPartner.callAPI({
  operation:'createReport',
  endpoint:'reports',
  body:{
    reportType:'GET_FLAT_FILE_OPEN_LISTINGS_DATA',
    marketplaceIds:['A1PA6795UKMFR9']
  }
});
let res = await sellingPartner.callAPI({
  operation:'finances.listFinancialEvents',
  query:{
    PostedAfter:'2020-03-01T00:00:00-07:00',
    PostedBefore:'2020-03-02T00:00:00-07:00'
  },
  options:{
    raw_result:true
  }
});
try {
  let res = await sellingPartner.callAPI({
    operation: 'getCompetitivePricing',
    endpoint: 'productPricing',
    query: {
      Asins: ['B00Z7T970I','B01BHHE9VK'],
      ItemType: "Asin",
      MarketplaceId: 'A1PA6795UKMFR9'
    },
    options: {
      version: 'v0',
      raw_result: true,
      response_timeout: 10000,
      deadline_timeout: 30000
    }
  });
} catch(err) {
  if(err.code && err.code ==='API_RESPONSE_TIMEOUT') console.log('SP-API ERROR: response timeout: ' + err.timeout + 'ms exceeded.',err.message);
  if(err.code && err.code ==='API_DEADLINE_TIMEOUT') console.log('SP-API ERROR: deadline timeout: ' + err.timeout + 'ms exceeded.',err.message);
}

NOTE: As the original design of the client (< v0.4.0) didn't keep in mind the possibility of having the exact same operation name for multiple endpoints (i.e. getShipment, see Issue #33) and multiple versions of the same endpoint, we had to replace original operation-only based calls to the API with a new concept that includes endpoints and version-specific operation calls. This concept comes without any breaking changes, so you can still safely upgrade from any version below 0.4.0 to the latest version, but the use of .callAPI() without specifying an endpoint is considered deprecated, is discouraged and will trigger a console warning.

Endpoints

The exact endpoint's name of an operation will be the references name (see SP API Developer Guide) without API and all spaces removed and continued with a capital letter. So the Catalog Items API endpoint's name will be catalogItems, FBA Small and Light API will be fbaSmallAndLight, Sellers API will be sellers and so on. You can also retrieve the endpoint names and their operations and versions by calling sellingPartner.endpoints.

Versions

Every operation belongs to an endpoint that consists of one or more versions and each version consists of one or more operations. You will find a complete list of the endpoints with all versions and operations in the SP API Developer Guide. For a complete list of all currently by the client supported endpoints with versions and operations you can just call sellingPartner.endpoints.

Version specific operation implementations

The client uses the first (in fact the oldest) endpoint version if no version is provided since new versions of some operations are not backward compatible. So in order to prevent breaking changes we can't enable latest endpoint versions by default. I.e. the two different implementations of the getCatalogItem operation (see catalogItemsV0 vs. catalogItems_2020-12-01) expect different input parameters and return different results.

The implementation of the getCatalogItem operation in the v0 version expects an asin and a MarketplaceId as input:

let res = await sellingPartner.callAPI({
  operation:'getCatalogItem',
  endpoint:'catalogItems',
  query:{
    MarketplaceId:'A1PA6795UKMFR9'
  },
  path:{
    asin:'B084DWG2VQ'
  },
  options:{
    version:'v0'
  }
});

In contrast, the implementation of the getCatalogItem operation in the 2020-12-01 version expects an asin, a marketplaceIds array and an includedData array as input:

let res = await sellingPartner.callAPI({
  operation:'getCatalogItem',
  endpoint:'catalogItems',
  query:{
    marketplaceIds:['A1PA6795UKMFR9'],
    includedData:['identifiers', 'images', 'productTypes', 'salesRanks', 'summaries', 'variations']
  },
  path:{
    asin:'B084DWG2VQ'
  },
  options:{
    version:'2020-12-01'
  }
});

Trying to call the new 2020-12-01 version without explicitly setting it would result in an InvalidInput error as the required MarketplaceId parameter is missing.

Defining endpoints versions on class level

There are different ways of specifying the version to use for endpoints and their corresponding operations. You can specify the version directly inside the options object of the .callAPI() function as seen in the examples above. But you can also enable a newer version for all operations of an endpoint by using the endpoints_versions setting in the constructor config object. I.e. you can tell the class instance to use the new 2020-12-01 version for the catalogItems endpoint and thus enabling it for all operations of the endpoint throughout the class instance like this:

  let sellingPartner = new SellingPartnerAPI({
    region:'eu',
    refresh_token:'<REFRESH_TOKEN>',
    endpoints_versions:{
      'catalogItems':'2020-12-01'
    }
  });

By doing so you can skip setting the version inside the options object each time when you are using .callAPI() with the new version of the getCatalogItem operation.

Fallback

If trying to call an operation that is not part of the endpoint's version you specified, the client will automatically try to find the operation in an earlier endpoint's version and use that implementation if version_fallback is set to true (which is the default). I.e. the listCatalogCategories operation is not part of the new catalogItems endpoint version. So if the new version was set as in the example code above, the following call would still work, because it will automatically fallback to the operation's implementation in version v0:

let res = await sellingPartner.callAPI({
  operation:'listCatalogCategories',
  endpoint:'catalogItems',
  query:{
    MarketplaceId:'A1PA6795UKMFR9',
    ASIN:'B084DWG2VQ'
  }
});

Unsupported endpoints/versions/operations

The newest client version should have full support for all endpoints, versions and operations on release, however it might lack support for very recently added new endpoints, versions or operations. If you need an endpoint/version/operation that is not yet supported you can still call it by using the api_path parameter. I.e. if the new catalogItems version 2020-12-01 would not be supported yet we could still use the new implementation of the getCatalogItem operation by using the api_path and method properties:

let res = await sellingPartner.callAPI({
  api_path:'/catalog/2020-12-01/items/B084DWG2VQ',
  method:'GET',
  query:{
    marketplaceIds:['A1PA6795UKMFR9'],
    includedData:['identifiers', 'images', 'productTypes', 'salesRanks', 'summaries', 'variations']
  }
});

NOTE: If your api_path includes special characters that require encoding (i.e. an SKU that contains UTF-8 characters) you will have to encode these characters manually before passing your api_path to .callAPI().

Grantless operations

Some operations don't require an explicit authorization by a seller, see list of grantless operations. A grantless operation needs another access token than other operations and as such a grantless token is NOT the access_token you can provide in the constructor config object. However if the auto_request_tokens option is set to true the client should handle everything for you.

If you do the token request manually you need to create a grantless token by calling refreshAccessToken with the scope of the corresponding endpoint. Currently there are only two different scopes: sellingpartnerapi::migration for authorization endpoint and sellingpartnerapi::notifications for notifications endpoint.

If you don't need or have a refresh token (i.e. because you want to retrieve an SP API authorization code of an already via MWS authorized seller) you may use the client with the only_grantless_operations option set to true which allows you to create an instance of the client without a refresh_token.

To sum up, please see the following example that will request an auth code for an authorized MWS seller account.

First create a class instance that only allows to call grantless operations (no refresh_token included):

let sellingPartner = new SellingPartnerAPI({
  region:'eu',
  options:{
    auto_request_tokens:false,
    only_grantless_operations:true
  }
});

Then request a grantless token with the scope needed for the operation you want to call and refresh the role credentials:

await sellingPartner.refreshAccessToken('sellingpartnerapi::migration');
await sellingPartner.refreshRoleCredentials();

Finally call the grantless operation:

let res = await sellingPartner.callAPI({
  operation:'getAuthorizationCode',
  endpoint:'authorization',
  query:{
    sellingPartnerId:'<YOUR_CUSTOMERS_SELLER_ID>',
    developerId:'<YOUR_DEVELOPER_ID>',
    mwsAuthToken:'<YOUR_CUSTOMERS_MWS_TOKEN>'
  }
});

Restore rates

If you set the auto_request_throttled option in the class constructor config object to true (which is the default), the client will automatically retry the call if its throttled. It will either use the restore rate from the result header field x-amzn-ratelimit-limit if given (see Usage Plans and Rate Limits), or the value of restore_rate option in .callAPI() function if given, or otherwise use the default restore rate of the operation. For testing purposes you can also set debug_log to true, which will trigger a console log every time the client retries a call. If you set auto_request_throttled to false the client will throw a QuotaExceeded error when a request is throttled.

NOTE: If you are using the same operation with the same seller account across multiple class instances the restore rate logic might NOT work correct or, even worse, result in an infinite quota exceeded loop. So if you're planning to do that you should probably set auto_request_throttled to false, catch the QuotaExceeded errors and handle the restore rate logic on your own.

Download, decrypt and unzip reports

The .download() function takes the download details (url and encryption details) received from a getReportDocument operation as input, downloads the content, unzips it (if result is compressed), decrypts it and returns it.

Retrieve the download details from a getReportDocument operation:

let report_document = await sellingPartner.callAPI({
  operation:'getReportDocument',
  endpoint:'reports',
  path:{
    reportDocumentId:'<REPORT_DOCUMENT_ID>' // retrieve the reportDocumentId from a "getReport" operation (when processingStatus of report is "DONE")
  }
});

The structure of the returned report_document should look like this:

{
  reportDocumentId:'<REPORT_DOCUMENT_ID>',
  compressionAlgorithm:'GZIP', // Only included if report is compressed
  encryptionDetails:{ // Only included if old reports endpoint version used (2020-09-04)
    standard:'AES',
    initializationVector:'<INITIALIZATION_VECTOR>',
    key:'<KEY>'
  },
  url: '<REPORT_DOWNLOAD_URL>' // Expires after 5 minutes!
}

Call the .download() function to receive the content of the report. The default without any config options will download, decrypt and unzip the content and return it without reformatting or saving it to the disk:

let report = await sellingPartner.download(report_document);

You may also include an options object as a 2nd parameter to the .download() function, i.e. to enable a json result or to additionally save the report to a file. It supports four optional properties:

| Name | Type | Default | Description | | :------------------------ | :-----: | :-----: | -------------------------------------------------------------------------------------------------------------------------- | | jsonoptional | boolean | false | Whether or not the content should be transformed to json before returning it (from tab delimited flat-file or XML). | | unzipoptional | boolean | true | Whether or not the content should be unzipped before returning it. | | fileoptional | string | - | The absolute file path to save the report to.NOTE: Even when saved to disk the report content is still returned. | | charsetoptional | string | utf8 | The charset to use for decoding the content. If not defined, it uses per default the charset returned in content-type header or utf8 if no charset found in content-type header.NOTE: Is ignored when content is compressed and unzip is set to false. |

The following call will download the report, transform it to json and save it to disk:

let report = await sellingPartner.download(report_document, {
  json:true,
  file:'<ABSOLUTE_FILE_PATH>/report.json'
});

Some reports may have an encoding other than UTF-8 and require special decoding with a different charset, i.e. the GET_MERCHANT_LISTINGS_ALL_DATA report is encoded as cp1252 for eu region marketplaces. The right charset to use for decoding is taken from the return header content-type, but you may force the use of a specific charset for decoding by passing in the optional charset property:

let report = await sellingPartner.download(report_document, {
 charset:'cp1252' 
});

Encrypt and upload feeds

The .upload() function takes the feed upload details (url and encryption details) received from a createFeedDocument operation, the feed content and its content type to upload as input, encrypts the content and uploads it.

Start by creating a feed object with a contentType and the content either as a string or a file path to a document:

| Name | Type | Default | Description | | :---------------------------- | :----: | :-----: | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | contentoptional | string | - | The content to upload as a string.Required if file is not provided. | | fileoptional | string | - | The absolute file path to the feed content document to upload.Required if content is not provided. | | contentTyperequired | string | - | The contentType of the content to upload.Should be one of text/xml or text/tab-separated-values and the charset of the content, i.e. text/xml; charset=utf-8. |

This will create an inventory feed (POST_INVENTORY_AVAILABILITY_DATA) that will update the quantity of a given SKU to 10:

let feed = {
  content:`<?xml version="1.0" encoding="utf-8"?>
    <AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">
      <Header>
        <DocumentVersion>1.02</DocumentVersion>
        <MerchantIdentifier>YOUR_MERCHANT_IDENTIFIER</MerchantIdentifier>
      </Header>
      <MessageType>Inventory</MessageType>
      <Message>
        <MessageID>1</MessageID>
        <Inventory>
          <SKU>YOUR_SKU</SKU>
          <Quantity>10</Quantity>
        </Inventory>
      </Message>
    </AmazonEnvelope>`,
  contentType:'text/xml; charset=utf-8'
};

Before you can upload the feed you need to retrieve the feed upload details from a createFeedDocument operation:

let feed_upload_details = await sellingPartner.callAPI({
  operation:'createFeedDocument',
  endpoint:'feeds',
  body:{
    contentType:feed.contentType
  }
});

Call the .upload() function to encrypt and upload the content of the feed:

let res = await sellingPartner.upload(feed_upload_details, feed);

After uploading the feed you have to trigger the processing of the feed by calling the createFeed operation with the necessary params (marketplaceIds, feedType and inputFeedDocumentId):

let feed_creation_infos = await sellingPartner.callAPI({
  operation:'createFeed',
  endpoint:'feeds',
  body:{
    marketplaceIds:['A1PA6795UKMFR9'],
    feedType:'POST_INVENTORY_AVAILABILITY_DATA',
    inputFeedDocumentId:feed_upload_details.feedDocumentId // retrieve the feedDocumentId from the "createFeedDocument" operation
  }
});

NOTE: Although uploading and creating the feed was successful it doesn't mean that the processing of the feed itself was also successful. You can check the result of the feed once it has been processed by downloading the processing result with the .download() function quite similar as how to download reports. Use the feedId returned by the createFeed operation and call the getFeed operation, which will include a resultFeedDocumentId if feed processing is already done. The resultFeedDocumentId can be used with a getFeedDocument operation that will return the feed download details needed for the feed result download.

TypeScript Support

All TypeScript related information can be found in lib/typings. Currently types are not yet defined for all operations and/or params, so feel free to add new types following the readme. You are also welcome to create a pull request.

Sandbox mode

You can easily enable sandbox mode by setting use_sandbox in the constructor config options to true. General information on sandbox setup and behaviour can be found in the corresponding section in the Selling Partner API Developer Guide.

When using the sandbox you have to make sure to use the correct request parameters for the operation you want to test. You can find these inside the api models definitions in the docs by searching the corresponding json file for x-amzn-api-sandbox.

For example, this will test the listCatalogItems operation in sandbox mode:

let res = await sellingPartner.callAPI({
  operation:'listCatalogItems',
  endpoint:'catalogItems',
  query:{
    MarketplaceId:'TEST_CASE_200',
    SellerSKU:'SKU_200'
  }
});

Known Issues

Since the Selling Partner API is still pretty new, not all API paths and endpoints have been tested for full functionality. If you find any calls not working please open up a new issue.

Some operations don't respect the correct restore rate yet, meaning they restore a lot slower than the default restore rate.

There is an issue with values of arrays as part of the query, when a value contains a ,. Due to Amazon expecting array values in query separated by , it will wrongfully split up values containing a , into two separate values. This is already a known issue communicated to Amazon.

Seller Support

If you are selling on the european market we might be able to support you with everything else that can't be done with the API, i.e. a detailed sales dashboard, review management, product sourcing or sales and revenue estimations for products. Feel free to visit us at https://amz.tools.