Bing Webmaster Tools API Client
An async Python client library for the Bing Webmaster Tools API, providing a clean interface for managing websites, analyzing search traffic, handling content submissions, and more.
Overview
Description
The Bing Webmaster API Client is a modern, async Python library that provides a comprehensive interface to Bing Webmaster Tools API. The library is designed with a focus on developer experience, type safety, and reliability, offering structured access to all Bing Webmaster Tools features through a clean, domain-driven API.
Key Features
- Async/await support - Built on aiohttp for efficient async operations
- Type-safe - Full typing support with runtime validation using Pydantic
- Domain-driven design - Operations organized into logical service groups
- Comprehensive - Complete coverage of Bing Webmaster Tools API
- Developer-friendly - Intuitive interface with detailed documentation
- Reliable - Built-in retry logic, rate limiting, and error handling
- Flexible - Support for both Pydantic models and dictionaries as input
- Production-ready - Extensive logging, testing, and error handling
Requirements
- Python 3.9 or higher
- Bing Webmaster API key (follow the instructions here)
Installation
PyPI Installation
Install the package using pip:
pip install bing-webmaster-tools
Quick Start
Basic Setup
-
Get your API key from Bing Webmaster Tools
-
Set your API key as an environment variable:
export BING_WEBMASTER_API_KEY=your_api_key_here
Or use a .env file:
BING_WEBMASTER_API_KEY=your_api_key_here
Examples
Look under the examples subdirectory to see boilerplate scripts which can help you get started. These assume you are using the environment variable BING_WEBMASTER_API_KEY
as described in the Basic Setup.
Authentication
The client supports several ways to provide authentication:
- Environment variables (recommended):
client = BingWebmasterClient(Settings.from_env())
- Direct initialization:
client = BingWebmasterClient(Settings(api_key="your_api_key_here"))
- Configuration file:
from dotenv import load_dotenv
load_dotenv()
client = BingWebmasterClient(Settings.from_env())
Usage
Client Initialization
The client can be initialized in several ways:
from bing_webmaster_tools import Settings, BingWebmasterClient
async with BingWebmasterClient(Settings.from_env()) as client:
sites = await client.sites.get_sites()
settings = Settings(
api_key="your_api_key",
timeout=30,
max_retries=5
)
async with BingWebmasterClient(settings) as client:
sites = await client.sites.get_sites()
client = BingWebmasterClient(Settings.from_env())
await client.init()
try:
sites = await client.sites.get_sites()
finally:
await client.close()
Configuration Options
The client behavior can be customized through the Settings class:
from bing_webmaster_tools import Settings
settings = Settings(
api_key="your_api_key",
base_url="https://ssl.bing.com/webmaster/api.svc/json",
timeout=30,
max_retries=3,
rate_limit_calls=10,
rate_limit_period=1,
disable_destructive_operations=False
)
Environment variables can be used with the BING_WEBMASTER_
prefix:
BING_WEBMASTER_API_KEY=your_api_key
BING_WEBMASTER_TIMEOUT=60
BING_WEBMASTER_MAX_RETRIES=5
Error Handling
The client provides structured error handling:
from bing_webmaster_tools.exceptions import (
BingWebmasterError,
AuthenticationError,
RateLimitError
)
async def handle_api_calls():
try:
await client.sites.get_sites()
except AuthenticationError:
print("Check your API key")
except RateLimitError:
print("Too many requests")
except BingWebmasterError as e:
print(f"API error: {e.message}, Status: {e.status_code}")
Rate Limiting
The client includes built-in rate limiting:
- Default: 5 calls per second
- Configurable via settings
- Automatic retry with exponential backoff on rate limit errors
To turn off rate limiting, simply set the rate limit configuration variables to None
.
Services
The client provides access to different API functionalities through specialized services.
For full details on available services and methods, refer to the API documentation.
Development
Development Installation
To install from source for development:
git clone https://github.com/merj/bing-webmaster-tools
cd bing-webmaster-tools
pip install poetry
poetry install