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:The JWT token to use for authenticated requests.
Step 2: Make Authenticated Requests
Include the JWT token in theAuthorization 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:A descriptive name for the API key.
The type of API key. Use
"secret" for server-to-server authentication.Using API Keys
Include the API key in thex-medusa-access-token header:
Header-based authentication is recommended for better security.
Revoking API Keys
Revoke an API key when it’s no longer needed: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:The customer’s email address.
The customer’s password.
The customer’s first name.
The customer’s last name.
packages/medusa/src/api/store/customers/route.ts:11
Step 2: Authenticate
Obtain a JWT token: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 thereq.auth_context object:
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
Secure Token Storage
Secure Token Storage
- 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
Token Rotation
Token Rotation
- Implement token refresh mechanisms for long-lived sessions
- Rotate API keys periodically
- Revoke unused or compromised API keys immediately
HTTPS Only
HTTPS Only
- Always use HTTPS in production to prevent token interception
- Configure secure cookie flags when using session cookies
Scope Limitations
Scope Limitations
- 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