Amazon Selling Partner API (SP-API) Python Client

2.1.8 · active · verified Sun Apr 12

python-amazon-sp-api is a comprehensive Python wrapper for the Amazon Selling Partner API (SP-API). It provides a convenient way to interact with various Amazon SP-API endpoints for tasks such as order management, inventory, reports, and more. The library is actively maintained, with frequent updates to support new API versions and features. The current stable version is 2.1.8.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize an `Orders` client, which is a common use case for the Amazon SP-API. It highlights the required authentication parameters (LWA credentials, AWS IAM role/keys) and shows how to make a basic API call to retrieve orders. Ensure your environment variables are correctly set for authentication.

import os
from sp_api.api import Orders
from sp_api.base import Marketplaces

# It is highly recommended to set these as environment variables
# for security and ease of management.
REFRESH_TOKEN = os.environ.get('SP_API_REFRESH_TOKEN', 'YOUR_REFRESH_TOKEN_HERE')
LWA_APP_ID = os.environ.get('SP_API_LWA_APP_ID', 'YOUR_LWA_APP_ID_HERE')
LWA_CLIENT_SECRET = os.environ.get('SP_API_LWA_CLIENT_SECRET', 'YOUR_LWA_CLIENT_SECRET_HERE')
AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID', 'YOUR_AWS_ACCESS_KEY_ID_HERE')
AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY', 'YOUR_AWS_SECRET_ACCESS_KEY_HERE')
IAM_ROLE_ARN = os.environ.get('SP_API_IAM_ROLE_ARN', 'YOUR_IAM_ROLE_ARN_HERE') # If using IAM

try:
    client = Orders(
        refresh_token=REFRESH_TOKEN,
        lwa_app_id=LWA_APP_ID,
        lwa_client_secret=LWA_CLIENT_SECRET,
        aws_access_key_id=AWS_ACCESS_KEY_ID,
        aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
        role_arn=IAM_ROLE_ARN, # Use role_arn if using IAM for AWS credentials
        marketplace=Marketplaces.US # Or specify your target marketplace
    )

    # Example: Get orders created after a specific date
    response = client.get_orders(CreatedAfter='2023-01-01T00:00:00Z', MaxResultsPerPage=10)
    print("Successfully retrieved orders:")
    print(response.payload)

except Exception as e:
    print(f"An error occurred: {e}")
    # Specific error handling for SP-API e.g., AuthorizationError, RateLimitError

view raw JSON →