The WorkOS Node.js SDK provides comprehensive authentication capabilities for both server-side and client-side applications. This guide covers the core authentication concepts and patterns.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/workos/workos-node/llms.txt
Use this file to discover all available pages before exploring further.
Authentication modes
The SDK supports two distinct authentication modes based on your application architecture:Server-side authentication (confidential clients)
Server-side applications that can securely store secrets use an API key for authentication:WORKOS_API_KEY environment variable:
Public client authentication (PKCE)
For applications that cannot securely store secrets (browser apps, mobile apps, Electron apps, CLI tools), initialize with just a client ID:Authentication methods
The SDK provides several authentication methods for different use cases:OAuth code exchange
Exchange an authorization code for access and refresh tokens:workos.ts:331 implementation for details.
Password authentication
Authenticate users with email and password:Magic link authentication
Authenticate users via magic link codes:Refresh token authentication
Exchange a refresh token for a new access token:TOTP authentication
Authenticate users with time-based one-time passwords (multi-factor authentication):Authentication response
All authentication methods return anAuthenticationResponse object:
The
refreshToken is only returned on initial authentication. Subsequent refresh operations return a new access token but may not include a new refresh token unless rotation is enabled.Client ID resolution
The SDK automatically resolves the client ID from multiple sources:- Explicitly passed in method options
- Provided during WorkOS initialization
WORKOS_CLIENT_IDenvironment variable
workos.ts:127:
Authorization headers
The SDK automatically manages authorization headers:- API key authentication: Adds
Authorization: Bearer sk_...to all requests - Access token authentication: Can be passed explicitly for user-scoped operations
workos.ts:203:
Best practices
Use environment variables
Store API keys and client IDs in environment variables rather than hardcoding them in your application.
Use PKCE for public clients
Always use PKCE flow for applications that cannot securely store client secrets.
Implement token refresh
Implement automatic token refresh using refresh tokens to maintain user sessions.
Defense in depth
Consider using PKCE even with confidential clients for additional security (OAuth 2.1 recommendation).