- 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
|
|
| By default, if This option accepts a string to configure the value of
This option is only available after Medusa v2.1.1. |
|
| 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. |
Send Requests to Custom Routes#
The sidebar shows the different methods that you can use to send requests to Medusa's API routes.
To send requests to custom routes, the JS SDK has a client.fetch
method that wraps the JavaScript Fetch API that you can use. The method automatically appends configurations and headers, such as authentication headers, to your request.
For example, to send a request to a custom route at http://localhost:9000/custom
:
The fetch
method accepts as a first parameter the route's path relative to the baseUrl
configuration you passed when you initialized the SDK.
In the second parameter, you can pass an object of request configurations. You don't need to configure the content-type to be JSON, or stringify the body
or query
value, as that's handled by the method.
The method returns a Promise that, when resolved, has the data returned by the request. If the request returns a JSON object, it'll be automatically parsed to a JavaScript object and returned.
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.