SDKs overview

Medusa, an open-source headless commerce platform, offers a suite of SDKs and libraries designed to facilitate interaction with its backend services. These tools are primarily focused on JavaScript and TypeScript environments, reflecting Medusa's core technology stack built on Node.js and TypeScript. The SDKs abstract the underlying RESTful API, providing developers with a more structured and efficient way to integrate commerce functionalities into custom applications.

The primary SDKs enable developers to manage various aspects of an e-commerce store, including product catalogs, customer accounts, orders, and payment flows. They are foundational for building custom storefronts, integrating with third-party services, and extending Medusa's core capabilities. The official SDKs are maintained by the Medusa team, ensuring compatibility and alignment with platform updates. For more general information on SDKs, the Mozilla Developer Network defines an SDK as a collection of tools for building software applications.

Official SDKs by language

Medusa provides official SDKs that simplify development across its ecosystem. These SDKs are maintained by the Medusa team and offer type-safe methods for interacting with the Medusa Store and Admin APIs. The key official SDKs are primarily for JavaScript/TypeScript and React environments, aligning with modern web development practices.

JavaScript/TypeScript

The @medusajs/medusa-js SDK is the core client library for interacting with the Medusa backend. It provides methods for calling both the Storefront API and the Admin API, allowing developers to build both customer-facing applications and administrative tools. This SDK is built with TypeScript, offering type definitions for enhanced developer experience and error prevention.

React

For React-based storefronts and admin dashboards, Medusa offers the medusa-react library. This package provides React hooks and context providers that simplify data fetching and state management when working with the Medusa API. It leverages popular React libraries like TanStack Query (React Query) to manage caching, background updates, and error handling, reducing boilerplate code for common API interactions.

The following table outlines the key official SDKs:

Language/Framework Package Name Install Command (npm) Maturity
JavaScript/TypeScript @medusajs/medusa-js npm install @medusajs/medusa-js Stable
React medusa-react npm install medusa-react @tanstack/react-query Stable

Installation

Installing Medusa's official SDKs typically involves using a package manager such as npm or yarn. The process is straightforward, requiring the appropriate package to be added to your project's dependencies. Below are the installation instructions for the primary official SDKs.

Installing @medusajs/medusa-js

To use the core JavaScript/TypeScript client for direct API interactions, install it using npm or yarn:

npm install @medusajs/medusa-js
# or
yarn add @medusajs/medusa-js

This package provides methods to interact with both the Storefront API and the Admin API. For detailed usage, refer to the Medusa JS Client documentation.

Installing medusa-react

For React-based projects, install medusa-react along with its peer dependency, TanStack Query:

npm install medusa-react @tanstack/react-query
# or
yarn add medusa-react @tanstack/react-query

After installation, you'll need to wrap your React application or component tree with the MedusaProvider and QueryClientProvider to make the hooks available throughout your application. The Medusa React documentation provides comprehensive setup guides.

Quickstart example

This section provides a quickstart example demonstrating how to fetch products using the @medusajs/medusa-js SDK in a JavaScript/TypeScript environment. This example assumes you have a running Medusa backend accessible at http://localhost:9000.

Example: Fetching Products with @medusajs/medusa-js

First, ensure you have @medusajs/medusa-js installed in your project as described in the installation section.

import Medusa from "@medusajs/medusa-js";

const MEDUSA_BACKEND_URL = process.env.MEDUSA_BACKEND_URL || "http://localhost:9000";

const medusaClient = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 });

async function fetchProducts() {
  try {
    const { products, count, offset, limit } = await medusaClient.products.list();
    console.log("Fetched products:", products);
    console.log("Total products:", count);
  } catch (error) {
    console.error("Error fetching products:", error);
  }
}

fetchProducts();

Explanation:

  1. Import Medusa: The Medusa class is imported from the SDK.
  2. Initialize Client: An instance of Medusa is created, pointing to your Medusa backend's URL. The baseUrl is crucial for the client to know where to send API requests.
  3. Call API Method: The medusaClient.products.list() method is called to retrieve a list of products. This method corresponds to the GET /store/products endpoint of the Medusa Store API.
  4. Handle Response: The response contains an array of products, along with pagination metadata like count, offset, and limit.
  5. Error Handling: A try...catch block is used to handle potential errors during the API call.

This example demonstrates a basic interaction. The SDK supports a wide range of operations for products, orders, customers, carts, and more, as detailed in the Medusa JS Client product methods documentation.

Community libraries

While Medusa provides robust official SDKs, the open-source nature of the platform has fostered a community that contributes additional libraries and plugins. These community-driven projects can extend Medusa's functionality, provide integrations with other services, or offer alternative client implementations for different languages or frameworks.

Community libraries often emerge to fill specific niches or to offer solutions that are not yet part of the official roadmap. They can range from payment gateway integrations to analytics tools or starter kits for less common frontend frameworks. While these libraries can be valuable, developers should exercise due diligence when incorporating them into production environments, considering factors like maintenance, documentation, and community support.

Examples of community contributions can often be found on platforms like GitHub, within the Medusa ecosystem's discussion forums, or by exploring the Medusa Plugin documentation which details how to extend the platform. The Medusa community actively shares resources and projects, contributing to a diverse set of tools available for developers building with Medusa.

Developers are encouraged to explore the Medusa GitHub repository and community channels to discover these contributions. When using third-party libraries, checking the project's activity, open issues, and contributor base can help assess its reliability and long-term viability, a common practice in managing software supply chain risks.