- Get Started
- Product
- Resources
- Tools & SDKs
- Framework
- Reference
- Get Started
- Product
- Resources
- Tools & SDKs
- Framework
- Reference
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:
src/admin/lib/config.ts
.JS SDK Configurations#
The Medusa
initializer accepts as a parameter an object with the following properties:
Property | Description | Default |
---|---|---|
| A required string indicating the URL to the Medusa backend. | - |
| 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. | - |
| A string that specifies the user authentication method to use. Possible types are:
| - |
| A string that, when |
|
| A string that, when
|
|
| An object of key-value pairs indicating headers to pass in all requests, where the key indicates the name of the header field. | - |
| A string indicating the admin user's API key. If specified, it's used to send authenticated requests. | - |
| A boolean indicating whether to show debug messages of requests sent in the console. This is useful during development. |
|
| Replace the logger used by the JS SDK to log messages. The logger must be a class or object having the following methods:
| 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:
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:
Learn more in the Next.js documentation.