Tutorial: Automate your SP-API calls using a prebuilt Python SDK

Automate your SP-API calls using a prebuilt Python SDK.

This tutorial shows you how to install and integrate a prebuilt Python SDK. You learn the prerequisites required to install the prebuilt Python SDK and also view an example using the Selling Partner API for sellers.

Prerequisites

To complete this tutorial, you need:

Step 1. Add the Python SDK dependency to your application

The Python SDK is published on PyPI. You can add the Python SDK dependency to your application using your project's requirements.txt file or Poetry. Download the latest Python SDK from the Selling Partner API SDK releases GitHub repository or PyPi.

requirements.txt

Add the following code as a dependency in the requirements.txt file:

amzn-sp-api >= 1.0.0

Poetry

Run the following command to add the SDK dependency in your Poetry project:

poetry add amzn-sp-api

Step 2. Connect the Python SDK to SP-API

To connect the Python SDK to the SP-API, you must:

  1. Configure your Login with Amazon credentials
  2. Create an instance for a specific API client
  3. Call an operation

For an example, refer to the following sample code:

# LWA credentials configuration
    config = SPAPIConfig(
        client_id="",
        client_secret="",
        refresh_token="",
        region="NA",
        scope = None
    )

# Create the API client with configuration
    client = SPAPIClient(config)
    sellers_api = SellersApi(client.api_client)

# Call the API operation
    response = None
    try:
        response = sellers_api.get_marketplace_participations()
    except ApiException as e:
        print(f"API Exception occurred: {str(e)}")
    if response is not None:
        print("Sellers API Response:")
        get_marketplace_participations_response = GetMarketplaceParticipationsResponse(response.payload)
        for marketplaceParticipation in get_marketplace_participations_response.payload:
            print(marketplaceParticipation.marketplace.id)

Report issues

You can report issues on GitHub.


OSZAR »