pify/webhook
Inbound webhook deliveries.
Shopify signs the raw body with base64(HMAC-SHA256(client secret, body))
in X-Shopify-Hmac-Sha256. Every other header is unsigned. The body is
the REST form of the resource (snake_case, integer ids), not the GraphQL
form, so a consumer reads a reference from it and fetches the resource
through the Admin API before it applies anything. Deliveries can arrive
out of order and more than once.
An orders/delete body and a products/delete body are both
{"id": N}, so no check can tell them apart. A consumer deletes an
order only after order.get confirms that the order is absent.
Types
The headers Shopify sets on every delivery.
pub type Delivery {
Delivery(
topic: Topic,
hmac: String,
shop_domain: String,
api_version: String,
webhook_id: String,
event_id: option.Option(String),
triggered_at: option.Option(String),
name: option.Option(String),
)
}
Constructors
-
Delivery( topic: Topic, hmac: String, shop_domain: String, api_version: String, webhook_id: String, event_id: option.Option(String), triggered_at: option.Option(String), name: option.Option(String), )Arguments
- api_version
-
The version that serialized the body. It can differ from the version the subscription asked for.
- webhook_id
-
Unique per delivery and stable across retries of it.
- event_id
-
Shared by every subscription that one merchant action triggered.
pub type HeaderError {
HeaderErrorMissing(name: String)
}
Constructors
-
HeaderErrorMissing(name: String)
The order a delivery is about. Re-read the order through
order.get(admin_graphql_api_id) before applying it.
pub type OrderRef {
OrderRef(
id: Int,
admin_graphql_api_id: String,
name: option.Option(String),
updated_at: option.Option(String),
)
}
Constructors
-
OrderRef( id: Int, admin_graphql_api_id: String, name: option.Option(String), updated_at: option.Option(String), )
pub type Topic {
TopicOrdersCreate
TopicOrdersUpdated
TopicOrdersCancelled
TopicOrdersDelete
TopicOrdersFulfilled
TopicOrdersPaid
TopicOrdersPartiallyFulfilled
TopicOrdersEdited
TopicProductsCreate
TopicProductsUpdate
TopicProductsDelete
TopicInventoryLevelsUpdate
TopicAppUninstalled
TopicUnknown(raw: String)
}
Constructors
-
TopicOrdersCreate -
TopicOrdersUpdated -
TopicOrdersCancelled -
TopicOrdersDelete -
TopicOrdersFulfilled -
TopicOrdersPaid -
TopicOrdersPartiallyFulfilled -
TopicOrdersEdited -
TopicProductsCreate -
TopicProductsUpdate -
TopicProductsDelete -
TopicInventoryLevelsUpdate -
TopicAppUninstalled -
TopicUnknown(raw: String)A topic this library does not know. Kept verbatim, never dropped.
Values
pub fn check_topic(
topic: Topic,
body: BitArray,
) -> Result(Nil, error.ShopifyError)
Hold the unsigned topic header to the signed body’s shape. A delete
body is {"id": N}; an order or product body carries its own GraphQL id;
an orders/edited body wraps the edit in order_edit.
pub fn dedupe_key(topic: Topic, body: BitArray) -> String
Lowercase hex SHA-256 of the topic, a newline and the raw body. Only the body is signed, so the key reads no unsigned id. A retry resends the same body and gets the same key; two topics with one body get two keys.
pub const header_api_version: String
pub const header_event_id: String
pub const header_hmac: String
pub const header_name: String
pub const header_shop_domain: String
pub const header_topic: String
pub const header_triggered_at: String
pub const header_webhook_id: String
pub fn order_ref(
body: BitArray,
) -> Result(OrderRef, error.ShopifyError)
Read the order reference from an orders/* body. A delete body carries
only the numeric id and an orders/edited body only order_edit.order_id,
so the GraphQL id is built from the number.
pub fn parse_headers(
headers: List(#(String, String)),
) -> Result(Delivery, HeaderError)
Header names are matched case-insensitively.
pub fn parse_topic(raw: String) -> Topic
pub fn topic_to_graphql_enum(
topic: Topic,
) -> Result(String, error.ShopifyError)
The WebhookSubscriptionTopic enum value a subscription names. A topic
this library does not know has none and is ShopifyErrorTopicUnsupported.
pub fn topic_to_string(topic: Topic) -> String
pub fn verify(
secret: String,
body: BitArray,
hmac: String,
) -> Result(Nil, error.ShopifyError)
Verify the signature over the raw body bytes. Run this before any JSON parse. An empty secret never verifies, because anyone can sign with it.