Skip to main content

Pure async Amazon Ads API v1 client — Sponsored Products, Sponsored Brands, Sponsored Display

Project description

async-amazon-ads-api-v1

PyPI version Python versions License: MIT

Pure async Amazon Ads API v1 client — Sponsored Products, Sponsored Brands, Sponsored Display.

Installation

pip install async-amazon-ads-api-v1
# 或
uv add async-amazon-ads-api-v1

Redis 缓存支持:

pip install "async-amazon-ads-api-v1[redis]"
# 或
uv add "async-amazon-ads-api-v1[redis]"

Quick Start

所有 API 方法仅接受 Pydantic model 实例,不支持 dict。

使用 Access Token(直接提供)

import asyncio
from async_amazon_ads_api_v1 import AmazonAdsConfig, Region, SPClient
from async_amazon_ads_api_v1.models.sp import SPQueryCampaignRequest


async def main() -> None:
    config = AmazonAdsConfig(
        access_token="your-access-token",
        client_id="your-client-id",
        region=Region.NA,
    )
    body = SPQueryCampaignRequest(
        adProductFilter={"include": ["SPONSORED_PRODUCTS"]},
        stateFilter={"include": ["ENABLED"]},
    )

    async with SPClient(config) as sp:
        resp = await sp.campaigns.query(body)
        print(resp.model_dump_json(indent=2))


asyncio.run(main())

使用 Refresh Token(自动续期)

import asyncio
from async_amazon_ads_api_v1 import AmazonAdsConfig, Region, SPClient
from async_amazon_ads_api_v1.models.sp import SPQueryCampaignRequest


async def main() -> None:
    config = AmazonAdsConfig(
        access_token="your-access-token",   # 可选,提供后直接使用
        client_id="your-client-id",
        refresh_token="your-refresh-token", # access_token 过期后自动刷新
        client_secret="your-client-secret",
        region=Region.NA,
    )
    body = SPQueryCampaignRequest(
        adProductFilter={"include": ["SPONSORED_PRODUCTS"]},
        stateFilter={"include": ["ENABLED"]},
    )

    async with SPClient(config) as sp:
        resp = await sp.campaigns.query(body)
        print(resp.model_dump_json(indent=2))


asyncio.run(main())

从环境变量加载

export AMAZON_ACCESS_TOKEN=...
export AMAZON_CLIENT_ID=...
export AMAZON_REFRESH_TOKEN=...     # 可选,用于自动续期
export AMAZON_CLIENT_SECRET=...     # 可选,refresh_token 时需要
export AMAZON_REGION=na             # na | eu | fe,默认 na
export AMAZON_PROFILE_ID=...        # 可选
from async_amazon_ads_api_v1.config.loader import from_toml

config = from_toml()

Environment Variables

变量 必填 说明
AMAZON_ACCESS_TOKEN 条件 OAuth bearer token(或使用 refresh_token 自动获取)
AMAZON_CLIENT_ID 条件 OAuth client ID(使用 refresh_token 时必填)
AMAZON_REFRESH_TOKEN OAuth refresh token,用于自动续期
AMAZON_CLIENT_SECRET 条件 OAuth client secret(使用 refresh_token 时必填)
AMAZON_REGION na | eu | fe,默认 na
AMAZON_PROFILE_ID 广告主 profile ID
AMAZON_TOKEN_URL 自定义 token 端点
AMAZON_TOKEN_CACHE_DIR 文件缓存目录(默认系统临时目录)
AMAZON_CACHE_BACKEND file | redis,默认 file
AMAZON_REDIS_URL Redis 连接 URL(使用 redis 缓存时需要)
AMAZON_ENDPOINT_NA 覆盖 NA 端点
AMAZON_ENDPOINT_EU 覆盖 EU 端点
AMAZON_ENDPOINT_FE 覆盖 FE 端点

Token Management

SDK 内置 OAuth token 生命周期管理,支持自动续期和缓存:

from async_amazon_ads_api_v1 import AmazonAdsConfig, Region

# 自动续期 + 文件缓存(默认)
config = AmazonAdsConfig(
    client_id="your-client-id",
    client_secret="your-client-secret",
    refresh_token="your-refresh-token",
    region=Region.NA,
)

# 自动续期 + Redis 缓存
config = AmazonAdsConfig(
    client_id="your-client-id",
    client_secret="your-client-secret",
    refresh_token="your-refresh-token",
    region=Region.NA,
    cache_backend="redis",
    redis_url="redis://localhost:6379",
)

API Reference

SPClient — Sponsored Products

