3.1. Framework Overview

In this chapter, you'll learn about the Medusa Framework and how it facilitates building customizations in your Medusa application.

What is the Medusa Framework?#

All commerce application require some degree of customization. So, it's important to choose a platform that facilitates building those customizations.

When you build customizations with other ecommerce platforms, they require you to pull data through HTTP APIs, run custom logic that span across systems in a separate application, and manually ensure data consistency across systems. This adds significant overhead and slows down development as you spend time managing complex distributed systems.

The Medusa Framework eliminates this overhead by providing powerful low-level APIs and tools that let you build any type of customization directly within your Medusa project. You can build custom features, orchestrate operations and query data seamlessy across systems, extend core functionality, and automate tasks in your Medusa application.

With the Medusa Framework, you can focus your efforts on building meaningful business customizations and continuously delivering new features.

Using the Medusa Framework, you can build customizations like:

Framework Concepts and Tools#


Build Custom Features#

The Medusa Framework allows you to build custom features tailored to your business needs.

To create a custom feature, you can create a module that contains your feature's data models and the logic to manage them. A module is integrated into your Medusa application without side effects.

Then, you can build commerce features and flows in workflows that use your module. By using workflows, you benefit from features like rollback mechanism and retry configuration.

Finally, you can expose your custom feature with API routes that are built on top of your module and workflows.

API Route Example
1import type {2  MedusaRequest,3  MedusaResponse,4} from "@medusajs/framework/http"5import { createPostWorkflow } from "../../../workflows/create-post"6
7type PostRequestBody = {8  title: string9}10
11export const POST = async (12  req: MedusaRequest<PostRequestBody>,13  res: MedusaResponse14) => {15  const { result } = await createPostWorkflow(req.scope)16    .run({17      input: result.validatedBody,18    })19
20  return res.json(result)21}

Examples#

The following tutorials are step-by-step guides that show you how to build custom features using the Medusa Framework.

Product Reviews
Build a product reviews feature in your Medusa application.
Wishlist
Build a wishlist feature in your Medusa application.

Start Learning#

To learn more about the different concepts useful for building custom features, check out the following chapters:

Modules
Workflows
API Routes

Extend Existing Features#

The Medusa Framework is flexible and extensible, allowing you to extend and build on top of existing models and features.

To associate new properties and relations with an existing model, you can create a module with data models that define these additions. Then, you can define a module link that associates two data models from separate modules.

Then, you can hook into existing workflows to perform custom actions as part of existing features and flows. For example, you can create a brand when a product is created.

Workflow Hook Example
1import { createProductsWorkflow } from "@medusajs/medusa/core-flows"2import { StepResponse } from "@medusajs/framework/workflows-sdk"3import { Modules } from "@medusajs/framework/utils"4import { LinkDefinition } from "@medusajs/framework/types"5import { BRAND_MODULE } from "../../modules/brand"6import BrandModuleService from "../../modules/brand/service"7
8createProductsWorkflow.hooks.productsCreated(9  (async ({ products, additional_data }, { container }) => {10    if (!additional_data?.brand_id) {11      return new StepResponse([], [])12    }13
14    const brandModuleService: BrandModuleService = container.resolve(15      BRAND_MODULE16    )17    18    const brand = await brandModuleService.createBrands({19      name: additional_data.brand_name,20    })21  })22)

You can also build custom workflows using your custom module and Medusa's modules, and use existing workflows and steps within your custom workflows.

Examples#

The following tutorials are step-by-step guides that show you how to extend existing features using the Medusa Framework.

Custom Item Pricing
Add products with custom items to the cart.
Loyalty Points System
Reward and allow customers to redeem loyalty points.

Start Learning#

To learn more about the different concepts useful for extending features, check out the following chapters:

Modules
Module Links
Workflow Hooks

Integrate Third-Party Services#

The Medusa Framework provides the tools and infrastructure to build a middleware solution for your commerce ecosystem. You can integrate third-party services, perform operations across systems, and query data from multiple sources.

Orchestrate Operations Across Systems#

The Medusa Framework solves one of the biggest hurdles for ecommerce platforms: orchestrating operations across systems. Medusa has a built-in durable execution engine to help complete tasks that span multiple systems.

You can integrate a third-party service in a module. This module provides an interface to perform operations with the third-party service.

Then, you can build workflows that perform operations across systems. In the workflow, you can use your module to interact with the integrated third-party service.

For example, you can create a workflow that syncs products from your ERP system to your Medusa application.

By using a workflow to manage operations across systems, you benefit from features like rollback mechanism, background long-running execution, retry configuration, and more. This is essential for building a middleware solution that performs operations across systems, as you don't have to worry about data inconsistencies or failures.

You can then execute this workflow at a specific interval using scheduled jobs or when an event occurs using events and subscribers. You can also expose its features to client applications using an API route.

Examples

The following tutorials are step-by-step guides that show you how to orchestrate operations across third-party services using the Medusa Framework.

Migrate Data from Magento
Migrate data from Magento to your Medusa application.
Integrate Third-Party Services
Integrate CMS, Fulfillment, Payment, and other third-party services.

Start Learning

To learn more about the different concepts useful for integrating third-party services, check out the following chapters:

Modules
Workflows
API Routes
Events and Subscribers
Scheduled Jobs

Query Data Across Systems#

Another essential feature for integrating third-party services is querying data across those systems efficiently.

The Framework allows you to build links not only between Medusa data models, but also virtual data models using read-only module links. You can build a module that provides the logic to query data from a third-party service, then create a read-only link between an existing data model and a virtual one from the third-party service.

Then, you can use Query to retrieve a product and its brand from the third-party service in a single query.

Query Example
1const { result } = await query.graph({2  entity: "product",3  fields: ["id", "brand.*"],4  filters: {5    id: "prod_123",6  },7})8
9// result = [{10//   id: "prod_123",11//   brand: {12//     id: "brand_123",13//     name: "Brand 123",14//     product_id: "prod_123"15//   }16//   ...17// }]

Query simplifies the process of retrieving data across systems, as you can retrieve data from multiple sources in a single query.

Examples

The following tutorials are step-by-step guides that show you how to query data across systems using the Medusa Framework.

Integrate Sanity CMS
Query data from third-party services using read-only links.

Start Learning

To learn more about the different concepts useful for querying data across systems, check out the following chapters:

Modules
Read-Only Links
Query

Automate Tasks#

The Medusa Framework provides the tools to automate tasks in your Medusa application. Automation is useful when you want to perform a task periodically, such as syncing data, or when an event occurs, such as sending a confirmation email when an order is placed.

To build the task to be automated, you first create a workflow that contains the task's logic, such as syncing data or sending an email.

Then, you can execute this workflow when an event occurs using a subscriber, or at a specific interval using a scheduled job.

Examples#

The following guides are step-by-step guides that show you how to automate tasks using the Medusa Framework.

Restock Notifications
Send restock notifications to customers when a product is back in stock.
Sync Data from and to ERP
Sync data between your Medusa application and an ERP system.

Start Learning#

To learn more about the different concepts useful for automating tasks, check out the following chapters:

Workflows
Events and Subscribers
Scheduled Jobs

Re-Use Customizations Across Applications#

If you have custom features that you want to re-use across multiple Medusa applications, or you want to publish your customizations for the community to use, you can build a plugin.

A plugin encapsulates your customizations in a single package. The customizations include modules, workflows, API routes, and more.

Diagram showcasing a wishlist plugin installed in a Medusa application

You can then publish that plugin to NPM and install it in any Medusa application. This allows you to re-use your customizations efficiently across multiple projects, or share them with the community.

Examples#

The following tutorials are step-by-step guides that show you how to build plugins using the Medusa Framework.

Wishlist Plugin
Build a wishlist plugin for your Medusa application.
Migrate Data from Magento Plugin
Build a plugin that migrates data from Magento to your Medusa application.

Start Learning#

To learn more about the different concepts useful for building plugins, check out the following chapters:

Plugins
Was this chapter helpful?
Ask Anything
FAQ
What is Medusa?
How can I create a module?
How can I create a data model?
How do I create a workflow?
How can I extend a data model in the Product Module?
Recipes
How do I build a marketplace with Medusa?
How do I build digital products with Medusa?
How do I build subscription-based purchases with Medusa?
What other recipes are available in the Medusa documentation?
Chat is cleared on refresh
Line break
⇧↵