Skip to main content

Python async wrapper for the Amazon Selling-Partner API

Project description

CodeFactor Downloads Downloads Downloads

PYTHON-AMAZON-SP-API-ASYNC

Amazon Selling-Partner API (Async Fork)

Note: This is an async fork modified from python-amazon-sp-api.

A wrapper to access Amazon's Selling Partner API with an easy-to-use async/await interface.

⚠️ Warning

⚠️ Important Notice:

  • This project has not been rigorously tested and is provided as-is. Use at your own risk.
  • If the original repository (python-amazon-sp-api) implements async support in the future, this fork will become obsolete and should no longer be used.
  • Please monitor the original repository for official async support and migrate to it when available.

🙏 Acknowledgments

This project is based on python-amazon-sp-api with modifications. Special thanks to:

  • Original Author: @saleweaver (Michael Primke)
  • Original Repository: python-amazon-sp-api
  • Original Contributors: Thanks to all developers who contributed to the original project

This fork version has been refactored for async/await support based on the original project, but the core architecture and API design philosophy are derived from the original project. If you find this project useful, please consider supporting the original project as well.

🔄 Key Changes

Main improvements in this fork compared to the original version:

  • ✨ Full Async Support: All API calls use async/await pattern, implemented with httpx for better concurrency performance
  • 📦 Modern Build System: Migrated to pyproject.toml and uv package manager for simplified dependency management
  • 🐍 Python 3.13+: Requires Python 3.13 or higher to leverage the latest language features
  • ⚡ Performance Optimization: Async I/O support, ideal for high-concurrency scenarios

📋 Differences from Original

Feature Original This Fork
HTTP Client requests (sync) httpx (async)
API Call Pattern Synchronous async/await
Package Name python-amazon-sp-api python-amazon-sp-api-async
Python Version >= 3.9 >= 3.13
Package Management setup.py / requirements.txt pyproject.toml / uv

🚀 New Features in This Fork

This section lists additional features and improvements added specifically in this fork:

💾 Flexible Cache Manager

A flexible cache manager system has been added to improve token caching efficiency:

  • Dual Cache Modes: Supports both memory cache (using cachetools) and disk cache (using diskcache as optional dependency)
  • Smart Fallback: Automatically falls back to memory cache if diskcache is not installed
  • Configurable: Control cache behavior via environment variables or function parameters
  • Singleton Pattern: Reuses cache instances with the same configuration

Installation:

# Optional: Install diskcache for persistent disk caching
uv add diskcache

Configuration:

  • SP_API_CACHE_TYPE: Cache type (memory or disk, default: disk if diskcache installed, else memory)
  • SP_API_CACHE_DIR: Disk cache directory (default: ~/.sp_api_cache)
  • SP_API_AUTH_CACHE_SIZE: Cache size (default: 100)
  • SP_API_AUTH_CACHE_TTL: Cache TTL in seconds (default: 3200)

Usage: The cache manager is automatically used by AccessTokenClient for token caching. You can also use it directly:

from sp_api_async.auth.cache_manager import get_cache_manager

# Auto-select cache type based on available dependencies
cache = get_cache_manager(cache_name='my_cache')

# Explicitly specify cache type
cache = get_cache_manager(cache_type='memory', cache_name='token_cache')
cache = get_cache_manager(cache_type='disk', cache_name='token_cache')  # Requires diskcache

SP-API fees & call optimization

With Amazon’s new SP-API pricing model (annual fees plus usage-based charges for GET requests), inefficient integrations will quickly become costly. If your systems rely on python-amazon-sp-api and you want to control these expenses, I offer consulting to review and optimize your implementation—such as replacing high-volume Orders API polling with report-based workflows and reducing unnecessary GET traffic wherever possible. If you’d like expert support preparing your SP-API usage for the upcoming pricing changes, feel free to get in touch.

fees@clairinsights.com


🚀 Version 2 🚀

Version 2 is currently being built - featuring pydantic, async support and better versioning. Check out v2-alpha here: v2-alpha


🌟 Thank you for using python-amazon-sp-api! 🌟

This tool helps developers and businesses connect seamlessly with Amazon's vast marketplace, enabling powerful automations and data management.

If you appreciate this project and find it useful, please consider supporting its continued development:

  • 🙌 GitHub Sponsors
  • 🌐 BTC Address: bc1q6uqgczasmnvnc5upumarugw2mksnwneg0f65ws
  • 🌐 ETH Address: 0xf59534F7a7F5410DBCD0c779Ac3bB6503bd32Ae5

Your support helps keep the project alive and evolving, and is greatly appreciated!


Documentation

Documentation is available here

Q & A

If you have questions, please ask them in GitHub discussions

