forked from amzn/selling-partner-api-models
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Umeda
committed
Feb 20, 2024
1 parent
b570970
commit 28f5a33
Showing
13 changed files
with
5,306 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
# Selling Partner API JavaScript client library for Node.js (v18 or later) | ||
|
||
## About this library | ||
This library is to help SP-API developers to create an app in JavaScript on Node.js easier than calling HTTP endpoints directly. This library comes with the following features. | ||
1. Login with Amazon (LWA) helper that does OAuth token refresh flow. | ||
2. This library takes care of HTTP communication with SP-API endpoints with a help of [superagent](https://www.npmjs.com/package/superagent), so you can call SP-API by just calling a right method of library that corresponds to SP-API operation. | ||
3. Calling SP-API requires non-standard `x-amz-access-token` HTTP request header in request. The SDK generated by this library supports to include this header with token value you specify. | ||
4. SP-API operaitons to handle restricted data are categorized as ["Restricted Operations"](https://developer-docs.amazon.com/sp-api/lang-ja_JP/docs/tokens-api-use-case-guide#restricted-operations), which requires ["Restricted Data Token" (RDT)](https://developer-docs.amazon.com/sp-api/lang-ja_JP/docs/authorization-with-the-restricted-data-token) instead of access token for tighter security. Calling restricted operation involves two seprate steps, one of wihch is calling Tokens API to retrieve RDT and the other of which is calling protected operation with RDT. This library helps to compbine these two steps into single library call. | ||
|
||
## Installation and Generating SDK | ||
Please note this library doesn't include SDK. You need to generate SDK with the template and script files included in this library. | ||
### Prerequisites for SDK Generation | ||
* Java version 7 or higher | ||
* swagger-codegen-cli (swagger-codegen-cli-2.4.29) is recommended. | ||
* Download swagger-codegen-cli JAR file and store it. You will need its path later when generating SDK. | ||
* Node.js v18 or higher. | ||
|
||
__CAUTION__: Please be aware that there are two major known issues with the latest JavaScript client library. | ||
1. If you use swagger-codegen-cli newer than 2.4.29 (such as 2.4.30 or 3.x), there is a known compatibility issue with SP-API models that makes generating SDK fail. We recommend that you use swagger-codegen-cli-2.4.29 specifically. | ||
2. Swagger codegen tool fails to generate SDK for "Merchant Fulfillment V0 API." For workaround, you need to modify a part of the API model file "merchantFulfillmentV0.json" | ||
|
||
### Download swagger-codegen-cli JAR file | ||
We use `swagger-codegen-cli` executable JAR file. You can download it by the following command. | ||
```bash | ||
$ wget https://repo1.maven.org/maven2/io/swagger/swagger-codegen-cli/2.4.29/swagger-codegen-cli-2.4.29.jar | ||
``` | ||
### Installing dependencies | ||
Change the directory to `<package root>/src`. Run the following command to install dependencies in Node.js environment. | ||
```bash | ||
$ npm install | ||
``` | ||
|
||
### Download API models and Generate SDK | ||
Go to `<package root>/src` directory which is under the package root. In this directory you should be able to find `generate-js-sdk.sh` shell script file. | ||
Run this script as the command line below. | ||
```bash | ||
$ ./generate-js-sdk.sh -j <your path to swagger-codegen-cli-2.4.X.jar> | ||
``` | ||
|
||
You will find `models` directory and `sdk` directory under the package root.<br> | ||
* `models`: directory contains API models cloned from SP-API GitHub. | ||
* `sdk`: directory contains generated JavaScript SDK. | ||
|
||
### Modify Merchant Fulfillment API model file to avoid error during SDK generation | ||
If you want to call Merchant Fulfillment API with the generated SDK, you need to follow this instruction. There is an fatal known issue in generating Merchant Fulfillment API library. If downloading API models is successful, you should be able to find `merchantFulfillmentV0.json` file In `<package root>/models/merchant-fulfillment-api-model` directory. Open this file with an editor and find the following part. | ||
``` | ||
"AvailableFormatOptionsForLabel": { | ||
"$ref": "#/definitions/AvailableFormatOptionsForLabelList" | ||
}, | ||
``` | ||
Since this part causes a fatal error in SDK generation for JavaScript, please replace this part with the following snipet. | ||
``` | ||
"AvailableFormatOptionsForLabel": { | ||
"type": "array", | ||
"description": "The available label formats.", | ||
"items": { | ||
"$ref": "#/definitions/LabelFormatOption" | ||
} | ||
}, | ||
``` | ||
After modification, please run the shell script (`generate-js-sdk.sh`) again but you should type 'n' to the prompt that asks whether to download models again. | ||
```bash | ||
$ ./generate-js-sdk.sh -j ~/devbin/swagger-codegen-cli-2.4.29.jar | ||
Found <package root>/models already exists. Would you like to delete all the files under '<package root>/models' and clone again? [y/n]: n # answer 'n' otherwise modification will be overwritten. | ||
|
||
``` | ||
With the correct modification in `merchantFulfillmentV0.json`, you should be able to find generated SDK for Merchant Fulfillment API. | ||
|
||
### How to run tests | ||
This library contains a sample programs in `sample_node_app` directory under the packagte root.<br>In order to run the program, you need to have LWA credential information saved in the file and name it `app.config.mjs` and put it `<package root>/src` directory. Because __client secret__ and __refresh token__ shouldn't be exposed, you must make sure that you don't commit this file to the repository. | ||
```javascript | ||
export const AppConfig = { | ||
lwaClientId: "< LWA client ID >", | ||
lwaClientSecret: "< LWA client secret >", | ||
lwaRefreshToken: "< LWA refresh token >", | ||
endpoint: "https://sandbox.sellingpartnerapi-na.amazon.com", | ||
} | ||
``` | ||
After you save `app.config.mjs` file, you can run the sample program`. | ||
```bash | ||
$ npm run test | ||
``` | ||
--- | ||
## How to use SDK | ||
### Calling SP-API operation with LWA credentials | ||
```javascript | ||
import { SellersApi, ApiClient as SellersApiClient } from '../../sdk/src/sellers/index.js'; | ||
|
||
const sellerApiClient = new SellersApiClient("https://sellingpartnerapi-na.amazon.com"); | ||
const sellerApi = new SellersApi(sellerApiClient); | ||
sellerApiClient.enableAutoRetrievalAccessToken("<client ID>", "<client secret>", "<refresh token>"); | ||
const participations = await sellerApi.getMarketplaceParticipations(); | ||
``` | ||
### Calling RDT-required SP-API operation with LWA credentials | ||
```javascript | ||
import { OrdersV0Api, ApiClient as OrdersApiClient } from '../../sdk/src/ordersV0/index.js'; | ||
|
||
const ordersApiClient = new OrdersApiClient("https://sellingpartnerapi-fe.amazon.com"); | ||
ordersApiClient.enableAutoRetrievalRestrictedDataToken("<client ID>", | ||
"<client secret>", "<refresh token>", ["buyerInfo", "shippingAddress"]); | ||
const ordersApi = new OrdersV0Api(ordersApiClient); | ||
const order = await ordersApi.getOrder("<order ID to retrieve>"); | ||
``` | ||
### Calling SP-API with access token | ||
In case you manage LWA token refresh flow, you can explicitly use the access token you got yourself for your SP-API call as follows. | ||
```javascript | ||
import { SellersApi, ApiClient as SellersApiClient } from '../../sdk/src/sellers/index.js'; | ||
|
||
const sellerApiClient = new SellersApiClient("https://sellingpartnerapi-fe.amazon.com"); | ||
const sellerApi = new SellersApi(sellerApiClient); | ||
sellerApiClient.applyXAmzAccessTokenToRequest("<access token you retrieve yourself>"); | ||
const participations = await sellerApi.getMarketplaceParticipations(); | ||
``` | ||
### LWA Token refresh helper | ||
The following code shows how to use `LwaAuthClient` to execute token refresh flow. | ||
```javascript | ||
import { LwaAuthClient } from "<path to helper/LwaAuthClient.mjs>"; | ||
|
||
const lwaClient = new LwaAuthClient("<client ID>", "<client secret>", "<refresh token>"); | ||
const accessToken = await lwaClient.getAccessToken(); | ||
``` |
Oops, something went wrong.