Hey there, fellow developer! Ready to dive into the world of Amazon Seller Central API integration? You're in for a treat. We'll be using the awesome python-amazon-sp-api
package to make our lives easier. Buckle up, and let's get coding!
Before we jump in, make sure you've got:
First things first, let's get our tools ready:
pip install python-amazon-sp-api
This package will handle most of the heavy lifting for us. Neat, right?
Now, let's set up our credentials and initialize the API client:
from sp_api.api import Orders from sp_api.base import Marketplaces credentials = dict( refresh_token='your_refresh_token', lwa_app_id='your_lwa_app_id', lwa_client_secret='your_lwa_client_secret', aws_access_key='your_aws_access_key', aws_secret_key='your_aws_secret_key', role_arn='your_role_arn' ) client = Orders(credentials=credentials, marketplace=Marketplaces.US)
Let's get our hands dirty with some basic operations:
response = client.get_orders(CreatedAfter='2023-01-01') print(response.payload)
from sp_api.api import Products products_client = Products(credentials=credentials, marketplace=Marketplaces.US) response = products_client.get_product_pricing_for_skus(skus=['your_sku']) print(response.payload)
from sp_api.api import Inventory inventory_client = Inventory(credentials=credentials, marketplace=Marketplaces.US) response = inventory_client.update_inventory_item( seller_sku='your_sku', body={ 'inventory': { 'sellable': 100 } } ) print(response.payload)
The package handles rate limits automatically, but you can customize:
from sp_api.base import ApiResponse, Marketplaces, SellingApiException from sp_api.base.client import SellingApiClientConfig client = Orders( credentials=credentials, marketplace=Marketplaces.US, config=SellingApiClientConfig( rate_limit=1, # requests per second retry_count=3 ) )
try: response = client.get_orders(CreatedAfter='2023-01-01') except SellingApiException as e: print(f"Error: {e}")
response = client.get_orders(CreatedAfter='2023-01-01') while response.next_token: response = client.get_orders(CreatedAfter='2023-01-01', NextToken=response.next_token) # Process orders
Use the sandbox environment for testing:
from sp_api.base import Marketplaces client = Orders(credentials=credentials, marketplace=Marketplaces.US, sandbox=True)
For logging:
import logging logging.basicConfig(level=logging.DEBUG)
And there you have it! You're now equipped to build a robust Amazon Seller Central API integration. Remember, the key to mastering this is practice and exploration. Don't be afraid to dive into the python-amazon-sp-api
documentation for more advanced features.
Happy coding, and may your sales skyrocket! 🚀