Skip to main content

py-webhook

PyPI version License: MIT Python 3.10+

Simple webhook sender and receiver for Python/FastAPI. Send webhooks with retry logic, receive with HMAC-SHA256 signature verification.

Features

  • HMAC-SHA256 Signatures -- every webhook is signed and verified
  • Retry Logic -- configurable retries with exponential backoff
  • Async -- built on httpx for non-blocking I/O
  • FastAPI Integration -- drop-in Depends() for automatic verification
  • Lightweight -- minimal dependencies, no magic

Installation

pip install py-webhook

Quick Start

Sending Webhooks

import asyncio
from pywebhook import WebhookSender

sender = WebhookSender(max_retries=3, timeout=10.0)

async def main():
    response = await sender.send(
        url="https://example.com/webhook",
        event="order.created",
        data={"order_id": 42, "total": 99.99},
        secret="your-shared-secret",
    )
    print(f"Delivered: {response.status_code}")

asyncio.run(main())

The sender automatically:

  • Generates a WebhookPayload with a UUID, timestamp, event name, and your data
  • Signs the JSON body with HMAC-SHA256
  • Sends headers: X-Webhook-Signature, X-Webhook-Event, X-Webhook-Id
  • Retries failed requests with exponential backoff

Receiving Webhooks (FastAPI)

from fastapi import Depends, FastAPI
from pywebhook import WebhookReceiver

app = FastAPI()
receiver = WebhookReceiver()

@app.post("/webhook")
async def handle_webhook(payload: dict = Depends(receiver.handler("your-shared-secret"))):
    print(f"Received event: {payload['event']}")
    print(f"Data: {payload['data']}")
    return {"status": "ok"}

The handler dependency automatically:

  • Reads the raw request body
  • Verifies the X-Webhook-Signature header against the shared secret
  • Returns 401 if the signature is invalid
  • Parses and returns the JSON payload

Manual Verification

from pywebhook import WebhookReceiver

receiver = WebhookReceiver()

body = b'{"event":"ping","data":{}}'
signature = "abc123..."
is_valid = receiver.verify(body, signature, secret="your-shared-secret")

Payload Model

from pywebhook import WebhookPayload

payload = WebhookPayload(event="user.signup", data={"user_id": 7})
print(payload.id)         # auto-generated UUID
print(payload.timestamp)  # auto-generated ISO 8601
print(payload.event)      # "user.signup"
print(payload.data)       # {"user_id": 7}

Configuration

Parameter Default Description
max_retries 3 Number of delivery attempts
timeout 10.0 Request timeout in seconds
backoff_base 1.0 Base delay for exponential backoff (seconds)

License

MIT -- see LICENSE for details.

Release files for pywebhook-lite 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pywebhook-lite 0.1.0
File Size Uploaded
pywebhook_lite-0.1.0.tar.gz 6.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pywebhook-lite 0.1.0
File Interpreter ABI Platform
pywebhook_lite-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 12.5 kB

Release files / pywebhook_lite-0.1.0.tar.gz

Download URL pywebhook_lite-0.1.0.tar.gz
Size 6.1 kB
Tags Source
SHA-256 checksum
How to use checksums
ad0886134095e4024749e9b780a9f16c0117c700a808234de14bb96d2f75f512
BLAKE2b-256 checksum
How to use checksums
bd01b1c9f3a2a80d8c4dd6c1f3d07ea57f5aab41562cfdd9ae898a0e95119948
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release files / pywebhook_lite-0.1.0-py3-none-any.whl

Download URL pywebhook_lite-0.1.0-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0123aebf434c6aa7dff3998a14980e328692bf71085e866da0b8ffd78b5ff874
BLAKE2b-256 checksum
How to use checksums
41a32599eb365b3f59480db8d881f0a33c3046cda2ad077e65968fe2cc99ccc5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page