Skip to main content

Triggers

The ballerinax/trigger.shopify package supports event-driven integration through Shopify webhook notifications. After you subscribe to Shopify webhook topics, the listener receives webhook requests and invokes your service callbacks when matching order, customer, product, or fulfillment events occur.

Three components work together:

ComponentRole
shopify:ListenerExposes the webhook endpoint and dispatches incoming Shopify events to attached services.
shopify:ListenerConfigDefines listener configuration, including the Shopify API secret key used to validate webhook requests.
shopify:OrdersServiceDefines order event callbacks such as onOrdersCreate, onOrdersPaid, and onOrdersUpdated.
shopify:CustomersServiceDefines customer event callbacks such as onCustomersCreate, onCustomersUpdate, and onCustomersMarketingConsentUpdate.
shopify:ProductsServiceDefines product event callbacks such as onProductsCreate and onProductsUpdate.
shopify:FulfillmentsServiceDefines fulfillment event callbacks such as onFulfillmentsCreate and onFulfillmentsUpdate.

For action-based operations, see the Action Reference.


Listener

The shopify:Listener receives Shopify webhook HTTP requests and routes events to the relevant service type. Configure Shopify webhook subscriptions to point to the public URL of the running listener, use JSON as the webhook format, and add a trailing / to the delivery URL if it is not already present.

Configuration

The listener supports the following connection strategy:

Config TypeDescription
ListenerConfigConfiguration for the Shopify webhook listener.

ListenerConfig fields:

FieldTypeDefaultDescription
apiSecretKeystringRequiredShopify API secret key used to validate incoming webhook requests.

The listener also accepts a port or an HTTP listener object to expose the webhook endpoint. If you do not provide a port, the listener uses the default port 8090.

Initializing the listener

Listener with a configurable API secret and port:

import ballerinax/trigger.shopify;

configurable string apiSecretKey = ?;
configurable int listenerPort = ?;

shopify:ListenerConfig listenerConfig = {
apiSecretKey: apiSecretKey
};

listener shopify:Listener shopifyListener = new (listenerConfig, listenerPort);

Listener with the default port:

import ballerinax/trigger.shopify;

configurable string apiSecretKey = ?;

shopify:ListenerConfig listenerConfig = {
apiSecretKey: apiSecretKey
};

listener shopify:Listener shopifyListener = new (listenerConfig);

Service

A Shopify trigger service is a Ballerina service attached to a shopify:Listener. Select the service type that matches the Shopify webhook topic you want to handle.

Callback signatures

Service TypeCallbackSignatureDescription
shopify:OrdersServiceonOrdersCreateremote function onOrdersCreate(shopify:OrderEvent event) returns error?Invoked when an order is created.
shopify:OrdersServiceonOrdersCancelledremote function onOrdersCancelled(shopify:OrderEvent event) returns error?Invoked when an order is cancelled.
shopify:OrdersServiceonOrdersFulfilledremote function onOrdersFulfilled(shopify:OrderEvent event) returns error?Invoked when an order is fulfilled.
shopify:OrdersServiceonOrdersPaidremote function onOrdersPaid(shopify:OrderEvent event) returns error?Invoked when an order is paid.
shopify:OrdersServiceonOrdersPartiallyFulfilledremote function onOrdersPartiallyFulfilled(shopify:OrderEvent event) returns error?Invoked when an order is partially fulfilled.
shopify:OrdersServiceonOrdersUpdatedremote function onOrdersUpdated(shopify:OrderEvent event) returns error?Invoked when an order is updated.
shopify:CustomersServiceonCustomersCreateremote function onCustomersCreate(shopify:CustomerEvent event) returns error?Invoked when a customer is created.
shopify:CustomersServiceonCustomersDisableremote function onCustomersDisable(shopify:CustomerEvent event) returns error?Invoked when a customer is disabled.
shopify:CustomersServiceonCustomersEnableremote function onCustomersEnable(shopify:CustomerEvent event) returns error?Invoked when a customer is enabled.
shopify:CustomersServiceonCustomersUpdateremote function onCustomersUpdate(shopify:CustomerEvent event) returns error?Invoked when a customer is updated.
shopify:CustomersServiceonCustomersMarketingConsentUpdateremote function onCustomersMarketingConsentUpdate(shopify:CustomerEvent event) returns error?Invoked when a customer's marketing consent is updated.
shopify:ProductsServiceonProductsCreateremote function onProductsCreate(shopify:ProductEvent event) returns error?Invoked when a product is created.
shopify:ProductsServiceonProductsUpdateremote function onProductsUpdate(shopify:ProductEvent event) returns error?Invoked when a product is updated.
shopify:FulfillmentsServiceonFulfillmentsCreateremote function onFulfillmentsCreate(shopify:FulfillmentEvent event) returns error?Invoked when a fulfillment is created.
shopify:FulfillmentsServiceonFulfillmentsUpdateremote function onFulfillmentsUpdate(shopify:FulfillmentEvent event) returns error?Invoked when a fulfillment is updated.
note

