Medusa JS SDK

In this documentation, you'll learn how to install and use Medusa's JS SDK.

What is Medusa JS SDK?#

Medusa's JS SDK is a library to easily send requests to your Medusa application. You can use it in your admin customizations or custom storefronts.


How to Install Medusa JS SDK?#

The Medusa JS SDK is available in your Medusa application by default. So, you don't need to install it before using it in your admin customizations.

To install the Medusa JS SDK in other projects, such as a custom storefront, run the following command:

You install two libraries:

  • @medusajs/js-sdk: the Medusa JS SDK.
  • @medusajs/types: Medusa's types library, which is useful if you're using TypeScript in your development.

Setup JS SDK#

In your project, create the following config.ts file:

NoteFor admin customizations, create this file at src/admin/lib/config.ts .

JS SDK Configurations#

The Medusa initializer accepts as a parameter an object with the following properties:

PropertyDescriptionDefault

baseUrl

A required string indicating the URL to the Medusa backend.

-

publishableKey

A string indicating the publishable API key to use in the storefront. You can retrieve it from the Medusa Admin.

This is required for storefront applications. Otherwise, all requests will fail.

-

auth.type

A string that specifies the user authentication method to use.

Possible types are:

  • session: The user is authenticated with a cookie session.
  • jwt: The user is authenticated with a JWT token that's passed in the Bearer authorization header.

-

auth.jwtTokenStorageKey

A string that, when auth.type is jwt, specifies the key of the JWT token in the storage specified in the auth.jwtTokenStorageMethod configuration.

medusa_auth_token

auth.jwtTokenStorageMethod

A string that, when auth.type is jwt, specifies where the JWT token is stored. Possible values are:

  • local for the Local Storage.
  • session for the Session Storage.
  • memory to store it within the SDK for the current application's runtime.

local

globalHeaders

An object of key-value pairs indicating headers to pass in all requests, where the key indicates the name of the header field.

-

apiKey

A string indicating the admin user's API key. If specified, it's used to send authenticated requests.

-

debug

A boolean indicating whether to show debug messages of requests sent in the console. This is useful during development.

false

logger

Replace the logger used by the JS SDK to log messages. The logger must be a class or object having the following methods:

  • error: A function that accepts an error message to log.
  • warn: A function that accepts a warning message to log.
  • info: A function that accepts an info message to log.
  • debug: A function that accepts a debug message to log.

JavaScript's console is used by default.


Medusa JS SDK Tips#

Use Tanstack (React) Query in Admin Customizations#

In admin customizations, use Tanstack Query with the JS SDK to send requests to custom or existing API routes.

Tanstack Query is installed by default in your Medusa application.

Use the configured SDK with the useQuery Tanstack Query hook to send GET requests, and useMutation hook to send POST or DELETE requests.

For example:

Refer to Tanstack Query's documentation to learn more about sending Queries and Mutations.

Cache in Next.js Projects#

Every method of the SDK that sends requests accepts as a last parameter an object of key-value headers to pass in the request.

In Next.js storefronts or projects, pass the next.tags header in the last parameter for data caching.

For example:

Code
1sdk.store.product.list({}, {2  next: {3    tags: ["products"]4  }5})

The tags property accepts an array of tags that the data is cached under.

Then, to purge the cache later, use Next.js's revalidateTag utility:

Code
1import { revalidateTag } from "next/cache";2
3// ...4
5revalidateTag("products")

Learn more in the Next.js documentation.

Was this page helpful?
Edit this page