Hey there, fellow developer! Ready to dive into the world of Amazon Seller Central API integration? You're in the right place. We'll be using the awesome amazon-sp-api
package to make our lives easier. Let's get cracking!
Before we jump in, make sure you've got:
Got all that? Great! Let's move on.
First things first, let's get our project set up:
mkdir amazon-sp-api-integration cd amazon-sp-api-integration npm init -y npm install amazon-sp-api
Easy peasy, right?
Now, let's import the SellingPartnerAPI
and set up our configuration:
const SellingPartnerAPI = require('amazon-sp-api'); const spApi = new SellingPartnerAPI({ region: 'NA', // or EU, FE refresh_token: 'your-refresh-token', credentials: { SELLING_PARTNER_APP_CLIENT_ID: 'your-app-client-id', SELLING_PARTNER_APP_CLIENT_SECRET: 'your-app-client-secret', AWS_ACCESS_KEY_ID: 'your-aws-access-key', AWS_SECRET_ACCESS_KEY: 'your-aws-secret-key', }, });
The amazon-sp-api
package handles token refresh for us, so we don't need to worry about it. Neat, huh?
Let's fetch some order data:
async function getOrders() { try { const res = await spApi.callAPI({ operation: 'getOrders', endpoint: 'orders', query: { MarketplaceIds: ['ATVPDKIKX0DER'], // US marketplace CreatedAfter: new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString(), }, }); console.log(res); } catch (error) { console.error('Error fetching orders:', error); } } getOrders();
The package handles rate limiting for us (thanks, amazon-sp-api
!). For pagination, we can use the nextToken
:
async function getAllOrders() { let allOrders = []; let nextToken = null; do { const res = await spApi.callAPI({ operation: 'getOrders', endpoint: 'orders', query: { MarketplaceIds: ['ATVPDKIKX0DER'], CreatedAfter: new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString(), NextToken: nextToken, }, }); allOrders = allOrders.concat(res.Orders); nextToken = res.NextToken; } while (nextToken); return allOrders; }
Let's wrap our API calls in a try-catch block and add some logging:
const winston = require('winston'); const logger = winston.createLogger({ level: 'info', format: winston.format.json(), transports: [ new winston.transports.File({ filename: 'error.log', level: 'error' }), new winston.transports.File({ filename: 'combined.log' }), ], }); async function safeApiCall(operation, params) { try { return await spApi.callAPI(params); } catch (error) { logger.error(`Error in ${operation}:`, error); throw error; } }
For unit testing, you can use Jest with mocked API responses. For integration testing, create a separate test environment with its own API credentials.
const [orders, inventory] = await Promise.all([ safeApiCall('getOrders', { /* params */ }), safeApiCall('getInventory', { /* params */ }), ]);
And there you have it! You've just built an Amazon Seller Central API integration using the amazon-sp-api
package. Pretty cool, right? Remember to check out the official documentation for more advanced features and updates.
Now go forth and build amazing things with your new API superpowers! 🚀