A python 3 wrapper to access Amazon's Advertising API with an easy-to-use interface.
pip install python-amazon-ad-api
If you find this project is useful consider donating or sponsor it to keep on going on it, thank you.
You need obtain your own credentials with Amazon that may include an amazon developer account and access as seller or vendor. Please view the checklist of Amazon Ads API onboarding overview
You can use your credentials as follows passing it to the client as a dict. Please review the full documentation to see all posibilities to include your credentials.
from ad_api.api import ads_api_v1
my_credentials = dict(
refresh_token='your-refresh_token',
client_id='your-client_id',
client_secret='your-client_secret',
profile_id='your-profile_id',
)
info = {
"adProductFilter":
{
"include": [
"SPONSORED_PRODUCTS"
]
},
"stateFilter":
{
"include": [
"ENABLED"
]
}
}
result = ads_api_v1.Campaigns(credentials=my_credentials).query_campaigns(
body=info
)Use a credentials.yml file with your credentials for more convenience and manage diferent accounts or profiles. Amazon requires one profile per marketplace so it is helpful to keep all in one file and switch directly from the code, using the account.
Create a file credentials.yml
version: '1.0'
default:
refresh_token: 'your-refresh-token'
client_id: 'your-client-id'
client_secret: 'your-client-secret'
profile_id: 'your-profile-id'
germany:
refresh_token: 'other-refresh-token'
client_id: 'other-client-id'
client_secret: 'other-client-secret'
profile_id: 'other-profile-id'Python code
from ad_api.api import ads_api_v1
info = {
"adProductFilter":
{
"include": [
"SPONSORED_PRODUCTS"
]
}
}
# Leave empty will use the 'default' account
result=ads_api_v1.Campaigns().query_campaigns(body=info)
# will use germany account data
result=ads_api_v1.Campaigns(account="germany").query_campaigns(body=info)- macOS and Other Unix:
~/.config/python-ad-api - Windows:
%APPDATA%\python-ad-apiwhere the APPDATA environment variable falls back to%HOME%\AppData\Roamingif undefined
Marketplaces are used to define basically the API endpoints Amazon need to use depending on the regions, by default it will use EU so if you are using one of the marketplaces that are under the Europe (EU). Covers UK, FR, IT, ES, DE, NL, AE, SE, PL, and TR marketplaces you can skip. If you are using either North America (NA) or Far East (FE), you will need import from base and pass the marketplace as follows:
from ad_api.api import ads_api_v1
from ad_api.base import Marketplaces
info = {
"adProductFilter":
{
"include": [
"SPONSORED_PRODUCTS"
]
}
}
# You can pass NA or US, CA, MX or BR for North America and JP, AU or SG for Far East
result=ads_api_v1.Campaigns(marketplace=Marketplaces.NA).query_campaigns(body=info)You can use a try except statement when you call the API and catch exceptions if some problem ocurred:
from ad_api.api import ads_api_v1
from ad_api.base import AdvertisingApiException
info = {
"adProductFilter":
{
"include": [
"SPONSORED_PRODUCTS"
]
},
"stateFilter":
{
"include": [
"ENABLED"
]
}
}
try:
result = ads_api_v1.Campaigns().query_campaigns(
body=info
)
logging.info(result)
except AdvertisingApiException as error:
logging.info(error)Use debug=True if you want see some logs like the header you submit to the api endpoint, the method and path used among the params and the data submitted if any, to trace some possible errors. Note that the header includes the Authorization bearer token, so do not leave debug output in shared logs.
from ad_api.api import ads_api_v1
from ad_api.base import AdvertisingApiException
info = {
"adProductFilter":
{
"include": [
"SPONSORED_PRODUCTS"
]
},
"stateFilter":
{
"include": [
"ENABLED"
]
}
}
try:
result = ads_api_v1.Campaigns(debug=True).query_campaigns(
body=info
)
logging.info(result)
except AdvertisingApiException as error:
logging.info(error)ad_api.api.Invoices no longer exists. Amazon serves /invoices and
/invoices/{invoiceId} from the same contract as the rest of billing, so both
methods moved to Billing:
# before
from ad_api.api import Invoices
Invoices(account=store).list_invoices()
# now
from ad_api.api import Billing
Billing(account=store).list_invoices()Every Amazon Ads API family this package speaks to, and how many modules it ships. The per-module detail, with the contract and the vendor page of each one, lives in the documentation:
python-amazon-ad-api.readthedocs.io
| Family | Modules | Docs |
|---|---|---|
| Amazon Ads API v0 | 30 | v0 |
| Amazon Ads API v1 | 19 | v1 |
Coverage against the whole vendor tree, entries implemented and pending, is on Coverage and the tree itself on Full Vendor tree.
Both v1 and ads_api_v1 name the same namespace, so pick whichever reads
clearer: from ad_api.api import v1 is shorter, and ads_api_v1 makes it
obvious at the call site that the v1 API is being used.
from ad_api.api import v1
result = v1.Campaigns(credentials=credentials, debug=True).query_campaigns(
body={
"adProductFilter": {"include": ["SPONSORED_PRODUCTS"]},
"stateFilter": {"include": ["ENABLED"]},
"maxResults": 100,
}
)
campaigns = result.payload.get("campaigns")
next_token = result.payload.get("nextToken")This example uses
CampaignsV3.list_campaigns, which is deprecated. See the v1query_campaignsexample above.
import logging
from ad_api.base import AdvertisingApiException
from ad_api.api import sponsored_products
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s:%(levelname)s:%(message)s"
)
def sp_list_campaigns_v3(info: dict = None):
credentials = dict(
refresh_token='your-refresh_token',
client_id='your-client_id',
client_secret='your-client_secret',
profile_id='your-profile_id',
)
try:
result = sponsored_products.CampaignsV3(credentials=credentials, debug=True).list_campaigns(
body=info
)
payload = result.payload
return payload
except AdvertisingApiException as error:
logging.error(error)
logging.error(error.code)
state_filter = \
{
"stateFilter":
{
"include": [
"ENABLED"
]
}
}
enabled_campaigns = sp_list_campaigns_v3(state_filter).get("campaigns")
for campaign in enabled_campaigns:
logging.info(campaign)
logging.info(len(campaigns))
This API is based on the API Client created by @saleweaver but adapted to amazon advertising authentication requeriments
We are not affiliated with Amazon but they used our api :)
Last release: v2.2.3

