YouTube New Video Webhook: PubSubHubbub vs a Monitor
YouTube pushes new-upload notifications through PubSubHubbub for free. Working Python code to subscribe, verify and parse them, and when a monitor with a signed webhook is the simpler choice.
By AyeWatch Team5 min read
Short answer: YouTube's way to get a webhook for new videos is PubSubHubbub: subscribe your HTTPS callback to a channel's feed at Google's hub, and it posts an Atom entry when the channel uploads a video or changes a title or description. An AyeWatch YouTube monitor checks the channel on a schedule, with no subscription to renew, at 1 credit a check.
PubSubHubbub, now standardized as WebSub, is a push protocol for feeds. A subscriber asks a hub to send updates for a topic URL to a callback URL. The hub confirms the request by calling the callback, then posts each new feed entry to it until the subscription's lease ends.
How YouTube push notifications work
Google's YouTube Data API guide describes the setup. The topic is a channel's video feed, https://www.youtube.com/xml/feeds/videos.xml?channel_id=CHANNEL_ID, and the hub is https://pubsubhubbub.appspot.com. Your server needs a public URL that can answer two requests:
- A GET to verify. After you subscribe, the hub calls your callback with
hub.mode,hub.topic,hub.challengeandhub.lease_seconds. You confirm by answering with the challenge as the body. - A POST for each notification. The body is an Atom entry with
yt:videoId,yt:channelId, the title, and published and updated times.
Notifications come for new uploads and for title or description changes on existing videos. A subscription lasts for the lease the hub grants, so you must subscribe again before it ends. Google's guide doesn't state the lease length; read hub.lease_seconds from the verification request and renew well before it runs out.
Subscribe to a channel
Send a form POST to the hub's subscribe URL. Pass a hub.secret so the hub signs each notification:
import os
import requests
CHANNEL_ID = "UCxxxxxxxxxxxxxxxxxxxxxx" # the channel's UC... ID
resp = requests.post(
"https://pubsubhubbub.appspot.com/subscribe",
data={
"hub.mode": "subscribe",
"hub.topic": "https://www.youtube.com/xml/feeds/videos.xml?channel_id=" + CHANNEL_ID,
"hub.callback": "https://hooks.yourapp.example/youtube",
"hub.secret": os.environ["HUB_SECRET"],
},
timeout=30,
)
print(resp.status_code, resp.text)
A 2xx answer means the hub took the request. It then calls your callback to verify it, so the receiver below must already be running. Find a channel's ID with the YouTube channel ID finder. Run the same request on a schedule to renew.
Verify and parse notifications in Flask
The hub signs each POST with X-Hub-Signature, written as the hash method, an equals sign and the hex HMAC of the raw body. The code reads the method from the header rather than assuming one:
import hmac
import os
import xml.etree.ElementTree as ET
from flask import Flask, request
app = Flask(__name__)
SECRET = os.environ["HUB_SECRET"].encode()
NS = {"atom": "http://www.w3.org/2005/Atom", "yt": "http://www.youtube.com/xml/schemas/2015"}
seen = set() # use a database table in production
@app.get("/youtube")
def verify():
if not request.args.get("hub.topic", "").startswith("https://www.youtube.com/xml/feeds/videos.xml"):
return "", 404
print("lease seconds:", request.args.get("hub.lease_seconds"))
return request.args.get("hub.challenge", ""), 200
@app.post("/youtube")
def notify():
raw = request.get_data()
method, _, digest = request.headers.get("X-Hub-Signature", "").partition("=")
if method not in ("sha1", "sha256"):
return "", 204
expected = hmac.new(SECRET, raw, method).hexdigest()
if not hmac.compare_digest(expected.encode(), digest.encode()):
return "", 204 # acknowledge, then ignore an unsigned or forged body
entry = ET.fromstring(raw).find("atom:entry", NS)
if entry is None:
return "", 204 # no entry, for example a deleted video
video_id = entry.findtext("yt:videoId", namespaces=NS)
if video_id in seen:
return "", 204 # a title or description edit of a video you already have
seen.add(video_id)
print("new video:", video_id, entry.findtext("atom:title", namespaces=NS))
return "", 204
WebSub lets a subscriber answer 2xx to a body with a bad signature and drop it, which is what the code does. Dedupe on the video ID, because edits send the same video again.
The jobs PubSubHubbub leaves to you
- Renewal. A lapsed lease stops notifications without an error. Store each lease and renew on a timer.
- Downtime. Google's guide doesn't describe retries, so plan for missed notifications while your server is down and backfill from the channel's feed or the YouTube Data API.
- Filtering. Every upload and edit arrives. Deciding which videos matter is your code.
- Search. PubSubHubbub covers channels you name. Finding new videos about a subject means the Data API's search, which by default has its own allowance of 100 calls a day.
The same job as an AyeWatch monitor
An AyeWatch Channel videos monitor checks the channel on a schedule and posts new videos to your webhook, signed with X-AyeWatch-Signature. There is no callback to verify and no lease to renew:
{
"source": {
"mode": "youtube_channel_videos",
"inputs": {
"channel": "https://www.youtube.com/@examplecameras",
"filter": "Only videos that review a mirrorless camera body"
}
},
"interval": "6_hours"
}
POST that body to /api/v1/topics. The channel input takes a youtube.com/@handle link, a youtube.com/channel/UC... link or a bare @handle; old /c/ and /user/ links don't work. Other modes: youtube_shorts, youtube_lives and youtube_community_posts for the same channel, and youtube_search with a query for new videos on a subject from any channel.
What a monitor won't do: notify within seconds of an upload (it finds the video at its next check), read comments, track view counts, or see private, unlisted or members-only videos.
Cost
PubSubHubbub is free; you pay for the server and your time. 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. Channel videos and Search monitors cost 1 credit a check, while Shorts, Live and Community posts cost 2, and a filter adds 1. One channel checked hourly uses 720 credits a month, more than Pro's 500, so on Pro check every 6 hours: 120 a channel, or 4 channels for 480. Pro+'s 1,500 cover two channels hourly (1,440).
Pick a route and set it up
- If you need notifications within moments of upload for a few known channels and can run a public server, use PubSubHubbub.
- If you need filtering, search across channels or no server to keep alive, use a monitor.
- For PubSubHubbub: deploy the Flask receiver, subscribe each channel, and schedule renewal from the lease seconds.
- For a monitor: register your webhook with PUT /api/v1/webhook and POST one youtube_channel_videos source per channel.
- Either way, dedupe on the video ID or update_id before you act.
| PubSubHubbub (WebSub) | AyeWatch YouTube monitor | |
|---|---|---|
| Price | Free | 1 credit a check, 2 with a filter |
| Speed | Pushed soon after upload | At the next check; ASAP mode on Pro+ |
| Edits | Sent again; you dedupe | Only new videos alert |
| Renewal | Before each lease ends | None |
| Filtering | Your code | A plain-English rule |
| Search across channels | Not covered | youtube_search mode |
| Signature | X-Hub-Signature with hub.secret | X-AyeWatch-Signature, HMAC-SHA256 |
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
If you already run a public server and need a handful of channels pushed fast, PubSubHubbub is free and works. If you want filtering, search or nothing to renew, create the monitor and point it at the receiver you already have.
FAQ
Does YouTube have a webhook for new videos?
Yes. YouTube publishes channel feed updates through PubSubHubbub at pubsubhubbub.appspot.com. You subscribe a public callback URL to the channel's feed and receive an Atom entry for each new upload.
Why does my PubSubHubbub subscription stop working?
Usually the lease ran out. Read hub.lease_seconds from the hub's verification request and subscribe again before it ends.
Why do I get notifications for old videos?
YouTube also notifies when a video's title or description changes. Keep the video IDs you have handled and skip repeats.
Can I get a webhook for new YouTube videos about a topic?
Not through PubSubHubbub, which covers named channels. An AyeWatch YouTube Search monitor with a query sends new videos on a subject from any channel.
How much does an AyeWatch YouTube channel monitor cost?
1 credit a check, or 2 with a filter. Every 6 hours that is 120 or 240 credits a month.