discussions

or

join on slack

Installation

Note: This fork version may not be published to PyPI yet. Please use the following installation methods:

# Using uv (recommended)
uv add python-amazon-sp-api-async

# Or using pip
pip install python-amazon-sp-api-async

# If you need AWS Secret Manager authentication support
pip install "python-amazon-sp-api-async[aws]"

# If you need AWS cached secrets support
pip install "python-amazon-sp-api-async[aws-caching]"

# Install from source
git clone https://github.com/zhangwg4apollo/python-amazon-sp-api-async.git
cd python-amazon-sp-api-async
uv add -e .

Usage

import asyncio
from sp_api_async.api import Orders
from sp_api_async.api import Reports
from sp_api_async.api import DataKiosk
from sp_api_async.api import Feeds
from sp_api_async.base import SellingApiException
from sp_api_async.base.reportTypes import ReportType
from datetime import datetime, timedelta

async def main():
    # DATA KIOSK API
    async with DataKiosk() as client:
        res = await client.create_query(query="{analytics_salesAndTraffic_2023_11_15{salesAndTrafficByAsin(startDate:\"2022-09-01\" endDate:\"2022-09-30\" aggregateBy:SKU marketplaceIds:[\"ATVPDKIKX0DER\"]){childAsin endDate marketplaceId parentAsin sales{orderedProductSales{amount currencyCode}totalOrderItems totalOrderItemsB2B}sku startDate traffic{browserPageViews browserPageViewsB2B browserPageViewsPercentage browserPageViewsPercentageB2B browserSessionPercentage unitSessionPercentageB2B unitSessionPercentage}}}}")
        print(res)

    # orders API
    async with Orders() as orders_client:
        try:
            res = await orders_client.get_orders(CreatedAfter=(datetime.utcnow() - timedelta(days=7)).isoformat())
            print(res.payload)  # json data
        except SellingApiException as ex:
            print(ex)

    # report request
    async with Reports() as reports_client:
        create_report_response = await reports_client.create_report(reportType=ReportType.GET_MERCHANT_LISTINGS_ALL_DATA)

    # submit feed
    # feeds can be submitted like explained in Amazon's docs, or simply by calling submit_feed
    async with Feeds() as feeds_client:
        await feeds_client.submit_feed(<feed_type>, <file_or_bytes_io>, content_type='text/tsv', **kwargs)

    # PII Data
    async with Orders(restricted_data_token='<token>') as orders_client:
        res = await orders_client.get_orders(CreatedAfter=(datetime.utcnow() - timedelta(days=7)).isoformat())

    # or use the shortcut
    async with Orders() as orders_client:
        orders = await orders_client.get_orders(
            LastUpdatedAfter=(datetime.utcnow() - timedelta(days=1)).isoformat()
        )

asyncio.run(main())

New endpoints

You can create a new endpoint file by running make_endpoint <model_json_url>

make_endpoint https://raw.githubusercontent.com/amzn/selling-partner-api-models/main/models/listings-restrictions-api-model/listingsRestrictions_2021-08-01.json

This creates a ready to use client. Please consider creating a pull request with the new code.

ADVERTISING API

You can use nearly the same client for the Amazon Advertising API. @denisneuf has built Python-Amazon-Advertising-API on top of this client. Check it out here

DISCLAIMER

We are not affiliated with Amazon

LICENSE

License

This project follows the same MIT license as the original project.


📚 Related Links


Base Client

The client is pretty extensible and can be used for any other API. Check it out here:

API Client

Alt

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_amazon_sp_api_async-0.0.3.tar.gz (123.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_amazon_sp_api_async-0.0.3-py3-none-any.whl (175.5 kB view details)

Uploaded Python 3

File details

Details for the file python_amazon_sp_api_async-0.0.3.tar.gz.

File metadata

File hashes

Hashes for python_amazon_sp_api_async-0.0.3.tar.gz
Algorithm Hash digest
SHA256 ed4a14872a42a87eebb066811d6d6ac1c1591be1e21abdd7fa9ccc8fc7a9143a
MD5 a72b30fb2feaa8dc5af5cb95efc8f099
BLAKE2b-256 87af62bb448cf8543abcc290d9a2160a9632a6674b2ce05e827b98b692688979

See more details on using hashes here.

File details

Details for the file python_amazon_sp_api_async-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for python_amazon_sp_api_async-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 dfaa76bc1d5405d2d2172002dc84ad7d993ad81c74ac428b05913a858f476053
MD5 416ba1338ba792da3a57613620570352
BLAKE2b-256 c73b2972b868ebc1efef016c47a6bb1f64667bbef3c8974dee9e7b41757b7cca

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page