Bing Ads Python SDK
The Bing Ads Python SDK (pypi-slug: `bingads`) is a client library designed to simplify interactions with the Microsoft Advertising (formerly Bing Ads) SOAP API. It provides proxy classes for various web services, abstracts OAuth authentication, and offers high-level interfaces for campaign management, bulk operations, and reporting. The library is actively maintained with frequent updates to reflect changes in Bing Ads API Version 13.
Warnings
- breaking The `bingads` library is built for the legacy SOAP API. Microsoft has introduced a new REST API-based SDK called `msads` (available via `pip install msads`), which offers better performance, simpler architecture, and modern Python features. While `bingads` is still supported, new projects are encouraged to consider `msads` for future compatibility and benefits.
- breaking When upgrading between major Bing Ads API versions (e.g., from v12 to v13), there can be significant changes in SOAP object namespaces or structure that can cause `suds.resolver:(ClassGoesHere) not-found` errors. For example, `ns4:DateRangeSearchParameter` might become `DateRangeSearchParameter`.
- gotcha Initial OAuth authentication for the Bing Ads API (to obtain a refresh token) requires a user to interact with a browser, even for non-interactive (server-side) applications. This refresh token then needs to be securely stored and used for subsequent non-interactive access token refreshes.
- deprecated The default OAuth scope for `bingads` SDK has transitioned to `https://ads.microsoft.com/.default` (or `msads.manage`). Additionally, the sandbox authentication endpoint `login.live-int.com` has been replaced by `login.windows-ppe.net` and will be deprecated.
- deprecated The Microsoft Advertising Developer Portal page for obtaining developer tokens is scheduled for deprecation on May 31, 2025. A new portal will replace it.
Install
-
pip install bingads
Imports
- AuthorizationData
from bingads.authorization import AuthorizationData
- OAuthDesktopMobileApplication
from bingads.authorization import OAuthDesktopMobileApplication
- ServiceClient
from bingads.service_client import ServiceClient
- BulkServiceManager
from bingads.service_client import BulkServiceManager
- ReportingServiceManager
from bingads.service_client import ReportingServiceManager
- CustomerManagementService
from bingads.v13.customermanagement import CustomerManagementService
Quickstart
import os
from bingads.authorization import AuthorizationData, OAuthDesktopMobileApplication
from bingads.service_client import ServiceClient
# --- Configuration (replace with your actual values or env vars) ---
DEVELOPER_TOKEN = os.environ.get('BINGADS_DEVELOPER_TOKEN', 'YOUR_DEVELOPER_TOKEN_HERE')
CLIENT_ID = os.environ.get('BINGADS_CLIENT_ID', 'YOUR_CLIENT_ID_HERE')
REFRESH_TOKEN = os.environ.get('BINGADS_REFRESH_TOKEN', '') # Obtain this initially via user consent flow
# NOTE: For server-side apps, the REFRESH_TOKEN needs to be obtained once via a browser-based flow
# and then stored securely. This example assumes a refresh token is available.
# Define the OAuth scope for the Bing Ads API.
OAUTH_SCOPE = ['https://ads.microsoft.com/.default']
if not REFRESH_TOKEN:
print("WARNING: REFRESH_TOKEN not set. This example will only work if you manually obtain a refresh token.")
print("Refer to Bing Ads Python SDK documentation for obtaining refresh token via OAuth flow.")
# In a real application, you'd initiate the OAuth flow here if no refresh token is present.
# For this quickstart, we'll proceed assuming an empty token or one from env.
# Setup AuthorizationData
authorization_data = AuthorizationData(
developer_token=DEVELOPER_TOKEN,
authentication=OAuthDesktopMobileApplication(
client_id=CLIENT_ID,
oauth_scopes=OAUTH_SCOPE
)
)
# Set the refresh token if available
if REFRESH_TOKEN:
authorization_data.authentication.refresh_token = REFRESH_TOKEN
# Example: Get a list of accessible accounts
try:
# Get Customer Management Service Client
customer_service = ServiceClient(
service='CustomerManagementService',
version=13,
authorization_data=authorization_data
)
# Authenticate and get an access token (if refresh token is valid)
if not authorization_data.authentication.oauth_tokens:
authorization_data.authentication.request_oauth_tokens(authorization_data.authentication.refresh_token)
# If the refresh token was empty, this will likely fail unless the user interactively provides consent.
# For a quickstart, we assume refresh_token is pre-populated for non-interactive execution.
print(f"Access Token: {authorization_data.authentication.oauth_tokens.access_token[:10]}...\n")
# Get User accounts
user_accounts = customer_service.GetAccountsInfo(
UserId=None, # None implies current authenticated user
ReturnAdditionalFields=customer_service.factory.create('AccountAdditionalField').None
)
if user_accounts and user_accounts.AccountInfo:
print("Successfully retrieved accounts:")
for account in user_accounts.AccountInfo:
print(f"- Account ID: {account.Id}, Name: {account.Name}, Number: {account.Number}")
else:
print("No accounts found or accessible.")
except Exception as e:
print(f"An error occurred: {e}")
if "AuthenticationToken" in str(e) or "DeveloperToken" in str(e) or "Customer " in str(e):
print("Please ensure your DEVELOPER_TOKEN, CLIENT_ID, and REFRESH_TOKEN are correctly configured and valid.")
print("If running for the first time or if the refresh token is expired, you might need to run an interactive OAuth flow.")