Connecting Agents to Webhooks
Receiving webhook events and triggering agent workflows in response.
What Is a Webhook?
A webhook is an HTTP callback. When an event occurs in an external service, it sends a POST request to your endpoint with event data. Your agent processes the payload and acts.
Webhooks are push-based (events arrive when they happen) versus polling (you check repeatedly).
FastAPI Webhook Endpoint
FastAPI makes it easy to create a webhook receiver. Define a POST route, parse the JSON body, and hand off to your agent logic.
from fastapi import FastAPI, Request
from pydantic import BaseModel
app = FastAPI()
class WebhookPayload(BaseModel):
event: str
data: dict
@app.post('/webhook')
async def receive_webhook(payload: WebhookPayload):
print(f'Received event: {payload.event}')
print(f'Data: {payload.data}')
# Route to the right agent handler
if payload.event == 'email.received':
await handle_email_event(payload.data)
elif payload.event == 'file.uploaded':
await handle_file_event(payload.data)
return {'status': 'accepted'}
async def handle_email_event(data: dict):
print(f'Processing email from: {data.get("from")}')
async def handle_file_event(data: dict):
print(f'Processing file: {data.get("filename")}')All lessons in this course
- Trigger-Action Agent Patterns
- Connecting Agents to Webhooks
- Scheduling and Cron-Based Agents
- Building a Multi-App Automation Pipeline