docs: general improvements and additions (#12296)
This commit is contained in:
@@ -26,3 +26,38 @@ The Medusa application infers the associated sales channels and ensures that onl
|
||||
## How to Create a Publishable API Key?
|
||||
|
||||
To create a publishable API key, either use the [Medusa Admin](!user-guide!/settings/developer/publishable-api-keys) or the [Admin API Routes](!api!/admin#publishable-api-keys).
|
||||
|
||||
---
|
||||
|
||||
## Access Sales Channels in Custom Store API Routes
|
||||
|
||||
If you create an API route under the `/store` prefix, you can access the sales channels associated with the request's publishable API key using the `publishable_key_context` property of the request object.
|
||||
|
||||
For example:
|
||||
|
||||
```ts
|
||||
import { MedusaStoreRequest, MedusaResponse } from "@medusajs/framework/http"
|
||||
import { getVariantAvailability } from "@medusajs/framework/utils"
|
||||
|
||||
export async function GET(
|
||||
req: MedusaStoreRequest,
|
||||
res: MedusaResponse
|
||||
) {
|
||||
const query = req.scope.resolve("query")
|
||||
const sales_channel_ids = req.publishable_key_context.sales_channel_ids
|
||||
|
||||
res.json({
|
||||
sales_channel_id: sales_channel_ids[0],
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
In this example, you retrieve the scope's sales channel IDs using `req.publishable_key_context.sales_channel_ids`, whose value is an array of IDs.
|
||||
|
||||
You can then use these IDs based on your business logic. For example, you can retrieve the sales channels' details using [Query](!docs!/learn/fundamentals/module-links/query).
|
||||
|
||||
<Note title="Tip">
|
||||
|
||||
Notice that the request object's type is `MedusaStoreRequest` instead of `MedusaRequest` to ensure the availability of the `publishable_key_context` property.
|
||||
|
||||
</Note>
|
||||
|
||||
Reference in New Issue
Block a user