资源 方法
client.campaigns create(), query(), update(), delete()
client.ad_groups create(), query(), update(), delete()
client.ads create(), query(), update(), delete()
client.targets create(), query(), update(), delete()
client.ad_extensions create(), query(), update()

SBClient — Sponsored Brands

资源 方法
client.campaigns create(), query(), update(), delete()
client.ad_groups create(), query(), update(), delete()
client.ads create(), query(), update(), delete()
client.targets create(), query(), update(), delete()
client.ad_extensions create(), query(), update()
client.advertising_deals create(), query(), update(), delete()
client.advertising_deal_targets create(), query(), delete()
client.branded_keywords_pricings create()
client.keyword_reservation_validations create()
client.recommendations create()
client.recommendation_types query()

SDClient — Sponsored Display

资源 方法
client.campaigns create(), query(), update(), delete()
client.ad_groups create(), query(), update(), delete()
client.ads create(), query(), update(), delete()
client.targets create(), query(), update(), delete()

Legacy / 非标准 API

以下资源类不遵循 /adsApi/v1 通用 CRUD 模式,需要单独实例化:

BudgetRules — 预算规则关联

SP、SB、SD 各自独立的资源类:

from async_amazon_ads_api_v1.client.legacy import SPBudgetRules, SBBudgetRules, SDBudgetRules

sp_rules = SPBudgetRules(ctx)
await sp_rules.create_budget_rules(request)
await sp_rules.update_budget_rules(request)

Portfolios — 投资组合

from async_amazon_ads_api_v1.client.legacy import Portfolios
from async_amazon_ads_api_v1.models.legacy.portfolios import UpdatePortfoliosRequestContent

pf = Portfolios(ctx)
resp = await pf.list()
await pf.update(UpdatePortfoliosRequestContent(portfolios=[...]))

SBOptimizationRules — SB 优化规则 (Beta)

from async_amazon_ads_api_v1.client.legacy import SBOptimizationRules
from async_amazon_ads_api_v1.models.legacy import SBEntityFilter, SBListOptimizationRulesRequest

rules = SBOptimizationRules(ctx)
request = SBListOptimizationRulesRequest(
    entityFilter=SBEntityFilter(entityType="CAMPAIGN", entityId="..."),
)
await rules.list_optimization_rules(request)
await rules.disassociate_optimization_rules(request)

SDOptimizationRules — SD 优化规则 (Beta)

from async_amazon_ads_api_v1.client.legacy import SDOptimizationRules

rules = SDOptimizationRules(ctx)
await rules.list_optimization_rules("ad-group-id")
await rules.disassociate_optimization_rules("ad-group-id", request)

SDCreatives — SD 创意

from async_amazon_ads_api_v1.client.legacy import SDCreatives

creatives = SDCreatives(ctx)
await creatives.create([{...}])

License

MIT

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

async_amazon_ads_api_v1-0.6.2.tar.gz (71.8 kB view details)

Uploaded Source

Built Distribution

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

async_amazon_ads_api_v1-0.6.2-py3-none-any.whl (118.2 kB view details)

Uploaded Python 3

File details

Details for the file async_amazon_ads_api_v1-0.6.2.tar.gz.

File metadata

  • Download URL: async_amazon_ads_api_v1-0.6.2.tar.gz
  • Upload date:
  • Size: 71.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for async_amazon_ads_api_v1-0.6.2.tar.gz
Algorithm Hash digest
SHA256 b855bd65146218a67acb410751e0831241383fc3902277282c43875732fa3505
MD5 8e4da7ddb80bb92cd1ef3f0d6c7a67b2
BLAKE2b-256 a4b15c591a7b8af42401f4ce5af5ba56536894f984d2aa2a8fb2961f310c8a3d

See more details on using hashes here.

Provenance

The following attestation bundles were made for async_amazon_ads_api_v1-0.6.2.tar.gz:

Publisher: publish.yml on apollosuite/async_amazon_ads_api_v1

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file async_amazon_ads_api_v1-0.6.2-py3-none-any.whl.

File metadata

File hashes

Hashes for async_amazon_ads_api_v1-0.6.2-py3-none-any.whl
Algorithm Hash digest
SHA256 572ca2426e01c5973f646d7d70ae705db24e42ef4e279665dc5bc718207ecced
MD5 e4dcf49be7abf4eb20844a3841c17b86
BLAKE2b-256 06b069e8d7c5602b78d15335f98bdb211bdafa982a180d179cc48ee6f942c028

See more details on using hashes here.

Provenance

The following attestation bundles were made for async_amazon_ads_api_v1-0.6.2-py3-none-any.whl:

Publisher: publish.yml on apollosuite/async_amazon_ads_api_v1

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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