Implement only the callbacks relevant to the webhook topics subscribed in Shopify.

Full usage example

import ballerina/log;
import ballerinax/trigger.shopify;

configurable string apiSecretKey = ?;
configurable int listenerPort = ?;

shopify:ListenerConfig listenerConfig = {
apiSecretKey: apiSecretKey
};

listener shopify:Listener shopifyListener = new (listenerConfig, listenerPort);

service shopify:OrdersService on shopifyListener {
remote function onOrdersCreate(shopify:OrderEvent event) returns error? {
log:printInfo("Shopify order created",
id = event.id,
name = event.name,
email = event.email,
total = event.total_price
);
}

remote function onOrdersPaid(shopify:OrderEvent event) returns error? {
log:printInfo("Shopify order paid",
id = event.id,
name = event.name,
total = event.total_price
);
}
}

Supporting types

OrderEvent

FieldTypeDescription
idint?Unique identifier for the order.
namestring?Order name generated from the order number and store settings.
emailstring?Customer email address.
created_atstring?Date and time when the order was created.
updated_atstring?Date and time when the order was last modified.
processed_atstring?Date and time when the order was processed.
currencystring?Three-letter shop currency code.
total_pricestring?Total order price in the shop currency.
subtotal_pricestring?Order subtotal in the shop currency.
total_taxstring?Total tax for the order.
financial_statusstring?Payment status of the order.
fulfillment_statusstring?Fulfillment status of the order.
line_itemsOrderLineItem[]?Line items included in the order.
customerCustomer?Customer associated with the order.

CustomerEvent

FieldTypeDescription
idint?Unique identifier for the customer.
emailstring?Customer email address.
first_namestring?Customer first name.
last_namestring?Customer last name.
phonestring?Customer phone number.
created_atstring?Date and time when the customer was created.
updated_atstring?Date and time when the customer information was last updated.
orders_countint?Number of orders associated with the customer.
total_spentstring?Total amount spent by the customer.
statestring?Customer account state.
accepts_marketingboolean?Whether the customer consented to marketing.
tagsstring?Comma-separated tags attached to the customer.

ProductEvent

FieldTypeDescription
idint?Unique identifier for the product.
titlestring?Product title.
body_htmlstring?Product description in HTML.
vendorstring?Product vendor.
product_typestring?Product type.
created_atstring?Date and time when the product was created.
updated_atstring?Date and time when the product was last updated.
published_atstring?Date and time when the product was published.
handlestring?Human-friendly product handle.
variantsProductVariant[]?Product variants.
optionsProductOption[]?Product options.
imagesProductImage[]?Product images.

FulfillmentEvent

FieldTypeDescription
idint?Unique identifier for the fulfillment.
order_idint?Order associated with the fulfillment.
statusstring?Fulfillment status.
created_atstring?Date and time when the fulfillment was created.
updated_atstring?Date and time when the fulfillment was last updated.
tracking_companystring?Tracking company name, when available.
tracking_numberstring?Tracking number, when available.
tracking_urlstring?Tracking URL, when available.
line_itemsLineItem[]?Fulfilled line items.