Tutorial: Automate your SP-API Calls using a prebuilt JavaScript SDK

Automate your SP-API calls using a prebuilt JavaScript SDK.

This tutorial shows you how to install and integrate a prebuilt JavaScript SDK. You learn the prerequisites required to install the prebuilt JavaScript SDK and also view an example using the Selling Partner API for sellers.

Prerequisites

To complete this tutorial, you need:

  • Integrated development environment (IDE) software
  • Node v14 or higher

Before your application can connect to the Selling Partner API, you must register it, and it must be authorized by a selling partner. Follow the steps to register as a developer, register your application, and view information about your application. Then, return to this tutorial.

Step 1. Add the JavaScript SDK dependency to your application

To add the JavaScript SDK dependency to your application, use npm or yarn. Download the latest JavaScript SDK from the Selling Partner API SDK releases GitHub repository.

npm

To create a package with npm, enter the following command:

npm install @amazon-sp-api-release/amazon-sp-api-sdk-js

yarn

To create a package with yarn, enter the following command:

yarn add @amazon-sp-api-release/amazon-sp-api-sdk-js

Add package dependency

Add the following code as a dependency in the package.json file:

"@amazon-sp-api-release/amazon-sp-api-sdk-js": "^1.0.0"

Step 2. Connect the JavaScript SDK to SP-API

To connect the JavaScript SDK to the SP-API, you must:

  1. Configure your Login with Amazon credentials.
  2. Use the built-in ApiClient function to enable auto accessToken retrieval or use the LwaAuthClient to retrieve the accessToken.
  3. Create an instance for a specific API and API client and apply the accessToken.
  4. Call an operation.

For an example, refer to the following sample code:

import {
  SellersSpApi
} from '@amazon-sp-api-release/amazon-sp-api-sdk-js';

async function getMarketplaceParticipations() {
  try {
      //Configure the Sellers ApiClient
      const sellersApiClient = new SellersSpApi.ApiClient(AppConfig.spApiNAEndpoint);
      sellersApiClient.enableAutoRetrievalAccessToken('<YOUR_CLIENT_ID>','<YOUR_CLIENT_SECRET>', '<YOUR_REFRESH_TOKEN>' null);
      const sellersApi = new SellersSpApi.SellersApi(sellersApiClient);
      
      //Call the GetMarketplaceParticipations operation
      const participations = await sellersApi.getMarketplaceParticipations();
      console.log(
        JSON.stringify(participations, null, ' ') + 
          '\n**********************************'
      )
  } catch (error) {
      console.error('Exception when calling getMarketplaceParticipations API', error.message);
  }
}

getMarketplaceParticipations();

Alternatively, you can modify the index.js and app.config.mjs files from @amazon-sp-api-release/amazon-sp-api-sdk-js/sample-node-app. The app includes multiple API call samples with different ways to retrieve tokens. It also shows you how to set up a rate limiter and add retry logic when you make API calls.

Report issues

You can report issues on GitHub.


OSZAR »