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
TheMedusa class is the entry point that provides access to all SDK functionality:
Admin API
Theadmin namespace provides access to all administrative operations:
product,productVariant,productCollection,productCategoryorder,draftOrder,claim,exchange,returncustomer,customerGroupinventory,stockLocation,reservationpayment,paymentCollectionpromotion,campaignregion,currency,taxRate,taxRegionuser,invite,apiKey- And many more…
Store API
Thestore namespace provides customer-facing operations:
product,collection,categorycart(create, update, manage items, shipping, complete)order(list, retrieve, transfer)customer(profile, addresses)regionpayment,fulfillment
Authentication
Theauth namespace handles all authentication flows:
login()- Authenticate users, customers, or custom actorsregister()- Register new userslogout()- Clear authenticationcallback()- Handle OAuth callbacksrefresh()- Refresh authentication tokensresetPassword()- Initiate password resetupdateProvider()- Update authentication credentials
Client
The low-levelClient 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 throwsFetchError 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