Skip to main content

Official Primitive Python SDK for webhook verification and validation

Project description

primitivedotdev

Official Primitive Python SDK for webhook verification and validation.

This package helps you:

  • verify Primitive webhook signatures
  • parse webhook request bodies
  • validate webhook payloads against the canonical JSON schema
  • work with typed email.received events in Python

Validated events are returned as generated Pydantic models derived from the canonical JSON schema.

Requirements

  • Python >=3.10

Installation

pip install primitivedotdev

Basic Usage

from primitive import PrimitiveWebhookError, handle_webhook


def webhook_handler(body: bytes, headers: dict[str, str]) -> dict[str, object]:
    try:
        event = handle_webhook(
            body=body,
            headers=headers,
            secret="whsec_...",
        )

        print("Email from:", event.email.headers.from_)
        print("Subject:", event.email.headers.subject)
        return {"received": True}
    except PrimitiveWebhookError as error:
        return {"error": error.code, "message": str(error)}

Core API

Main functions

  • handle_webhook(...)
    • verifies the webhook signature
    • decodes and parses the request body
    • validates the payload
    • returns a typed EmailReceivedEvent
  • parse_webhook_event(input)
    • parses a JSON payload into a known webhook event or dict
    • validates known event types against the canonical schema
    • raises WebhookValidationError for malformed known events
  • validate_email_received_event(input)
    • validates an email.received payload and returns the typed event
  • safe_validate_email_received_event(input)
    • returns a ValidationSuccess or ValidationFailure
  • verify_webhook_signature(...)
    • verifies Primitive-Signature
  • validate_email_auth(auth)
    • computes a verdict from SPF, DKIM, and DMARC results

Helpful exports

  • email_received_event_json_schema
  • WEBHOOK_VERSION
  • PrimitiveWebhookError
  • WebhookVerificationError
  • WebhookPayloadError
  • WebhookValidationError
  • RawEmailDecodeError

Types and models

The package exports the main webhook models and helper types, including:

  • EmailReceivedEvent
  • WebhookEvent
  • UnknownEvent
  • EmailAuth
  • EmailAnalysis
  • ParsedData
  • RawContent
  • WebhookAttachment

Parsing Events

  • parse_webhook_event(input) strictly validates known event types such as email.received
  • malformed known events raise WebhookValidationError
  • unknown future event types are returned as dictionaries for forward compatibility

JSON Schema

The webhook payload contract is defined by the canonical JSON schema in the repository and is exported by this package as email_received_event_json_schema.

The SDK uses that schema to generate:

  • the packaged schema artifact
  • Pydantic models
  • runtime validation behavior

Error Handling

All SDK-specific runtime errors extend PrimitiveWebhookError and include a stable error code.

from primitive import PrimitiveWebhookError

try:
    ...
except PrimitiveWebhookError as error:
    print(error.code, error)

Development

From sdks/sdk-python:

uv sync --dev
uv run python scripts/generate_schema_module.py
uv run python scripts/generate_models.py
uv run pytest
uv run ruff check .
uv run basedpyright
uv run python -m build

Or from repo root sdks/:

make python-sync
make python-check
make python-build

Repository Layout

sdks/
  json-schema/
    email-received-event.schema.json
  sdk-python/
    src/primitive/
      models_generated.py
      schema.py
      schemas/email_received_event.schema.json
      validation.py
      webhook.py
    scripts/
      generate_models.py
      generate_schema_module.py

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

primitivedotdev-0.1.1.tar.gz (50.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

primitivedotdev-0.1.1-py3-none-any.whl (28.9 kB view details)

Uploaded Python 3

File details

Details for the file primitivedotdev-0.1.1.tar.gz.

File metadata

  • Download URL: primitivedotdev-0.1.1.tar.gz
  • Upload date:
  • Size: 50.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for primitivedotdev-0.1.1.tar.gz
Algorithm Hash digest
SHA256 53ee69e2ccd52fbfc0dfdafa738b1972a7ccfc0ebf98b8a6efef46f0bade24a5
MD5 6fbbadd83d58f792e45fa9c7a183b322
BLAKE2b-256 6afb82b9eaa4e24ddde44e4454a2a071bfd810185c85ac4c91203db4d43b7244

See more details on using hashes here.

Provenance

The following attestation bundles were made for primitivedotdev-0.1.1.tar.gz:

Publisher: python-release.yml on primitivedotdev/sdks

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file primitivedotdev-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: primitivedotdev-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 28.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for primitivedotdev-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6b1064b92ede41b1ae25628b4955b3085f8fe701c16df8fa0f81c03fdc8b6c61
MD5 7d42c6ee4ab30bbfbcb7fb71346eaa4f
BLAKE2b-256 d733f3421ab00531e2ee70127edb62121b177afad081eaed419aeb213db536df

See more details on using hashes here.

Provenance

The following attestation bundles were made for primitivedotdev-0.1.1-py3-none-any.whl:

Publisher: python-release.yml on primitivedotdev/sdks

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page