|
1
|
--- |
|
2
|
generate_toc: true |
|
3
|
--- |
|
4
|
|
|
5
|
import { CodeTabs, CodeTab, ChildDocs } from "docs-ui" |
|
6
|
|
|
7
|
export const metadata = { |
|
8
|
title: `API Key Module`, |
|
9
|
} |
|
10
|
|
|
11
|
# {metadata.title} |
|
12
|
|
|
13
|
In this section of the documentation, you will find resources to learn more about the API Key Module and how to use it in your application. |
|
14
|
|
|
15
|
<Note title="Looking for no-code docs?"> |
|
16
|
|
|
17
|
Refer to the [Medusa Admin User Guide](!user-guide!/settings/developer) to learn how to manage publishable and secret API keys using the dashboard. |
|
18
|
|
|
19
|
</Note> |
|
20
|
|
|
21
|
Medusa has API-key related features available out-of-the-box through the API Key Module. A [module](!docs!/learn/fundamentals/modules) is a standalone package that provides features for a single domain. Each of Medusa's commerce features are placed in Commerce Modules, such as this API Key Module. |
|
22
|
|
|
23
|
<Note> |
|
24
|
|
|
25
|
Learn more about why modules are isolated in [this documentation](!docs!/learn/fundamentals/modules/isolation). |
|
26
|
|
|
27
|
</Note> |
|
28
|
|
|
29
|
## API Key Features |
|
30
|
|
|
31
|
- [API Key Types and Management](./concepts/page.mdx): Manage API keys in your store. You can create both publishable and secret API keys for different use cases. |
|
32
|
- [Token Verification](./concepts/page.mdx#token-verification): Verify tokens of secret API keys to authenticate users or actions. |
|
33
|
- [Revoke Keys](./concepts/page.mdx#api-key-expiration): Revoke keys to disable their use permanently. |
|
34
|
- Roll API Keys: Roll API keys by [revoking](/references/api-key/revoke) a key then [re-creating it](/references/api-key/createApiKeys). |
|
35
|
|
|
36
|
--- |
|
37
|
|
|
38
|
## How to Use the API Key Module |
|
39
|
|
|
40
|
In your Medusa application, you build flows around Commerce Modules. A flow is built as a [Workflow](!docs!/learn/fundamentals/workflows), which is a special function composed of a series of steps that guarantees data consistency and reliable roll-back mechanism. |
|
41
|
|
|
42
|
You can build custom workflows and steps. You can also re-use Medusa's workflows and steps, which are provided by the `@medusajs/medusa/core-flows` package. |
|
43
|
|
|
44
|
For example: |
|
45
|
|
|
46
|
export const highlights = [ |
|
47
|
["12", "Modules.API_KEY", "Resolve the module in a step."] |
|
48
|
] |
|
49
|
|
|
50
|
```ts title="src/workflows/create-api-key.ts" highlights={highlights} |
|
51
|
import { |
|
52
|
createWorkflow, |
|
53
|
WorkflowResponse, |
|
54
|
createStep, |
|
55
|
StepResponse, |
|
56
|
} from "@medusajs/framework/workflows-sdk" |
|
57
|
import { Modules } from "@medusajs/framework/utils" |
|
58
|
|
|
59
|
const createApiKeyStep = createStep( |
|
60
|
"create-api-key", |
|
61
|
async ({}, { container }) => { |
|
62
|
const apiKeyModuleService = container.resolve(Modules.API_KEY) |
|
63
|
|
|
64
|
const apiKey = await apiKeyModuleService.createApiKeys({ |
|
65
|
title: "Publishable API key", |
|
66
|
type: "publishable", |
|
67
|
created_by: "user_123", |
|
68
|
}) |
|
69
|
|
|
70
|
return new StepResponse({ apiKey }, apiKey.id) |
|
71
|
}, |
|
72
|
async (apiKeyId, { container }) => { |
|
73
|
const apiKeyModuleService = container.resolve(Modules.API_KEY) |
|
74
|
|
|
75
|
await apiKeyModuleService.deleteApiKeys([apiKeyId]) |
|
76
|
} |
|
77
|
) |
|
78
|
|
|
79
|
export const createApiKeyWorkflow = createWorkflow( |
|
80
|
"create-api-key", |
|
81
|
() => { |
|
82
|
const { apiKey } = createApiKeyStep() |
|
83
|
|
|
84
|
return new WorkflowResponse({ |
|
85
|
apiKey, |
|
86
|
}) |
|
87
|
} |
|
88
|
) |
|
89
|
``` |
|
90
|
|
|
91
|
You can then execute the workflow in your custom API routes, scheduled jobs, or subscribers: |
|
92
|
|
|
93
|
<CodeTabs group="resource-types"> |
|
94
|
<CodeTab label="API Route" value="api-route"> |
|
95
|
|
|
96
|
```ts title="src/api/workflow/route.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports" |
|
97
|
import type { |
|
98
|
MedusaRequest, |
|
99
|
MedusaResponse, |
|
100
|
} from "@medusajs/framework/http" |
|
101
|
import { createApiKeyWorkflow } from "../../workflows/create-api-key" |
|
102
|
|
|
103
|
export async function GET( |
|
104
|
req: MedusaRequest, |
|
105
|
res: MedusaResponse |
|
106
|
) { |
|
107
|
const { result } = await createApiKeyWorkflow(req.scope) |
|
108
|
.run() |
|
109
|
|
|
110
|
res.send(result) |
|
111
|
} |
|
112
|
``` |
|
113
|
|
|
114
|
</CodeTab> |
|
115
|
<CodeTab label="Subscriber" value="subscriber"> |
|
116
|
|
|
117
|
```ts title="src/subscribers/user-created.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports" |
|
118
|
import { |
|
119
|
type SubscriberConfig, |
|
120
|
type SubscriberArgs, |
|
121
|
} from "@medusajs/framework" |
|
122
|
import { createApiKeyWorkflow } from "../workflows/create-api-key" |
|
123
|
|
|
124
|
export default async function handleUserCreated({ |
|
125
|
event: { data }, |
|
126
|
container, |
|
127
|
}: SubscriberArgs<{ id: string }>) { |
|
128
|
const { result } = await createApiKeyWorkflow(container) |
|
129
|
.run() |
|
130
|
|
|
131
|
console.log(result) |
|
132
|
} |
|
133
|
|
|
134
|
export const config: SubscriberConfig = { |
|
135
|
event: "user.created", |
|
136
|
} |
|
137
|
``` |
|
138
|
|
|
139
|
</CodeTab> |
|
140
|
<CodeTab label="Scheduled Job" value="scheduled-job"> |
|
141
|
|
|
142
|
```ts title="src/jobs/run-daily.ts" highlights={[["7"], ["8"]]} |
|
143
|
import { MedusaContainer } from "@medusajs/framework/types" |
|
144
|
import { createApiKeyWorkflow } from "../workflows/create-api-key" |
|
145
|
|
|
146
|
export default async function myCustomJob( |
|
147
|
container: MedusaContainer |
|
148
|
) { |
|
149
|
const { result } = await createApiKeyWorkflow(container) |
|
150
|
.run() |
|
151
|
|
|
152
|
console.log(result) |
|
153
|
} |
|
154
|
|
|
155
|
export const config = { |
|
156
|
name: "run-once-a-day", |
|
157
|
schedule: `0 0 * * *`, |
|
158
|
} |
|
159
|
``` |
|
160
|
|
|
161
|
</CodeTab> |
|
162
|
</CodeTabs> |
|
163
|
|
|
164
|
Learn more about workflows in [this documentation](!docs!/learn/fundamentals/workflows). |
|
165
|
|
|
166
|
--- |
|
167
|
|
|
168
|
<CommerceModuleSections /> |