Skip to main content

snowland-shopeeapi

基于 aiohttp 的 Shopee OpenAPI (Partner API) v2 异步 SDK,同时支持正式环境沙箱环境

特性

  • async/await 异步实现,基于 aiohttp
  • 自动处理 Shopee 请求签名(HMAC-SHA256)
  • 内置 Environment.PRODUCTION / Environment.SANDBOX 两套域名
  • 开箱即用的 API 分组:授权、店铺、商品、订单、物流
  • 统一异常处理(ShopeeAPIError

安装

pip install snowland-shopeeapi
# 开发/测试依赖
pip install "snowland-shopeeapi[dev]"

本 SDK 的底层 HTTP 依赖 snowland-http(异步 aiohttp 后端)。 若从源码安装本仓库,请同时安装其本地依赖:

pip install -e ../snowland-http[aiohttp]

快速开始

import asyncio
from snowland_shopeeapi import ShopeeClient, Environment

PARTNER_ID = 123456
PARTNER_KEY = "your_partner_key"
SHOP_ID = 789012
REDIRECT_URL = "https://your-callback.com/cb"

async def main():
    async with ShopeeClient(PARTNER_ID, PARTNER_KEY, Environment.SANDBOX) as client:
        # 1) 引导商家授权,拼接授权 URL
        auth_url = client.build_auth_url(REDIRECT_URL)
        print("请商家访问:", auth_url)

        # 2) 用授权回调拿到的 code 换取 token
        code = "auth_code_from_callback"
        token = await client.auth.get_token(code, SHOP_ID)
        access_token = token["access_token"]

        # 3) 调用店铺接口
        info = await client.shop.get_shop_info(access_token, SHOP_ID)
        print(info)

        # 4) 刷新即将过期的 token
        refreshed = await client.auth.refresh_token(
            token["refresh_token"], access_token, SHOP_ID
        )
        print(refreshed)

asyncio.run(main())

环境切换

from snowland_shopeeapi import Environment

# 正式环境
client = ShopeeClient(PARTNER_ID, PARTNER_KEY, Environment.PRODUCTION)

# 沙箱环境(默认)
client = ShopeeClient(PARTNER_ID, PARTNER_KEY, Environment.SANDBOX)

支持的 API 分组

分组 示例方法
client.auth get_token(code, shop_id)refresh_token(...)
client.shop get_shop_infoget_profileupdate_shop
client.product get_item_listget_item_base_infoadd_itemupdate_itemdelete_item
client.order get_order_listget_order_detail
client.logistics get_shipping_parametercreate_shipping_documentget_tracking_number
client.returns get_return_listget_return_detailconfirm_returndispute_return
client.conversation get_conversation_listget_messagessend_messagereply_message
client.discount add_discountupdate_discountdelete_discountget_discount_listget_discountadd_discount_itemupdate_discount_itemdelete_discount_item

所有店铺级接口都需要传入 access_tokenshop_id

连接池、限流与重试

底层 HTTP(连接池、限流)由 snowland-http 提供:默认使用 aiohttp 异步后端, 并通过 max_rate(每秒请求数)与 burst(突发上限)开启令牌桶限流,主动避免触发 Shopee 接口限流。

SDK 在此基础上对瞬时故障自动重试:网络错误、超时、HTTP 429/5xx,以及 Shopee 限流错误码 (RATE_LIMIT_EXCEEDED 等)。重试采用指数退避(retry_backoff * 2^(n-1)),并在收到 429 时优先采用响应头 Retry-After

client = ShopeeClient(
    PARTNER_ID, PARTNER_KEY, Environment.SANDBOX,
    max_rate=10, burst=5,
    enable_retry=True, max_retries=3, retry_backoff=0.5,
)

自定义请求

需要调用尚未封装的接口时,可直接使用底层方法:

data = await client.request(
    "POST", "/api/v2/xxx/some_path",
    json={"foo": "bar"}, access_token=access_token, shop_id=SHOP_ID,
)

测试

python -m unittest discover -s tests

许可证

BSD-3-Clause

Download files

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

Source Distribution

snowland_shopeeapi-0.1.0.tar.gz (16.7 kB view details)

Uploaded Source

Built Distribution

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

snowland_shopeeapi-0.1.0-py3-none-any.whl (14.5 kB view details)

Uploaded Python 3

File details

Details for the file snowland_shopeeapi-0.1.0.tar.gz.

File metadata

  • Download URL: snowland_shopeeapi-0.1.0.tar.gz
  • Upload date:
  • Size: 16.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for snowland_shopeeapi-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1ba1f1f553b6a79ff8fb158f524661dbe7dff926b6fedb82defd7b062f2c0d00
MD5 f8b588db44857b769d78e5769c19a1b6
BLAKE2b-256 7680e6350764529ceb2745332d4e6415ff92ce357177477aaf97dc15baa2a3a7

See more details on using hashes here.

File details

Details for the file snowland_shopeeapi-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for snowland_shopeeapi-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 94a87927b8925cd1e2d0ec115965ff02f0497373763247ac641105879a556c02
MD5 4d5fda54aa0b4ca4f88673e61369b860
BLAKE2b-256 7566a05e2c507e71900a20b036835a2017579626faefbdf966458d5ee686eeb3

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 Sentry Error logging StatusPage Status page