Skip to main content

Overview

Medusa supports multiple authentication methods depending on the API and use case:
  • JWT Authentication: For admin users and store customers
  • API Keys: For server-to-server admin operations
  • Session Management: For maintaining authenticated state

Admin Authentication

JWT Token Authentication

Admin users authenticate using JWT tokens obtained through the auth endpoints.

Step 1: Authenticate

Obtain a JWT token by authenticating with email and password:
Response:
string
The JWT token to use for authenticated requests.

Step 2: Make Authenticated Requests

Include the JWT token in the Authorization header:

API Key Authentication

API keys provide a secure way for server-to-server communication with the Admin API.

Creating an API Key

Create an API key through the Admin API:
string
required
A descriptive name for the API key.
string
required
The type of API key. Use "secret" for server-to-server authentication.
Response:
The API key token is only shown once. Store it securely and never expose it in client-side code.

Using API Keys

Include the API key in the x-medusa-access-token header:
Or use it as a query parameter:
Header-based authentication is recommended for better security.

Revoking API Keys

Revoke an API key when it’s no longer needed:
Source: packages/medusa/src/api/admin/api-keys/[id]/revoke/route.ts

Store Authentication

Customer Authentication

Store customers authenticate using JWT tokens.

Step 1: Register a Customer

Create a new customer account:
string
required
The customer’s email address.
string
required
The customer’s password.
string
The customer’s first name.
string
The customer’s last name.
Source: packages/medusa/src/api/store/customers/route.ts:11

Step 2: Authenticate

Obtain a JWT token:
Response:

Step 3: Make Authenticated Requests

Include the JWT token in authenticated store requests:

Session Management

Session Context

Authenticated requests automatically create and maintain session context through the req.auth_context object:
Source: packages/medusa/src/api/admin/customers/route.ts:53

Token Expiration

JWT tokens have an expiration time. When a token expires, clients must re-authenticate to obtain a new token.

Logout

To logout, clients should discard the JWT token. Server-side session invalidation may be implemented through custom middleware.

Security Best Practices

  • Store JWT tokens securely (e.g., httpOnly cookies, secure storage)
  • Never expose API keys in client-side code
  • Use environment variables for API keys in server environments
  • Implement token refresh mechanisms for long-lived sessions
  • Rotate API keys periodically
  • Revoke unused or compromised API keys immediately
  • Always use HTTPS in production to prevent token interception
  • Configure secure cookie flags when using session cookies
  • Create separate API keys for different integrations
  • Use role-based access control (RBAC) to limit permissions
  • Assign API keys to specific sales channels when applicable

Error Handling

Unauthorized (401)

Returned when authentication is required but not provided:

Forbidden (403)

Returned when the authenticated user lacks permissions:

Invalid Credentials

Returned when authentication credentials are incorrect:

Next Steps

Admin API

Start using the Admin API

Store API

Build your storefront with the Store API