|
1
|
export const metadata = { |
|
2
|
title: `API Key Concepts`, |
|
3
|
} |
|
4
|
|
|
5
|
# {metadata.title} |
|
6
|
|
|
7
|
In this guide, you’ll learn about the different types of API keys, their expiration and verification. |
|
8
|
|
|
9
|
## API Key Types |
|
10
|
|
|
11
|
There are two types of API keys: |
|
12
|
|
|
13
|
- `publishable`: A public key used in client applications, such as a storefront. |
|
14
|
- This API key is useful for operations that do not require authentication, such as fetching product data or categories. |
|
15
|
- `secret`: A secret key used for authentication and verification purposes, such as an admin user’s authentication token or a password reset token. |
|
16
|
- This API key is useful for operations that require authentication, such as creating orders or managing products as an admin user. |
|
17
|
|
|
18
|
The API key’s type is stored in the `type` property of the [ApiKey data model](/references/api-key/models/ApiKey). |
|
19
|
|
|
20
|
### Default Scopes and Permissions |
|
21
|
|
|
22
|
In your Medusa application, a `publishable` API key is only useful to send requests to the [Store API routes](!api!/store). Learn more about it in the [Publishable API Keys](../../sales-channel/publishable-api-keys/page.mdx) guide. |
|
23
|
|
|
24
|
In addition, a `secret` API key allows you to access the [Admin API routes](!api!/admin) and perform actions as the admin user that the key was created for. The `created_by` property of the [ApiKey data model](/references/api-key/models/ApiKey) indicates the ID of the associated admin user. |
|
25
|
|
|
26
|
To authenticate using a secret API key, pass it with HTTP Basic authentication in the `Authorization` header: |
|
27
|
|
|
28
|
```bash |
|
29
|
curl -X GET http://localhost:9000/admin/products \ |
|
30
|
-H 'Authorization: Basic {secret_api_key}' |
|
31
|
``` |
|
32
|
|
|
33
|
<Note title="Warning" type="warning"> |
|
34
|
|
|
35
|
Secret API keys must be sent using HTTP Basic authentication, not as a Bearer token. Sending a secret API key as `Authorization: Bearer {secret_api_key}` returns a `401` error. |
|
36
|
|
|
37
|
</Note> |
|
38
|
|
|
39
|
--- |
|
40
|
|
|
41
|
## API Key Creation |
|
42
|
|
|
43
|
When using the [Medusa Admin](!user-guide!/settings/developer) or [API routes](!api!/admin/api-keys), only admin users can create API keys. |
|
44
|
|
|
45
|
You can also create API keys in your customizations using the [createApiKeysWorkflow](/references/medusa-workflows/createApiKeysWorkflow). |
|
46
|
|
|
47
|
--- |
|
48
|
|
|
49
|
## API Key Tokens |
|
50
|
|
|
51
|
The API key data model has a `token` property that contains the actual key used for authentication. |
|
52
|
|
|
53
|
This token is created using the `salt` property in the data model, which is a random string generated when the API key is created. The salt is a `64`-character hexadecimal string generated randomly using the `crypto` module in Node.js. |
|
54
|
|
|
55
|
For display purposes, the API key data model also has a `redacted` property that contains the first six characters of the token, followed by `...`, then the last three characters of the token. You can use this property to show the API key in the UI without revealing the full token. |
|
56
|
|
|
57
|
--- |
|
58
|
|
|
59
|
## API Key Expiration |
|
60
|
|
|
61
|
An API key expires when it’s revoked using the [revokeApiKeysWorkflow](/references/medusa-workflows/revokeApiKeysWorkflow). This method will set the following properties in the API key: |
|
62
|
|
|
63
|
- `revoked_at`: The date and time when the API key was revoked. |
|
64
|
- `revoked_by`: The ID of the user who revoked the API key. |
|
65
|
|
|
66
|
The associated token is no longer usable or verifiable. |
|
67
|
|
|
68
|
--- |
|
69
|
|
|
70
|
## Token Verification |
|
71
|
|
|
72
|
To verify a token received as an input or in a request, use the [authenticate method of the module’s main service](/references/api-key/authenticate) which validates the token against all non-expired tokens. |