Files
medusa-store/www/apps/resources/app/commerce-modules/api-key/links-to-other-modules/page.mdx
Shahed Nasser 009d00f27d docs: redesign table of content (#12647)
* implement toc

* added to projects

* fixes and adapt for references

* added product frontmatter

* remove action menu from 404 pages
2025-05-30 16:55:36 +03:00

149 lines
3.4 KiB
Plaintext

---
products:
- sales channel
---
import { CodeTabs, CodeTab, Table } from "docs-ui"
export const metadata = {
title: `Links between API Key Module and Other Modules`,
}
# {metadata.title}
This document showcases the module links defined between the API Key Module and other Commerce Modules.
## Summary
The API Key Module has the following links to other modules:
<Table>
<Table.Header>
<Table.Row>
<Table.HeaderCell>
First Data Model
</Table.HeaderCell>
<Table.HeaderCell>
Second Data Model
</Table.HeaderCell>
<Table.HeaderCell>
Type
</Table.HeaderCell>
<Table.HeaderCell>
Description
</Table.HeaderCell>
</Table.Row>
</Table.Header>
<Table.Body>
<Table.Row>
<Table.Cell>
[ApiKey](/references/api-key/models/ApiKey)
</Table.Cell>
<Table.Cell>
[SalesChannel](/references/sales-channel/models/SalesChannel) in [Sales Channel Module](../../sales-channel/page.mdx)
</Table.Cell>
<Table.Cell>
Stored - many-to-many
</Table.Cell>
<Table.Cell>
[Learn more](#sales-channel-module)
</Table.Cell>
</Table.Row>
</Table.Body>
</Table>
---
## Sales Channel Module
You can create a publishable API key and associate it with a sales channel. Medusa defines a link between the `ApiKey` and the `SalesChannel` data models.
![A diagram showcasing an example of how data models from the API Key and Sales Channel modules are linked](https://res.cloudinary.com/dza7lstvk/image/upload/v1709812064/Medusa%20Resources/sales-channel-api-key_zmqi2l.jpg)
This is useful to avoid passing the sales channel's ID as a parameter of every request, and instead pass the publishable API key in the header of any request to the Store API route.
Learn more about this in the [Sales Channel Module's documentation](../../sales-channel/publishable-api-keys/page.mdx).
### Retrieve with Query
To retrieve the sales channels of an API key with [Query](!docs!/learn/fundamentals/module-links/query), pass `sales_channels.*` in `fields`:
<CodeTabs group="relation-query">
<CodeTab label="query.graph" value="method">
```ts
const { data: apiKeys } = await query.graph({
entity: "api_key",
fields: [
"sales_channels.*",
],
})
// apiKeys[0].sales_channels
```
</CodeTab>
<CodeTab label="useQueryGraphStep" value="step">
```ts
import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
// ...
const { data: apiKeys } = useQueryGraphStep({
entity: "api_key",
fields: [
"sales_channels.*",
],
})
// apiKeys[0].sales_channels
```
</CodeTab>
</CodeTabs>
### Manage with Link
To manage the sales channels of an API key, use [Link](!docs!/learn/fundamentals/module-links/link):
<CodeTabs group="relation-link">
<CodeTab label="link.create" value="method">
```ts
import { Modules } from "@medusajs/framework/utils"
// ...
await link.create({
[Modules.API_KEY]: {
publishable_key_id: "apk_123",
},
[Modules.SALES_CHANNEL]: {
sales_channel_id: "sc_123",
},
})
```
</CodeTab>
<CodeTab label="createRemoteLinkStep" value="step">
```ts
import { Modules } from "@medusajs/framework/utils"
import { createRemoteLinkStep } from "@medusajs/medusa/core-flows"
// ...
createRemoteLinkStep({
[Modules.API_KEY]: {
publishable_key_id: "apk_123",
},
[Modules.SALES_CHANNEL]: {
sales_channel_id: "sc_123",
},
})
```
</CodeTab>
</CodeTabs>