Raw
1 ---
2 tags:
3 - publishable api key
4 - api key
5 - storefront
6 - sales channel
7 products:
8 - api key
9 - sales channel
10 ---
11
12 export const metadata = {
13 title: `Use a Publishable API Key in the Storefront`,
14 }
15
16 # {metadata.title}
17
18 In this guide, you'll learn how to create and use the publishable API key in your storefront to interact with the Medusa application's Store API routes.
19
20 ## What is a Publishable API Key?
21
22 When sending requests to the `/store` API routes, you must pass a publishable API key in the header of your request.
23
24 Publishable API keys specify the scope of a request. Admin users create them, and they can only be used on the client-side, such as in a storefront.
25
26 Publishable API keys are associated with sales channels. Then, when the publishable API key is passed in the header of a request, the Medusa application automatically infers what sales channel is being used.
27
28 ---
29
30 ## How to Create a Publishable API Key?
31
32 You create a publishable API key either using the [Medusa Admin](!user-guide!/settings/developer/publishable-api-keys) or the [Admin API routes](!api!/admin/api-keys/create-api-key).
33
34 For example, using the [Create API Key API Route](!api!/admin/api-keys/create-api-key):
35
36 ```bash
37 curl -X POST 'http://localhost:9000/admin/api-keys' \
38 -H 'Authorization: Bearer {token}' \
39 -H 'Content-Type: application/json' \
40 --data-raw '{
41 "title": "Storefront Key",
42 "type": "publishable"
43 }'
44 ```
45
46 <Note title="Tip">
47
48 Learn how to send authenticated admin requests in the [Admin API reference](!api!/admin/authentication)
49
50 </Note>
51
52 ### Add Sales Channels to the Publishable API Key's Scope
53
54 To add sales channels to the publishable API key's scope, you can either use the [Medusa Admin](!user-guide!/settings/developer/publishable-api-keys#manage-publishable-api-keys-sales-channels), or send a request to the [Manage Sales Channels API route](!api!/admin/api-keys/manage-sales-channels):
55
56 ```bash
57 curl -X POST 'http://localhost:9000/admin/api-keys/apk_123/sales-channels' \
58 -H 'Authorization: Bearer {token}' \
59 -H 'Content-Type: application/json' \
60 --data-raw '{
61 "add": ["sc_123"]
62 }'
63 ```
64
65 Make sure to replace `apk_123` with the ID of the publishable API key, and `sc_123` with the ID of the sales channel.
66
67 ---
68
69 ## How to Use Publishable API Key in Storefront Requests
70
71 In your storefront, pass the `x-publishable-api-key` in the header of all your requests to the Medusa application.
72
73 For example:
74
75 ```bash
76 curl '{backend_url}/store/products' \
77 -H 'x-publishable-api-key: {your_publishable_api_key}'
78 ```
79
80 Where `{your_publishable_api_key}` is the token of your publishable API key.
81
82 ### Using the JS SDK
83
84 When using the JS SDK, set the publishable API key as an environment variable and pass it to the JS SDK's configurations.
85
86 For example:
87
88 export const highlights = [
89 ["12", "NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY", "An environment variable that holds the publishable API key's token."]
90 ]
91
92 ```ts
93 import Medusa from "@medusajs/js-sdk"
94
95 let MEDUSA_BACKEND_URL = "http://localhost:9000"
96
97 if (process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL) {
98 MEDUSA_BACKEND_URL = process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL
99 }
100
101 export const sdk = new Medusa({
102 baseUrl: MEDUSA_BACKEND_URL,
103 debug: process.env.NODE_ENV === "development",
104 publishableKey: process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY,
105 })
106 ```
107
108 Where `NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY` is an environment variable that holds your publishable API key's token. 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_`.
109
110 The JS SDK will now take care of passing the publishable API key in the header of all requests to the Medusa application.