Website Monitoring API Python Example: A Small Client
A short Python client for the AyeWatch REST API that creates a page monitor, lists monitors page by page, pauses and deletes them, sets the webhook and handles 429s and error messages.
By AyeWatch Team4 min read
Short answer: Here is a website monitoring API example in Python: a short client built on requests that creates an AyeWatch page monitor, lists monitors page by page, pauses or deletes one and registers your webhook. It sends your key as a Bearer token, waits on 429 responses and raises errors with the X-Request-Id for support.
What you need
- Python 3.8 or later and
pip install requests. - An AyeWatch paid plan or trial, since API access needs one.
- An API key from API access settings, exported as
AYEWATCH_API_KEY. Keys start withaw_live_, and you can keep up to 10 active keys, one per app.
The API lives at https://ayewatch.ai/api/v1. Successful responses wrap results in data; errors return {"error": {"message": ..., "status": ...}}. The Topics docs list every field.
The client
# ayewatch_client.py
import os
import time
import requests
class AyeWatch:
def __init__(self, key=None, base="https://ayewatch.ai/api/v1"):
self.base = base
self.session = requests.Session()
self.session.headers["Authorization"] = "Bearer " + (key or os.environ["AYEWATCH_API_KEY"])
def _call(self, method, path, **kwargs):
while True:
resp = self.session.request(method, self.base + path, timeout=30, **kwargs)
if resp.status_code != 429:
break
time.sleep(int(resp.headers.get("Retry-After", "5")))
if not resp.ok:
try:
message = resp.json()["error"]["message"]
except (ValueError, KeyError):
message = resp.text[:200]
request_id = resp.headers.get("X-Request-Id")
raise RuntimeError(f"{resp.status_code} {message} (request {request_id})")
return resp.json()
def create_page_monitor(self, url, rule, interval="1_day"):
body = {"name": url, "description": rule, "interval": interval}
return self._call("POST", "/topics", json=body)["data"]
def topics(self, **filters):
page = 1
while True:
result = self._call("GET", "/topics", params={"page": page, "page_size": 100, **filters})
yield from result["data"]
if not result["pagination"]["has_more"]:
return
page += 1
def get(self, topic_id):
return self._call("GET", f"/topics/{topic_id}")["data"]
def pause(self, topic_id):
return self._call("PUT", f"/topics/{topic_id}", json={"is_active": False})["data"]
def delete(self, topic_id):
self._call("DELETE", f"/topics/{topic_id}")
def create_source_monitor(self, mode, inputs, interval="1_day", name=None):
body = {"source": {"mode": mode, "inputs": inputs}, "interval": interval}
if name:
body["name"] = name
return self._call("POST", "/topics", json=body)["data"]
def set_webhook(self, url):
return self._call("PUT", "/webhook", json={"webhook_url": url, "is_active": True})["data"]
def rotate_secret(self):
return self._call("POST", "/webhook/rotate-secret")["data"]["secret"]
topics() is a generator, so it fetches the next page only when you reach it. The 429 loop waits the seconds the server asks for. Every other error raises with the message and the request ID, which is what support needs.
Using it
from ayewatch_client import AyeWatch
client = AyeWatch()
monitor = client.create_page_monitor(
"https://haldenoutdoor.example/returns",
"Alert me when the return window, restocking fee or return shipping cost changes.",
)
print("created", monitor["id"], monitor["interval"])
reddit = client.create_source_monitor(
"reddit_search",
{"query": '"website monitoring api" OR "change detection api"',
"filter": "Only posts where someone is choosing a monitoring tool"},
)
print("created", reddit["id"], reddit["name"])
for topic in client.topics(is_active="true"):
print(topic["id"], topic["interval"], topic["name"])
hook = client.set_webhook("https://hooks.yourapp.example/ayewatch")
print("save this secret in your receiver's config:", hook["secret"])
A name that is a URL makes a web page monitor; the rule goes in description. A platform monitor takes a source with a mode and its inputs, and is named after the source unless you pass a name. Account inputs, such as a subreddit or a LinkedIn company, take links rather than bare names.
The webhook response includes the signing secret, which your receiver needs to check X-AyeWatch-Signature. See verifying the signature in Python. rotate_secret() replaces it at once with no overlap, so deploy the new value to your receiver straight after calling it.
Details that trip people up
- Booleans in query strings. The list filter needs the string
"true"or"false". Passing Python'sTruesendsTrue, which the API rejects with a 400. - Unknown fields. Create and update bodies are strict: a misspelled field returns a 400 instead of being ignored.
- Intervals. The interval must be one your plan includes, such as
1_hour,6_hours,12_hoursor1_day. Send"monitoring_mode": "auto"instead of an interval to let AyeWatch pick, starting at one check a day. - Delete is permanent.
pause()keeps the monitor and its history in the app;delete()removes it. - Page size. The list endpoint returns up to 100 per page, 20 by default.
What this client won't do
It doesn't fetch alerts, because the API has no alert-history endpoint. Alerts reach you by webhook, email, push notification or Slack, so store each webhook delivery as it arrives. It creates one monitor per call; for a list, use the bulk CSV script, which also handles duplicate names. And it doesn't retry 5xx errors or network failures; wrap calls in your own retry if a job must not fail.
Cost
AyeWatch Pro is $9/mo with 50 topics and hourly checks; Pro+ is $19/mo with 200 topics and ASAP mode. Every plan starts with a 7-day free trial. API calls are free within 120 requests a minute per key. A page monitor costs 1 credit a check: 30 a month checked daily, 120 every 6 hours and 720 hourly.
Run the example
- Save the client as ayewatch_client.py and install requests.
- Export AYEWATCH_API_KEY with a key from API access settings.
- Replace the example URL and rule with a page you check by hand today.
- Set your own webhook URL and store the returned secret in your receiver's environment.
- Run the script, then open the monitor in the app to see its first check.
| Method | HTTP call | Returns |
|---|---|---|
| create_page_monitor | POST /topics with a URL name | The new monitor |
| create_source_monitor | POST /topics with a source | The new monitor |
| topics | GET /topics, page by page | Each monitor in turn |
| get | GET /topics/{id} | One monitor |
| pause | PUT /topics/{id} with is_active false | The updated monitor |
| delete | DELETE /topics/{id} | Nothing; removal is permanent |
| set_webhook | PUT /webhook | The webhook, with its signing secret |
| rotate_secret | POST /webhook/rotate-secret | The new secret |
Start a monitor in under a minute
Describe a topic or paste a URL. Cancel before the trial ends and you pay nothing.
Try it free for 7 daysThe bottom line
Copy the client, create one page monitor and read its first check in the app. Then build the receiver, because the webhook is where the useful part of the API arrives.
FAQ
Is there an official AyeWatch Python SDK?
No. The REST API is small enough that a requests session with a Bearer header covers it, as in this client.
How do I list only active monitors in Python?
Call GET /api/v1/topics with is_active set to the string true. Python's True becomes the text True, which the API rejects.
How do I handle the API rate limit in Python?
On a 429 response, sleep for the Retry-After header's seconds and send the request again. The limit is 120 requests a minute per key.
Can I fetch alerts with the Python client?
No. The API has no alert-history endpoint. Receive alerts on your webhook and store them as they arrive.
Where do I get the webhook secret?
PUT /api/v1/webhook returns it in data.secret, and POST /api/v1/webhook/rotate-secret replaces it.