Перейти к содержанию

Вебхуки

cdekx.webhooks.events.WebhookEvent

Bases: CdekModel

A CDEK webhook event.

attributes is typed automatically by type; if the type is unknown, it stays a dict and no data is lost.

is_known_type property

Return whether the event type is known to the library.

as_order_status()

Coerce attributes to the status model (or None).

cdekx.webhooks.events.parse_event(payload)

Parse the body of an incoming webhook.

Parameters:

Name Type Description Default
payload Any

a dict or a JSON string from CDEK.

required

Returns:

Type Description
WebhookEvent

class:WebhookEvent with typed attributes if the type is known.

Raises:

Type Description
UnknownEventError

the payload has none of the required event fields.

Example

event = parse_event({ # doctest: +SKIP ... "type": "ORDER_STATUS", ... "date_time": "2026-05-01T12:30:00+0300", ... "uuid": "8dea00e3-9aab-4d8e-887c-ef2aaa546456", ... "attributes": {"code": "DELIVERED", "cdek_number": "1234567890"}, ... })

cdekx.webhooks.events.OrderStatusAttributes

Bases: CdekModel

attributes of an ORDER_STATUS event.

cdekx.webhooks.events.PrintFormAttributes

Bases: CdekModel

attributes of a PRINT_FORM event (a print form is ready).

cdekx.webhooks.events.CourierInfoAttributes

Bases: CdekModel

attributes of a COURIER_INFO event.

cdekx.webhooks.asgi.WebhookApp

A small application for receiving webhooks (Starlette under the hood).

asgi property

The ASGI app (built lazily so Starlette is not required at import time).

handle_raw(body, *, path='', client_ip=None) async

Handle a raw request body.

Returns:

Type Description
int

A (status_code, payload) pair — convenient for tests and for

dict[str, Any]

integrating with frameworks other than Starlette.

cdekx.webhooks.security

Hardening the webhook receiver endpoint.

The platform limitations matter here:

  • CDEK does not sign the webhook body with an HMAC signature (unlike, say, Telegram or Stripe). The only built-in defences are checking the sender IP and your own "secret path" in the subscription URL.
  • The recommended layout is therefore https://example.com/webhooks/cdek/{secret}, where secret is a random string of 32+ characters known only to you. An HMAC helper is provided below for the case where you proxy events through your own service that signs them.

CDEK IP ranges change from time to time: check CDEK_IP_RANGES against the documentation or support before enabling strict verification.

is_cdek_ip(ip, ranges=CDEK_IP_RANGES)

Check whether an IP falls into the known CDEK ranges.

Returns False when the range list is empty (verification not configured) so that traffic is not blocked by default.

verify_ip(ip, ranges=CDEK_IP_RANGES)

Raise PermissionError if the IP is outside the CDEK ranges.

compare_signature(body, signature, secret)

Verify an HMAC-SHA256 signature of the body (for your own proxy layer).

Parameters:

Name Type Description Default
body bytes

raw request body bytes.

required
signature str | None

value of the signature header (hex or sha256=<hex>).

required
secret str

your shared secret.

required

allowed_types(event_types)

Normalise the list of allowed event types (None means all).