ScaleLeap / selling-partner-api-sdk

A fully typed TypeScript and Node.js SDK library for Amazon Selling Partner API

Home Page:https://npm.im/@scaleleap/selling-partner-api-sdk

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

More detailed example for creating client

kneumei opened this issue · comments

I'm coming from https://github.com/amz-tools/amazon-sp-api to this project for the type script in this project.

But one thing I'm having trouble with is figuring out how to create the client. With amazon-sp-api, the documentation is very clear on how to create an access token. In fact, the client in that project, the client manages the access token itself, so I don't have to worry about expiration.

With this project it's less clear to me from the docs how to create the access token...some additional documentation would be very helpful.

commented

Hi Kyle,

Thank you for reaching out.

There's an example of how to create an instance of a client class, which takes acesssToken as a prop.

But how you obtain this access token is up to you.

There are just too many options and permutations to create a neat abstraction at this point.

To get the token you need to follow the guide outlined in the SP API docs, or self authorize if it's an internal app.

Is this more clear?

Were you expecting this package to handle the OAuth flows as well?

Cheers!

Thank you for your response.

I'm using the Self Authorization flow and found the amazon-sp-api easy to work with. It handles the request to get the access token and calling sts (to exchange a user's accessKeyId and secretAccessKey for an STS token) for me.

This seems like something this library could help me do as well in some way...or perhaps give me starter code in an example, I was able to get things to work by looking at the integration tests

        let response = await axios.post("https://api.amazon.com/auth/o2/token", stringify({
            grant_type: "refresh_token",
            refresh_token: refresh_token,
            client_id: client_id,
            client_secret: client_secret
        }), {
            headers: {
                "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
            }
        });
        
       const sts = new STSClient({
            region: "us-east-1",
            credentials: {
                accessKeyId: accessKeyId,
                secretAccessKey: secretAccessKey,
            },
        });

        const { Credentials } = await sts.send(
            new AssumeRoleCommand({
              RoleArn: roleArn,
              RoleSessionName: 'selling-partner-api-axios',
            }),
          )

        let client = new FulfillmentOutboundApiClient({
            accessToken: response.data.access_token,
            credentials: {
                accessKeyId: accessKeyId ,
                secretAccessKey: secretAccessKey
            },
            basePath: 'https://sellingpartnerapi-na.amazon.com',
            region: 'us-east-1'
        });
        
        const result = await client.getFulfillmentOrder({
            sellerFulfillmentOrderId: "MY-ORDER"
        })
commented

Hi Kyle,

I've added more docs. Please take a look and let me know if you think this is more helpful.

It handles the request to get the access token and calling sts (to exchange a user's accessKeyId and secretAccessKey for an STS token) for me.

Yes, our package can do this too. I've added an example for this.

We are trying to keep it flexible, as there are many potential scenarios, and making the STS call inside the package is not always desirable, as it can be an overhead that is not necessary.

For example, our Lambda that makes the calls to the SP API, launches with the already assumed role and correct credentials (via standard AWS process), ready to make calls to the API. We do not need to make an additional STS call.

commented

Could you please show any example on how to obtain the accessToken? I'm currently stuck on this.

commented

Could you please show any example on how to obtain the accessToken? I'm currently stuck on this.

That's outside the scope of this package. It assumes you already have a token.

Could you please show any example on how to obtain the accessToken? I'm currently stuck on this.

@doverradio, if you have a refresh_token, check kneumei's comment or the authentication functions under the test directory.

Could you please show any example on how to obtain the accessToken? I'm currently stuck on this.

Just passing along so anyone new can use it:

import { SellersApiClient } from '@scaleleap/selling-partner-api-sdk'
import axios from 'axios'

const credentials = {
  accessToken: 'Atza|..',
  refreshToken: 'Atzr|...',
  roleArn: 'arn:aws..',
  appClientId: 'amzn1...',
  appClientSecret: 'amzn1..',
  accessKeyId: 'XX..',
  secretAccessKey: 'XX..',
  region: 'na',
  marketplaceId: 'ATVPDKIKX0DER',
  sellerId: 'XX'
}

const response = await axios.post('https://api.amazon.com/auth/o2/token', new URLSearchParams({
  grant_type: 'refresh_token',
  refresh_token: credentials.refreshToken,
  client_id: credentials.appClientId,
  client_secret: credentials.appClientSecret
})
  .toString(), {
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
  }
})

const accessToken = response.data.access_token

const client = new SellersApiClient({
  accessToken,

  // 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',

  roleArn: credentials.roleArn,

  credentials: {
    accessKeyId: credentials.accessKeyId,
    secretAccessKey: credentials.secretAccessKey,
    sessionToken: credentials.sessionToken
  }
})

const marketplaceParticipations = await client.getMarketplaceParticipations()

console.log(marketplaceParticipations.data.payload)