|
1
|
--- |
|
2
|
tags: |
|
3
|
- storefront |
|
4
|
--- |
|
5
|
|
|
6
|
export const metadata = { |
|
7
|
title: `Connect Your Storefront to Medusa`, |
|
8
|
} |
|
9
|
|
|
10
|
# {metadata.title} |
|
11
|
|
|
12
|
In this guide, you’ll learn how to send requests from your storefront application to the Medusa application. |
|
13
|
|
|
14
|
## Connect to the Medusa Application |
|
15
|
|
|
16
|
To send requests from the storefront to the Medusa application’s Store API Routes, you have two options: |
|
17
|
|
|
18
|
- **For JavaScript frameworks**: use Medusa’s [JS SDK](../../js-sdk/page.mdx) in any JavaScript framework. This NPM package facilitates interacting with the backend’s REST APIs. All Storefront Development guides use the JS SDK. |
|
19
|
- **For other frontend technologies**: interact directly with the Medusa application by sending requests to its [Store REST APIs](https://docs.medusajs.com/api/store). |
|
20
|
|
|
21
|
### Set Up the JS SDK |
|
22
|
|
|
23
|
To set up the JS SDK in your storefront application, install the `@medusajs/js-sdk` package with the types package: |
|
24
|
|
|
25
|
```bash npm2yarn |
|
26
|
npm install @medusajs/js-sdk@latest @medusajs/types@latest |
|
27
|
``` |
|
28
|
|
|
29
|
Then, create a file that initializes the JS SDK client. For example, create the file `src/lib/sdk.ts` with the following content: |
|
30
|
|
|
31
|
```ts title="src/lib/sdk.ts" |
|
32
|
import Medusa from "@medusajs/js-sdk" |
|
33
|
|
|
34
|
let MEDUSA_BACKEND_URL = "http://localhost:9000" |
|
35
|
|
|
36
|
if (process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL) { |
|
37
|
MEDUSA_BACKEND_URL = process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL |
|
38
|
} |
|
39
|
|
|
40
|
export const sdk = new Medusa({ |
|
41
|
baseUrl: MEDUSA_BACKEND_URL, |
|
42
|
debug: process.env.NODE_ENV === "development", |
|
43
|
publishableKey: process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY, |
|
44
|
}) |
|
45
|
``` |
|
46
|
|
|
47
|
Where: |
|
48
|
|
|
49
|
- `NEXT_PUBLIC_MEDUSA_BACKEND_URL` is the URL of your Medusa application. Based on your storefront framework, make sure the environment variable is accessible in the browser. For example, in Next.js, prefix the variable name with `NEXT_PUBLIC_`. |
|
50
|
- `NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY` is the publishable API key of your storefront. Learn more in the [Publishable API Keys](../publishable-api-keys/page.mdx) guide. |
|
51
|
|
|
52
|
You can then import the `sdk` instance from `src/lib/sdk.ts` and use it to send requests to the Medusa application. For example: |
|
53
|
|
|
54
|
```ts |
|
55
|
import { sdk } from "@/lib/sdk" |
|
56
|
|
|
57
|
sdk.store.product.list() |
|
58
|
.then(({ products }) => { |
|
59
|
console.log(products) |
|
60
|
}) |
|
61
|
``` |
|
62
|
|
|
63
|
Refer to the [JS SDK reference](../../js-sdk/page.mdx) to learn more about using the SDK to interact with the Medusa application. |
|
64
|
|
|
65
|
--- |
|
66
|
|
|
67
|
## Configure Store CORS in Medusa |
|
68
|
|
|
69
|
The Medusa application’s API routes are guarded by a CORS middleware. Make sure to set the `storeCors` property of the `http` configuration in `medusa-config.ts` to the storefront’s URL. |
|
70
|
|
|
71
|
For example: |
|
72
|
|
|
73
|
```ts title="medusa-config.ts" badgeLabel="Medusa Application" badgeColor="green" |
|
74
|
module.exports = defineConfig({ |
|
75
|
projectConfig: { |
|
76
|
http: { |
|
77
|
storeCors: "http://localhost:3000,http://localhost:8000", |
|
78
|
// ... |
|
79
|
}, |
|
80
|
}, |
|
81
|
// ... |
|
82
|
}) |
|
83
|
``` |
|
84
|
|
|
85
|
Refer to the [Medusa configuration](!docs!/learn/configurations/medusa-config#httpstorecors) guide to learn more about configuring CORS in the Medusa application. |
|
86
|
|
|
87
|
--- |
|
88
|
|
|
89
|
## Add Search to Your Storefront |
|
90
|
|
|
91
|
The JS SDK sends requests to Medusa’s Store API routes, which is enough for listing and filtering by a field’s exact value. For a search experience with full-text search, facets, and sorting, use the [InstantSearch adapter](../../instantsearch/page.mdx) instead. It’s the recommended approach since [Medusa v2.21.1](https://github.com/medusajs/medusa/releases/tag/v2.21.1), and it requires the [Search Module](../../infrastructure-modules/search/page.mdx) configured with a provider in your Medusa application. |
|
92
|
|
|
93
|
[InstantSearch](https://www.algolia.com/doc/guides/building-search-ui/what-is-instantsearch/js/) is a family of UI widget libraries, available for React, Vue, Angular, and plain JavaScript. The `@medusajs/instantsearch-adapter` package provides the search client that connects those widgets to Medusa: it converts each widget’s request into a Medusa search query, sends it to the `/store/search` API route, and converts the results back. |
|
94
|
|
|
95
|
The adapter reuses the SDK instance you created above, so it sends the publishable API key and other headers the same way. For example, create the file `src/lib/search-client.ts` with the following content: |
|
96
|
|
|
97
|
```ts title="src/lib/search-client.ts" |
|
98
|
import { |
|
99
|
createInstantSearchAdapter, |
|
100
|
} from "@medusajs/instantsearch-adapter" |
|
101
|
import { sdk } from "./sdk" |
|
102
|
|
|
103
|
export const PRODUCT_INDEX_NAME = "product" |
|
104
|
|
|
105
|
export const { searchClient } = createInstantSearchAdapter({ |
|
106
|
sdk, |
|
107
|
path: "/store/search", |
|
108
|
}) |
|
109
|
``` |
|
110
|
|
|
111
|
You then pass `searchClient` and `PRODUCT_INDEX_NAME` to an `InstantSearch` provider, and add the widgets for the search box, results, filters, sorting, and pagination inside it. |
|
112
|
|
|
113
|
Refer to the [InstantSearch Adapter](../../instantsearch/page.mdx) guide to install the adapter and build the search interface, and to the [Filtering, Sorting, and Pagination](../../instantsearch/examples/filtering-sorting-pagination/page.mdx) guide to add refinements to the results. |