@commercejs/core
Unified Commerce Orchestration Engine — createCommerce() wires adapters, payments, events, and webhooks into a single entry point.

Overview
@commercejs/core is the orchestration layer that ties the CommerceJS ecosystem together. It takes a CommerceAdapter and optional payment providers, notification providers, and analytics providers — then exposes a single API surface for all commerce operations. Events are emitted automatically, webhooks are dispatched, and domain calls are capability-checked at runtime.
Install
npm install @commercejs/core @commercejs/types
Quick Start
import { createCommerce } from '@commercejs/core'
import { createSallaAdapter } from '@commercejs/adapter-salla'
import { TapPaymentProvider } from '@commercejs/payment-tap'
const commerce = createCommerce({
adapter: createSallaAdapter({ accessToken: process.env.SALLA_TOKEN! }),
payments: {
tap: new TapPaymentProvider({
secretKey: process.env.TAP_SECRET_KEY!,
publishableKey: process.env.TAP_PUBLISHABLE_KEY!,
}),
},
defaultPayment: 'tap',
})
const products = await commerce.getProducts({ query: 'shirt' })
const cart = await commerce.createCart()
commerce.events.on('order.created', ({ order }) => {
console.log('New order:', order.id)
})
Multi-Adapter Composition
For advanced setups, compose multiple adapters with orchestrator factories:
import { createCompositeOrchestrator, withPlatformFallback } from '@commercejs/core'
const orchestrator = createCompositeOrchestrator({
sources: {
salla: sallaAdapter,
platform: platformAdapter,
},
mapping: {
catalog: 'salla',
store: 'salla',
cart: 'platform',
},
})
const full = withPlatformFallback(sallaAdapter, platformAdapter)
Notification & Analytics Providers
Register providers to automatically react to commerce events:
import { createResendProvider } from '@commercejs/notification-resend'
import { createGA4Provider } from '@commercejs/analytics-ga'
const commerce = createCommerce({
adapter,
notifications: {
resend: createResendProvider({ apiKey: 're_...', from: 'store@example.com' }),
},
notificationRules: [
{
event: 'order.created',
channel: 'email',
provider: 'resend',
buildMessage: (payload) => ({
to: payload.order.customer.email,
subject: 'Order confirmed',
}),
},
],
analytics: [
createGA4Provider({ measurementId: 'G-XXXXXXXXXX' }),
],
})
Configuration
adapter | CommerceAdapter | ✅ | Platform adapter to delegate domain calls to |
payments | Record<string, PaymentProvider> | — | Payment providers keyed by ID |
defaultPayment | string | — | Default payment provider key |
webhooks | WebhookEndpoint[] | — | Webhook delivery targets |
notifications | Record<string, NotificationProvider> | — | Notification providers keyed by ID |
notificationRules | NotificationRule[] | — | Event → notification routing rules |
analytics | AnalyticsProvider[] | — | Analytics providers (receive all events) |
fetch | typeof fetch | — | Custom fetch for HTTP calls |
sign | (payload, secret) => string | — | Custom webhook signing function |
API
Catalog
getProduct(params) | Get a single product by ID |
getProducts(params) | Search/list products |
getCategories(params?) | List categories |
getBrands() | List brands |
Cart
createCart() | Create a new cart |
getCart(cartId) | Retrieve a cart |
addToCart(cartId, item) | Add item to cart |
updateCartItem(cartId, itemId, qty) | Update item quantity |
removeFromCart(cartId, itemId) | Remove item from cart |
Checkout
getShippingMethods(cartId) | List available shipping methods |
setShippingAddress(cartId, address) | Set shipping address |
setBillingAddress(cartId, address) | Set billing address |
setShippingMethod(cartId, methodId) | Select shipping method |
getPaymentMethods(cartId) | List payment methods |
setPaymentMethod(cartId, methodId) | Select payment method |
placeOrder(cartId) | Complete checkout and place order |
Customer
login(email, password) | Authenticate customer |
register(input) | Create new customer |
getCustomer() | Get current customer |
logout() | End session |
getAddresses() | List saved addresses |
addAddress(address) | Add new address |
Payments
createPayment(input, providerId?) | Create payment session |
getPayment(sessionId, providerId) | Retrieve payment status |
refundPayment(input, providerId) | Process refund |
Event Bus
commerce.events.on('product.viewed', handler)
commerce.events.on('cart.item.added', handler)
commerce.events.on('order.created', handler)
commerce.events.on('*', wildcardHandler)
commerce.events.off('order.created', handler)
Orchestrator Factories
createOrchestrator(config) | Wrap a single adapter as an orchestrator |
createCompositeOrchestrator(config) | Merge domains from multiple adapters |
withPlatformFallback(primary, fallback) | Fill adapter gaps with a fallback |
Exports
createCommerce | Function | Main factory function |
CommerceConfig | Type | Configuration interface |
CommerceInstance | Type | Return type of createCommerce() |
createOrchestrator | Function | Single adapter → orchestrator |
createCompositeOrchestrator | Function | Multi-adapter composition |
withPlatformFallback | Function | Gap-filling fallback |
CommerceEventBus | Class | Typed event emitter |
createWebhookDispatcher | Function | Webhook delivery engine |
Documentation
Full docs at commerce.js.org
License
MIT