Skip to main content

JavaScript SDK Overview

The Medusa JavaScript SDK (@medusajs/js-sdk) is a comprehensive client library for interacting with the Medusa API. It provides a type-safe, modern interface for building commerce applications across various frontend frameworks and environments.

Features

The JavaScript SDK offers powerful features to streamline your development:
  • Type-Safe API: Full TypeScript support with auto-completion and type checking
  • Unified Interface: Consistent API across admin, store, and authentication methods
  • Flexible Authentication: Support for JWT tokens, session-based auth, and OAuth providers
  • Environment Agnostic: Works in browsers, Node.js, and React Native
  • Custom Storage: Configurable token storage with localStorage, sessionStorage, or custom implementations
  • Advanced Networking: Built-in support for Server-Sent Events (SSE) and streaming responses
  • Locale Support: Multi-language support with automatic locale header management
  • Configurable Headers: Global and per-request custom headers including Next.js cache tags

Use Cases

Storefront Development

Build modern e-commerce storefronts with React, Vue, or vanilla JavaScript:

Admin Dashboard

Manage your commerce backend with full admin capabilities:

Mobile Applications

Integrate Medusa into React Native or other mobile frameworks:

Server-Side Rendering (SSR)

Use the SDK in Next.js, Nuxt, or other SSR frameworks:

Architecture

The SDK is organized into several key components:

Main SDK Class

The Medusa class is the entry point that provides access to all SDK functionality:

Admin API

The admin namespace provides access to all administrative operations:
  • product, productVariant, productCollection, productCategory
  • order, draftOrder, claim, exchange, return
  • customer, customerGroup
  • inventory, stockLocation, reservation
  • payment, paymentCollection
  • promotion, campaign
  • region, currency, taxRate, taxRegion
  • user, invite, apiKey
  • And many more…

Store API

The store namespace provides customer-facing operations:
  • product, collection, category
  • cart (create, update, manage items, shipping, complete)
  • order (list, retrieve, transfer)
  • customer (profile, addresses)
  • region
  • payment, fulfillment

Authentication

The auth namespace handles all authentication flows:
  • login() - Authenticate users, customers, or custom actors
  • register() - Register new users
  • logout() - Clear authentication
  • callback() - Handle OAuth callbacks
  • refresh() - Refresh authentication tokens
  • resetPassword() - Initiate password reset
  • updateProvider() - Update authentication credentials

Client

The low-level Client class handles HTTP communication:
  • Automatic header management (auth, locale, publishable key)
  • Request/response transformation
  • Query parameter serialization
  • Error handling with FetchError
  • Server-Sent Events (SSE) support via fetchStream()

Authentication Methods

JWT Authentication (Default)

Store JWT tokens in browser storage:

Session-Based Authentication

Use HTTP-only cookies for enhanced security:

API Key Authentication

Use API keys for server-to-server communication:

Error Handling

The SDK throws FetchError instances for failed requests:

Next Steps

Installation

Learn how to install and initialize the SDK in your project

SDK Methods

Explore available methods and client usage patterns