Lightweight Python client for the ChatAds affiliate scoring API
Project description
ChatAds Python SDK
A tiny, dependency-light wrapper around the ChatAds /v1/chatads/messages endpoint. It mirrors the response payloads returned by the FastAPI service so you can drop it into CLIs, serverless functions, or orchestration tools.
Learn more at ChatAds.
Installation
pip install chatads-sdk
The package is published on PyPI. Install from source only if you're developing locally.
Quickstart
from chatads_sdk import ChatAdsClient, AsyncChatAdsClient, FunctionItemPayload
# Synchronous usage
with ChatAdsClient(
api_key="YOUR_X_API_KEY",
base_url="https://chatads--chatads-api-fastapiserver-serve.modal.run",
raise_on_failure=True,
max_retries=2,
retry_backoff_factor=0.75,
) as client:
payload = FunctionItemPayload(
message="Looking for a CRM to close more deals",
country="US",
)
result = client.analyze(payload)
if result.success and result.data.filled:
print(result.data.ad.product, result.data.ad.link)
else:
print("No match:", result.data.reason if result.data else "unknown")
# Async usage
async with AsyncChatAdsClient(
api_key="YOUR_X_API_KEY",
base_url="https://chatads--chatads-api-fastapiserver-serve.modal.run",
max_retries=3,
) as async_client:
result = await async_client.analyze_message(
"Need data warehousing ideas",
country="US",
message_analysis="fast",
)
print(result.raw)
Request Options
The FunctionItemPayload supports these fields:
| Field | Type | Description |
|---|---|---|
message |
str (required) | Message to analyze (1-5000 chars) |
ip |
str | Client IP address for geo-detection (max 64 chars) |
country |
str | ISO 3166-1 alpha-2 country code for geo-targeting |
message_analysis |
str | Extraction method: "fast", "balanced" (default), "thorough" |
fill_priority |
str | URL resolution: "speed" or "coverage" (default) |
min_intent |
str | Intent filter: "any", "low" (default), "medium", "high" |
skip_message_analysis |
bool | Skip NLP/LLM and use message directly as search query |
Response Structure
result.success # bool - True if request succeeded
result.data.matched # bool - True if keywords were extracted
result.data.filled # bool - True if affiliate URL was returned
result.data.ad # ChatAdsAd or None
result.data.keyword # Extracted keyword used for matching
result.data.reason # Reason for no match (if applicable)
result.error # ChatAdsError or None (code, message, details)
result.meta.request_id # Unique request identifier
result.meta.usage # UsageInfo with quota information
result.raw # Full raw JSON response
Error Handling
Non-2xx responses raise ChatAdsAPIError with:
status_code- HTTP status coderesponse.error.code- Error code (e.g.,DAILY_QUOTA_EXCEEDED,RATE_LIMITED)response.error.message- Human-readable messageretry_after- Seconds to wait (for 429 responses)
Set raise_on_failure=True to also raise on 200 responses with success=false.
Retryable status codes (automatic with max_retries>0):
408Request Timeout429Rate Limited500,502,503,504Server errors
Notes
- Retries are opt-in. Provide
max_retries>0to automatically retry transport errors and retryable status codes. The client honorsRetry-Afterheaders and falls back to exponential backoff. base_urlmust point to your HTTPS deployment (the client rejects plaintext URLs so API keys are never transmitted insecurely).- The default hosted environment lives at
https://chatads--chatads-api-fastapiserver-serve.modal.run; use your own domain if you're proxying ChatAds behind something else. FunctionItemPayloadmatches the server-sideFunctionItempydantic model. Keyword arguments passed toChatAdsClient.analyze_message()accept either snake_case or camelCase keys.- Reserved payload keys (e.g.,
message,pageUrl) cannot be overridden throughextra_fields; doing so raisesValueErrorto prevent silent mutations. debug=Trueenables structured request/response logging, but payload contents are redacted automatically so you don't leak PII into logs.
CLI Smoke Test
For a super-quick check, either edit the config block at the top of run_sdk_smoke.py or set:
export CHATADS_API_KEY="..."
export CHATADS_BASE_URL="https://chatads--chatads-api-fastapiserver-serve.modal.run"
export CHATADS_MESSAGE="Looking for ergonomic office chairs"
# Optional extras
export CHATADS_IP="1.2.3.4"
export CHATADS_COUNTRY="US"
Then run:
python run_sdk_smoke.py
It prints the raw JSON response or surfaces a ChatAdsAPIError with status/error fields so you can see exactly what the API returned.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file chatads_sdk-0.1.8.tar.gz.
File metadata
- Download URL: chatads_sdk-0.1.8.tar.gz
- Upload date:
- Size: 12.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
349701c3a46cfb2189e90f2db3b1323677e7eda627698b209803f990642ac795
|
|
| MD5 |
8faf55cf60482d8482d3f2edc9429d65
|
|
| BLAKE2b-256 |
228a2c14bb3dfbd8ce1911ccf999ea475836ed1f8941d99475e22c3abdcf4269
|
File details
Details for the file chatads_sdk-0.1.8-py3-none-any.whl.
File metadata
- Download URL: chatads_sdk-0.1.8-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f010826f0d672f446f9fee37e5933bb15e6740891d863240c4da6f05ee4b9ac3
|
|
| MD5 |
82209ad14f9fc31975431a211baafd72
|
|
| BLAKE2b-256 |
eca1903151a0a104f7ba68114db4282a105fa52bf3f8d8870af65e155681752c
|