Configure and manage outgoing webhooks
Add webhook functionality to your Medusa e-commerce server, allowing you to send real-time notifications to external services when specific events occur in your system. The plugin seamlessly integrates with Medusa's event system and provides a simple way to manage webhooks for various events.
1npm install @lambdacurry/medusa-webhooks2# or3yarn add @lambdacurry/medusa-webhooks1const plugins = [2 // ... other plugins3 {4 resolve: "@lambdacurry/medusa-webhooks",5 options: {6 // Add here the subcribers you will define7 subscriptions: ["product.created", "product.updated"],8 },9 },10];yarn medusa db:migrateThe plugin provides three different workflows for handling webhooks:
Here's an example of how to use these workflows in your subscriber:
1import {2 SubscriberArgs,3 SubscriberConfig,4} from "@medusajs/framework/subscribers";5import {6 getWebhooksSubscriptionsWorkflow,7 sendWebhooksEventsWorkflow,8 fullWebhooksSubscriptionsWorkflow,9} from "@lambdacurry/medusa-webhooks/workflows";10
11export const config: SubscriberConfig = {12 event: ["product.created", "product.updated"],13 context: {14 subscriberId: "product-added",15 },16};17
18export default async function handleProductAdded({19 event: { name },20 container,21}: SubscriberArgs<{ id: string }>): Promise<void> {22 const query = container.resolve("query");23 const logger = container.resolve("logger");24
25 // Fetch product data26 const { data: productResult } = await query.graph({27 entity: "product",28 fields: ["*"],29 });30
31 const product = productResult[0];32
33 if (!product) {34 logger.error("Product not found");35 return;36 }37
38 // Option 1: Use the full workflow (recommended for most cases)39 const fullResult = await fullWebhooksSubscriptionsWorkflow(container).run({40 input: {41 eventName: name,42 eventData: product,43 },44 });45
46 // Option 2: Use separate workflows for more control47 const { results: webhooks } = await getWebhooksSubscriptionsWorkflow(48 container49 ).run({50 input: {51 eventName: name,52 eventData: product,53 },54 });55
56 const sendResult = await sendWebhooksEventsWorkflow(container).run({57 input: {58 webhooks,59 eventData: product,60 },61 });62
63 console.log(fullResult);64 // or65 console.log(sendResult);66}To contribute to this plugin:
Please read more about how to run the plugin in dev mode: https://docs.medusajs.com/learn/fundamentals/plugins/create
MIT License