- A fully typed TypeScript and Node.js SDK package for Amazon Selling Partner API
- Uses models from API model's repo to generate classes automatically
- Picks up changes and releases daily when/if models have drifted
- Based on Axios and uses aws4-axios interceptor to automatically sign the requests
- Can optionally assume roles via STS, and refresh STS credentials on schedule
npm i -s @scaleleap/selling-partner-api-sdk
A few things to get started:
- Registering as a developer
- Registering your Hybrid Selling Partner API applications
- Authorizing Selling Partner API applications
Note that it is outside the responsibility of this package to handle the authorization process.
This package assumes you have already acquired the access and refresh tokens either by going through the OAuth flow or by using a self-authorized set of credentials.
This method is applicable if you want to assume the Selling Partner API role yourself, or you are using a static set of user credentials (not recommended).
import { SellersApiClient } from '@scaleleap/selling-partner-api-sdk'
const stsClient = new STSClient({
// Static set of credentials that have the permission to assume the role above
credentials: {
accessKeyId: 'AKIAIOSFODNN7EXAMPLE',
secretAccessKey: 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYzEXAMPLEKEY',
},
})
const { Credentials } = await stsClient.send(
new AssumeRoleCommand({
// This is the role you have set in your Selling Partner API application
RoleArn: 'arn:aws:iam::123456789012:role/your-SP-API-role-name',
RoleSessionName: 'selling-partner-api-axios',
}),
)
const client = new SellersApiClient({
accessToken: 'Atza|...',
// Or use `amazonMarketplaces.CA.sellingPartner.region.endpoint`
// from `@scaleleap/amazon-marketplaces` package
basePath: 'https://sellingpartnerapi-na.amazon.com',
// Or use `amazonMarketplaces.CA.sellingPartner.region.awsRegion`
// from `@scaleleap/amazon-marketplaces` package
region: 'us-east-1',
credentials: {
accessKeyId: Credentials?.AccessKeyId || '',
secretAccessKey: Credentials?.SecretAccessKey || '',
sessionToken: Credentials?.SessionToken || '',
}
})
const marketplaceParticipations = await client.getMarketplaceParticipations()
This package uses aws4-axios under the hood, which has the capability to make the STS call and get the credentials for you, and refresh the temporary AWS credentials session.
import { SellersApiClient } from '@scaleleap/selling-partner-api-sdk'
const client = new SellersApiClient({
accessToken: 'Atza|...',
// Or use `amazonMarketplaces.CA.sellingPartner.region.endpoint`
// from `@scaleleap/amazon-marketplaces` package
basePath: 'https://sellingpartnerapi-na.amazon.com',
// Or use `amazonMarketplaces.CA.sellingPartner.region.awsRegion`
// from `@scaleleap/amazon-marketplaces` package
region: 'us-east-1',
// This is the role you have set in your Selling Partner API application
roleArn: 'arn:aws:iam::123456789012:role/your-SP-API-role-name',
// Static set of credentials that have the permission to assume the role above
credentials: {
accessKeyId: 'AKIAIOSFODNN7EXAMPLE',
secretAccessKey: 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYzEXAMPLEKEY',
},
})
const marketplaceParticipations = await client.getMarketplaceParticipations()
See the full list of exported classes and types:
src/api-models/index.ts
.
See @scaleleap/amazon-marketplaces docs for a database of constants about Amazon Marketplaces.
Using the Amazon Reports and Amazon Feeds sections of the API can be a bit tedious. This library adds some useful helpers for feeds and reports.
submitFeedHelper will submit an XML feed and wait for the results. It will lightly parse the XML to let you know if there are errors or not. This way you can know whether you have to parse the XML or not.
const { hasErrors, xmlResponse} = await FeedHelpers.SubmitFeed(feedsClient, 'SELLING_PARTNER_REPORT_TYPE', xmlString);
getReportHelper will request a new report, wait on an interval until the report is finished, and then parse the tsv results into JSON. Parsing is optional. See Amazon's ReportType Values
await ReportHelpers.GetReport(reportsClient, 'SELLING_PARTNER_REPORT_TYPE', {
startDate: '2022-03-07',
endDate: '2022-03-08',
});
getLatestReportHelper will fetch the latest DONE version of a report and parse the tsv results into JSON. Parsing is optional.
await ReportHelpers.GetLatestReport(reportsClient, 'SELLING_PARTNER_REPORT_TYPE');
You can use the tsv to json parser alone if you'd like to
import parseAmazonReport from '@whitebox-co/selling-partner-api-sdk/lib/helpers';
This repository uses Conventional Commit style commit messages.
- Roman Filippov (Scale Leap)
- Toan Nguyen (nguyentoanit)
This project is licensed under the MIT License.