Official Primitive Python SDK for webhook handling and API access
Project description
primitivedotdev
Official Primitive Python SDK for webhook handling and API access.
This package helps you:
- verify Primitive webhook signatures
- parse webhook request bodies
- validate webhook payloads against the canonical JSON schema
- work with typed
email.receivedevents in Python - call the Primitive HTTP API from
primitive.api
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
API module
Use primitive.api for outbound calls to the Primitive HTTP API.
from primitive.api import create_client
from primitive.api.api.account.get_account import sync as get_account
client = create_client("prim_test")
account = get_account(client=client)
print(account)
Binary download helpers that support either API-key clients or token-based download links are exposed from primitive.api directly.
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
WebhookValidationErrorfor malformed known events
- parses a JSON payload into a known webhook event or
validate_email_received_event(input)- validates an
email.receivedpayload and returns the typed event
- validates an
safe_validate_email_received_event(input)- returns a
ValidationSuccessorValidationFailure
- returns a
verify_webhook_signature(...)- verifies
Primitive-Signature
- verifies
validate_email_auth(auth)- computes a verdict from SPF, DKIM, and DMARC results
Helpful exports
email_received_event_json_schemaWEBHOOK_VERSIONPrimitiveWebhookErrorWebhookVerificationErrorWebhookPayloadErrorWebhookValidationErrorRawEmailDecodeError
Types and models
The package exports the main webhook models and helper types, including:
EmailReceivedEventWebhookEventUnknownEventEmailAuthEmailAnalysisParsedDataRawContentWebhookAttachment
Parsing Events
parse_webhook_event(input)strictly validates known event types such asemail.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
api/
schema.py
schemas/email_received_event.schema.json
validation.py
webhook.py
scripts/
generate_api_client.py
generate_models.py
generate_schema_module.py
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file primitivedotdev-0.3.0.tar.gz.
File metadata
- Download URL: primitivedotdev-0.3.0.tar.gz
- Upload date:
- Size: 83.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c65be85bffc87595e1a5121b6df65c53b4229a8848512a81140a554dcf0e0c85
|
|
| MD5 |
7502064ea78bfd2d7a389246b8a108d8
|
|
| BLAKE2b-256 |
4c2558e34790065fb13c66a4c2ebd25deb72c3b493183591022fb24472fa6e3a
|
Provenance
The following attestation bundles were made for primitivedotdev-0.3.0.tar.gz:
Publisher:
python-release.yml on primitivedotdev/sdks
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
primitivedotdev-0.3.0.tar.gz -
Subject digest:
c65be85bffc87595e1a5121b6df65c53b4229a8848512a81140a554dcf0e0c85 - Sigstore transparency entry: 1325651790
- Sigstore integration time:
-
Permalink:
primitivedotdev/sdks@41ba23122a3b9af7f234ec15b9e542fe3f997ff2 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/primitivedotdev
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
self-hosted -
Publication workflow:
python-release.yml@41ba23122a3b9af7f234ec15b9e542fe3f997ff2 -
Trigger Event:
push
-
Statement type:
File details
Details for the file primitivedotdev-0.3.0-py3-none-any.whl.
File metadata
- Download URL: primitivedotdev-0.3.0-py3-none-any.whl
- Upload date:
- Size: 139.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b9ff1544b81fe676f10139a196ba697d243f58263d250d40a238598fd084d826
|
|
| MD5 |
d4f9310a4429aa8a6f514548e76c1bfa
|
|
| BLAKE2b-256 |
bc111d8af85dd4836564120784caac51c5e7de82d35185b92948ccd50b0735ce
|
Provenance
The following attestation bundles were made for primitivedotdev-0.3.0-py3-none-any.whl:
Publisher:
python-release.yml on primitivedotdev/sdks
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
primitivedotdev-0.3.0-py3-none-any.whl -
Subject digest:
b9ff1544b81fe676f10139a196ba697d243f58263d250d40a238598fd084d826 - Sigstore transparency entry: 1325651943
- Sigstore integration time:
-
Permalink:
primitivedotdev/sdks@41ba23122a3b9af7f234ec15b9e542fe3f997ff2 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/primitivedotdev
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
self-hosted -
Publication workflow:
python-release.yml@41ba23122a3b9af7f234ec15b9e542fe3f997ff2 -
Trigger Event:
push
-
Statement type: