### Install Dependencies and Start Medusa Server (npm) Source: https://docs.medusajs.com/learn/deployment/general Use this command to install project dependencies and start the Medusa server when using npm. ```bash cd .medusa/server && npm install && npm run start ``` -------------------------------- ### Install Dependencies Source: https://docs.medusajs.com/learn/introduction/from-v1-to-v2 Run the appropriate command to install project dependencies before proceeding with database setup. ```bash yarn ``` ```bash pnpm ``` -------------------------------- ### Install Dependencies and Start Medusa (npm) Source: https://docs.medusajs.com/learn/deployment/general Use this command to install dependencies and start the Medusa application when using npm. It includes running migrations and syncing links. ```bash cd .medusa/server && npm install && npm run predeploy && npm run start ``` -------------------------------- ### Install Dependencies and Start Medusa Server (Yarn) Source: https://docs.medusajs.com/learn/deployment/general Use this command to install project dependencies and start the Medusa server when using Yarn. ```bash cd .medusa/server && yarn install && yarn run start ``` -------------------------------- ### Example: Hello World API Route Source: https://docs.medusajs.com/learn/fundamentals/api-routes/http-methods This example demonstrates how to create a `GET` and `POST` API route in a single `route.ts` file. The `GET` route responds with a greeting message, and the `POST` route also responds with a greeting message. ```APIDOC ## GET /hello-world ### Description Responds with a greeting message for a GET request. ### Method GET ### Endpoint /hello-world ### Response #### Success Response (200) - **message** (string) - A greeting message. #### Response Example { "message": "[GET] Hello world!" } ## POST /hello-world ### Description Responds with a greeting message for a POST request. ### Method POST ### Endpoint /hello-world ### Response #### Success Response (200) - **message** (string) - A greeting message. #### Response Example { "message": "[POST] Hello world!" } ``` -------------------------------- ### Install Dependencies and Start Medusa Server (pnpm) Source: https://docs.medusajs.com/learn/deployment/general Use this command to install project dependencies and start the Medusa server when using pnpm. ```bash cd .medusa/server && pnpm install && pnpm run start ``` -------------------------------- ### Install Dependencies and Start Medusa (Yarn) Source: https://docs.medusajs.com/learn/deployment/general Use this command to install dependencies and start the Medusa application when using Yarn. It includes running migrations and syncing links. ```bash cd .medusa/server && yarn install && yarn predeploy && yarn run start ``` -------------------------------- ### Install Dependencies and Start Medusa (pnpm) Source: https://docs.medusajs.com/learn/deployment/general Use this command to install dependencies and start the Medusa application when using pnpm. It includes running migrations and syncing links. ```bash cd .medusa/server && pnpm install && pnpm run predeploy && pnpm run start ``` -------------------------------- ### Start Medusa frontend Source: https://docs.medusajs.com/learn/installation Start the Medusa storefront development server. ```bash npm run dev ``` -------------------------------- ### Example Migration Script for Blog Module Source: https://docs.medusajs.com/learn/fundamentals/data-models/write-migration This script demonstrates how to create a data migration for the Blog Module. It ensures the module is installed before running a workflow to perform data migration steps. ```typescript import { MedusaModule } from "@medusajs/framework/modules-sdk" import { ExecArgs } from "@medusajs/framework/types" import { BLOG_MODULE } from "../modules/blog" import { createWorkflow } from "@medusajs/framework/workflows-sdk" export default async function migrateBlogData({ container }: ExecArgs) { // Check that the blog module exists if (!MedusaModule.isInstalled(BLOG_MODULE)) { return } await migrateBlogDataWorkflow(container).run({}) } const migrateBlogDataWorkflow = createWorkflow( "migrate-blog-data", () => { // Assuming you have these steps createDefaultSiteStep() assignBlogDataToSiteStep() } ) ``` -------------------------------- ### Example Plugin Configuration Source: https://docs.medusajs.com/learn/fundamentals/framework Demonstrates how to configure a plugin in your Medusa project. Ensure the plugin is installed and imported correctly. ```javascript medusa-config.js module.exports = { plugins: [ { resolve: "medusa-plugin-example", options: { // plugin options }, }, ], }; ``` -------------------------------- ### Start Command for Medusa Application Build Source: https://docs.medusajs.com/learn/deployment/general Specify the start command for your Medusa application's production build. This command assumes dependencies are installed in the '.medusa/server' directory. ```bash yarn pnpm npm ``` -------------------------------- ### Create a GET API Route Source: https://docs.medusajs.com/learn/fundamentals/api-routes Example of creating a GET API route at the `/hello-world` path. This involves creating a `route.ts` file in the appropriate directory and exporting a handler function for the GET method. ```typescript import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"; export const GET = async (req: MedusaRequest, res: MedusaResponse) => { res.json({ message: "Hello World!", }); }; ``` -------------------------------- ### Create User Workflow Example Source: https://docs.medusajs.com/resources/commerce-modules/user This example demonstrates how to create a custom workflow for creating a user, utilizing steps from Medusa's core-flows package. Ensure you have the necessary packages installed. ```typescript import { WorkflowExecutionResult, StepResponse, createWorkflow, } from "@medusajs/medusa"; import { createCustomer } from "@medusajs/medusa/dist/workflows/customer/steps/create-customer"; export const create_user_workflow = createWorkflow( (input: { email: string; first_name: string; last_name: string }) => { const customer = createCustomer({ email: input.email, first_name: input.first_name, last_name: input.last_name }); return new StepResponse(customer.customer); } ); // Example of how to use the workflow: // const result: WorkflowExecutionResult = await create_user_workflow.run({ // input: { // email: "john.doe@example.com", // first_name: "John", // last_name: "Doe", // }, // }); ``` -------------------------------- ### Install the Index Module Source: https://docs.medusajs.com/learn/fundamentals/module-links/index-module Install the Index Module package using npm, yarn, or pnpm. ```bash yarn add @medusajs/index ``` ```bash pnpm add @medusajs/index ``` ```bash npm install @medusajs/index ``` -------------------------------- ### Start Next.js Starter Storefront (npm) Source: https://docs.medusajs.com/learn/installation/docker Use this script to start the Next.js Starter Storefront development server with npm. Ensure you are in the /server/apps/storefront directory. ```shell #!/bin/sh cd /server/apps/storefront echo "Starting Next.js Starter Storefront development server..." npm run dev ``` -------------------------------- ### Example GET API Route Source: https://docs.medusajs.com/learn/debugging-and-testing/testing-tools/integration-tests/api-routes This is an example of a custom GET API route in MedusaJS. It responds with a JSON message. ```typescript import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http" export async function GET( req: MedusaRequest, res: MedusaResponse ){ res.json({ message: "Hello, World!", }) } ``` -------------------------------- ### Create Database and Tables Source: https://docs.medusajs.com/learn/introduction/from-v1-to-v2 Run the database setup command to create the necessary database and tables for your v2 project. This command is available for npm, yarn, and pnpm. ```bash yarn medusa db:setup ``` ```bash pnpm medusa db:setup ``` ```bash npx medusa db:setup ``` -------------------------------- ### Start Next.js Starter Storefront (pnpm) Source: https://docs.medusajs.com/learn/installation/docker Use this script to start the Next.js Starter Storefront development server with pnpm. Ensure you are in the /server/apps/storefront directory. ```shell #!/bin/sh cd /server/apps/storefront echo "Starting Next.js Starter Storefront development server..." pnpm dev ``` -------------------------------- ### List Products with Pagination Parameters Source: https://docs.medusajs.com/api/admin Demonstrates how to list products using query parameters for pagination. Includes `limit` to control the number of items per page and `offset` to skip a certain number of items. ```bash curl "http://localhost:9000/admin/products?limit=5&offset=0" \ -H 'Authorization: Bearer {jwt_token}' ``` -------------------------------- ### Start Next.js Starter Storefront (yarn) Source: https://docs.medusajs.com/learn/installation/docker Use this script to start the Next.js Starter Storefront development server with yarn. Ensure you are in the /server/apps/storefront directory. ```shell #!/bin/sh cd /server/apps/storefront echo "Starting Next.js Starter Storefront development server..." yarn dev ``` -------------------------------- ### Run Book Documentation Locally Source: https://docs.medusajs.com/learn/resources/contribution-guidelines/docs Use this command to start the development server for the Medusa Book documentation project. ```bash yarn dev ``` -------------------------------- ### Example Middlewares Configuration Source: https://docs.medusajs.com/learn/fundamentals/api-routes/middlewares Demonstrates the configuration of multiple middlewares with different matcher patterns and methods. ```typescript 1export default defineMiddlewares({ routes: [ { matcher: "/custom/:id", middlewares: [/* ... */], }, { matcher: "/custom", middlewares: [/* ... */], }, { matcher: "/custom*", method: ["GET"], middlewares: [/* ... */], }, { matcher: "/custom/:id", method: ["GET"], middlewares: [/* ... */], }, ], }) ``` -------------------------------- ### Define a GET API Route Source: https://docs.medusajs.com/learn/debugging-and-testing/testing-tools/integration-tests/api-routes This is an example of a simple GET API route. It responds with a JSON object containing a 'Hello, World!' message. ```typescript import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http" export async function GET( req: MedusaRequest, res: MedusaResponse ){ res.json({ message: "Hello, World!", }) } ``` -------------------------------- ### Start Medusa with Worker Mode (npx) Source: https://docs.medusajs.com/learn/production/worker-mode Use this command to start your Medusa backend in cluster mode with a specified distribution of servers and workers. This example uses npx. ```bash npx medusa start --cluster 4 --servers 25% --workers 75% # Use 4 CPU cores, with 25% as servers and 75% as workers npx medusa start --cluster 4 --servers 1 --workers 3 # Use 4 CPU cores, with 1 as server and 3 as workers npx medusa start --cluster 4 --servers 1 --workers 1 # Use 4 CPU cores, with 1 as server and 1 as worker (the remaining 2 will run in shared mode) ``` -------------------------------- ### Start Medusa with Worker Mode (pnpm) Source: https://docs.medusajs.com/learn/production/worker-mode Use this command to start your Medusa backend in cluster mode with a specified distribution of servers and workers. This example uses pnpm. ```bash pnpm medusa start --cluster 4 --servers 25% --workers 75% # Use 4 CPU cores, with 25% as servers and 75% as workers pnpm medusa start --cluster 4 --servers 1 --workers 3 # Use 4 CPU cores, with 1 as server and 3 as workers pnpm medusa start --cluster 4 --servers 1 --workers 1 # Use 4 CPU cores, with 1 as server and 1 as worker (the remaining 2 will run in shared mode) ``` -------------------------------- ### Start Medusa with Worker Mode (Yarn) Source: https://docs.medusajs.com/learn/production/worker-mode Use this command to start your Medusa backend in cluster mode with a specified distribution of servers and workers. This example uses Yarn. ```bash yarn medusa start --cluster 4 --servers 25% --workers 75% # Use 4 CPU cores, with 25% as servers and 75% as workers yarn medusa start --cluster 4 --servers 1 --workers 3 # Use 4 CPU cores, with 1 as server and 3 as workers yarn medusa start --cluster 4 --servers 1 --workers 1 # Use 4 CPU cores, with 1 as server and 1 as worker (the remaining 2 will run in shared mode) ``` -------------------------------- ### Create Migration File for Blog Module Source: https://docs.medusajs.com/learn/fundamentals/data-models This example shows the structure of a migration file generated for the Blog Module. It includes the necessary import and the `up` and `down` methods for applying and reverting changes. ```typescript import { Migration } from "@medusajs/framework/mikro-orm/migrations" export class Migration20241121103722 extends Migration { async up(): Promise { this.addSql("create table if not exists \"post\" (\"id\" text not null, \"title\" text not null, \"created_at\" timestamptz not null default now(), \"updated_at\" timestamptz not null default now(), \"deleted_at\" timestamptz null, constraint \"post_pkey\" primary key (\"id\"));") } async down(): Promise { this.addSql("drop table if exists \"post\" cascade;") } } ``` -------------------------------- ### Start Next.js Starter Storefront Source: https://docs.medusajs.com/resources/nextjs-starter Run the Next.js Starter storefront using yarn, pnpm, or npm. Ensure your Medusa application is running before starting the storefront. ```bash cd apps/storefront yarn dev ``` ```bash cd apps/storefront pnpm run dev ``` ```bash cd apps/storefront npm run dev ``` -------------------------------- ### Create a GET API Route Source: https://docs.medusajs.com/learn/fundamentals/api-routes Create a file in `src/api/` with the name of your route. Export an async function for each HTTP method you want to support. This example shows a simple GET route. ```typescript import type { MedusaRequest, MedusaResponse, } from "@medusajs/framework/http" export const GET = ( req: MedusaRequest, res: MedusaResponse ) => { res.json({ message: "[GET] Hello world!", }) } ``` -------------------------------- ### Handle GET and POST Requests in API Route Source: https://docs.medusajs.com/learn/fundamentals/api-routes/http-methods This example demonstrates how to create an API route that can handle both GET and POST requests. It exports asynchronous handler functions for each HTTP method. ```typescript import type { MedusaRequest, MedusaResponse, } from "@medusajs/framework/http" export const GET = async ( req: MedusaRequest, res: MedusaResponse ) => { res.json({ message: "[GET] Hello world!", }) } export const POST = async ( req: MedusaRequest, res: MedusaResponse ) => { res.json({ message: "[POST] Hello world!", }) } ``` -------------------------------- ### Start Interactive Learning with Claude Code Source: https://docs.medusajs.com/learn/customization/custom-features Initiate the interactive learning experience by using a specific prompt in Claude Code. This prompt guides the AI to start teaching Medusa concepts. ```bash I want to learn Medusa. ``` -------------------------------- ### Example Prompt for Storefront Development with AI Source: https://docs.medusajs.com/resources/storefront-development This prompt demonstrates how to request the creation of a Next.js ecommerce storefront with specific features and a responsive megamenu. ```bash Build a Next.js ecommerce storefront that uses Medusa as the backend. The storefront should have product listing, product detail, cart, and checkout pages. Add a megamenu to the storefront's header that displays product categories and subcategories. The menu should be responsive and work well on mobile devices. ``` -------------------------------- ### Retrieve Products with Brand Name Starting with 'Acme' Source: https://docs.medusajs.com/learn/fundamentals/module-links/index-module Use the $like operator to filter products whose brand name starts with a specific string. This example retrieves products where the brand name begins with 'Acme'. ```typescript const { data: products, } = await query.index({ entity: "product", fields: ["*", "brand.*"], filters: { brand: { name: { $like: "Acme%", }, }, }, }) ``` -------------------------------- ### Dockerfile with NPM Source: https://docs.medusajs.com/learn/installation/docker This Dockerfile uses npm for dependency management. It includes steps for installing npm and then proceeding with the project setup. ```dockerfile FROM node:20-alpine WORKDIR /server COPY package.json package-lock.json turbo.json ./ COPY apps/backend/package.json ./apps/backend/ COPY apps/storefront/package.json ./apps/storefront/ RUN npm install --legacy-peer-deps COPY . . EXPOSE 9000 5173 8000 ENTRYPOINT ["./start.sh"] ``` -------------------------------- ### Navigate to project directory Source: https://docs.medusajs.com/learn/installation Change into the newly created Medusa project directory. ```bash cd my-medusa-store ``` -------------------------------- ### Module Registration Example Source: https://docs.medusajs.com/learn/fundamentals/modules Shows how to register a module with the Medusa core. ```javascript import { Medusa } from "@medusajs/medusa" const medusa = new Medusa({ // ... modules: [ MyModule ] }) medusa.run() ``` -------------------------------- ### Create Admin User Source: https://docs.medusajs.com/learn/customization/custom-features/api-route Before creating a brand, ensure you have an admin user. Refer to the installation guide if you need to create one. ```bash curl -X POST 'http://localhost:9000/admin/brands' \ -H 'Content-Type: application/json' \ -H 'Authorization: Bearer {token}' \ --data '{ "name": "Acme" }' ``` -------------------------------- ### Run Development Server Source: https://docs.medusajs.com/learn/resources/contribution-guidelines/docs Starts the development server for the Medusa project. Access the documentation site via the provided URLs. ```bash npm run dev ``` ```bash yarn dev ``` -------------------------------- ### Example Loader Function Source: https://docs.medusajs.com/learn/fundamentals/modules/loaders This loader function logs a message when the Medusa application starts. It receives a `container` object to resolve services like the logger. ```typescript import { LoaderOptions, } from "@medusajs/framework/types" export default async function helloWorldLoader({ container, }: LoaderOptions) { const logger = container.resolve("logger") logger.info("\"[HELLO MODULE] Just started the Medusa application!\"") } ``` -------------------------------- ### Run Medusa Backend with Docker Compose Source: https://docs.medusajs.com/learn/installation/docker This command starts the Medusa backend service using Docker Compose. Ensure you have Docker and Docker Compose installed and configured. ```bash docker compose up --build ``` -------------------------------- ### Create Storefront Environment File Source: https://docs.medusajs.com/resources/nextjs-starter Create the local environment file for the storefront from its template. This file will store custom configurations. ```bash cp apps/storefront/.env.template apps/storefront/.env.local ``` -------------------------------- ### Send JSON Response in API Route Source: https://docs.medusajs.com/learn/fundamentals/api-routes/responses Use the `json` method of the `MedusaResponse` object to send a JSON response. This example demonstrates a simple GET request handler. ```typescript import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http" export const GET = async ( req: MedusaRequest, res: MedusaResponse ) => { res.json({ message: "Hello, World!", }) } ``` -------------------------------- ### Start Monorepo Project with NPM Source: https://docs.medusajs.com/resources/create-medusa-app Navigate to the root directory and start your monorepo Medusa project using NPM. ```bash cd ../.. npm run ``` -------------------------------- ### Start Interactive Learning with Claude Code Source: https://docs.medusajs.com/learn/customization/custom-features Initiate the interactive Medusa learning experience in Claude Code by using a specific prompt. This will guide you through building a brands feature. ```bash ❯I want to learn Medusa. ``` -------------------------------- ### Start Medusa backend with migrations Source: https://docs.medusajs.com/learn/installation Start the Medusa backend server and run database migrations. This is useful for setting up the database schema. ```bash npm run migrate && npm run start ``` -------------------------------- ### Start Medusa Application Source: https://docs.medusajs.com/learn/configurations/medusa-config/asymmetric-encryption Start your Medusa application using your preferred package manager. This step is necessary before proceeding with token generation and testing. ```bash yarn dev ``` ```bash pnpm run dev ``` ```bash npm run dev ``` -------------------------------- ### Execute API Key Workflow in API Route Source: https://docs.medusajs.com/resources/commerce-modules/api-key Demonstrates how to run the `createApiKeyWorkflow` within a Medusa API route. This example shows how to access the workflow and execute it to get the API key result. ```typescript import type { MedusaRequest, MedusaResponse, } from "@medusajs/framework/http" import { createApiKeyWorkflow } from "../../workflows/create-api-key" export async function GET( req: MedusaRequest, res: MedusaResponse ) { const { result } = await createApiKeyWorkflow(req.scope) .run() res.send(result) } ``` -------------------------------- ### Fetch Products using useQuery Source: https://docs.medusajs.com/resources/js-sdk Use the configured SDK with the `useQuery` hook from Tanstack Query to send GET requests for fetching product data. This example demonstrates fetching a list of products and displaying their titles. ```tsx import { defineWidgetConfig } from "@medusajs/admin-sdk" import { Button, Container } from "@medusajs/ui" import { useQuery } from "@tanstack/react-query" import { sdk } from "../lib/config" import { DetailWidgetProps, HttpTypes } from "@medusajs/framework/types" const ProductWidget = () => { const { data, isLoading } = useQuery({ queryFn: () => sdk.admin.product.list(), queryKey: ["products"], }) return ( {isLoading && Loading...} {data?.products && (
    {data.products.map((product) => (
  • {product.title}
  • ))}
)}
) } export const config = defineWidgetConfig({ zone: "product.list.before", }) export default ProductWidget ``` -------------------------------- ### Install a Public Plugin Source: https://docs.medusajs.com/learn/fundamentals/plugins/create Use npm, yarn, or pnpm to install a public plugin. Replace @myorg/plugin-name with the actual name of the plugin. ```bash yarn add @myorg/plugin-name ``` ```bash pnpm add @myorg/plugin-name ``` ```bash npm install @myorg/plugin-name ``` -------------------------------- ### Create Medusa Application Source: https://docs.medusajs.com/learn/installation Use this command to create a new Medusa application. Replace `my-medusa-store` with your desired project name. You will be prompted to install the Next.js Starter Storefront. ```bash npx create-medusa-app my-medusa-store ```