Use this file to discover all available pages before exploring further.
The WorkOS Node.js SDK provides structured error handling with specific exception types for different error scenarios. This guide covers all exception types, their properties, and how to handle them effectively.
Thrown when an API key is required but not provided.Status: 403When it occurs: When attempting to call a server-side method without initializing the client with an API key.From api-key-required.exception.ts:1:
export class ApiKeyRequiredException extends Error { readonly status = 403; readonly name = 'ApiKeyRequiredException'; readonly path: string; constructor(path: string) { super( `API key required for "${path}". ` + `For server-side apps, initialize with: new WorkOS("sk_..."). ` + `For browser/mobile/CLI apps, use authenticateWithCodeAndVerifier() and authenticateWithRefreshToken() which work without an API key.`, ); this.path = path; }}
Example:
import { ApiKeyRequiredException } from '@workos-inc/node';try { const workos = new WorkOS({ clientId: 'client_...' }); // No API key await workos.organizations.listOrganizations(); // Requires API key} catch (error) { if (error instanceof ApiKeyRequiredException) { console.error('API key required for:', error.path); console.error('Status:', error.status); // 403 }}
Thrown when the API key is invalid or the request cannot be authorized.Status: 401When it occurs: Invalid or expired API key, missing authentication credentials.From unauthorized.exception.ts:3:
export class UnauthorizedException extends Error implements RequestException { readonly status = 401; readonly name = 'UnauthorizedException'; readonly message: string; constructor(readonly requestID: string) { super(); this.message = `Could not authorize the request. Maybe your API key is invalid?`; }}
Example:
import { UnauthorizedException } from '@workos-inc/node';try { const workos = new WorkOS('sk_invalid_key'); await workos.userManagement.getUser('user_123');} catch (error) { if (error instanceof UnauthorizedException) { console.error('Authorization failed'); console.error('Request ID:', error.requestID); // Check your API key in the WorkOS dashboard }}
Thrown when the request is malformed or contains invalid parameters.Status: 400When it occurs: Invalid request parameters, malformed data, validation failures.From bad-request.exception.ts:3:
Thrown when the requested resource cannot be found.Status: 404When it occurs: Accessing a resource that doesn’t exist or has been deleted.From not-found.exception.ts:3:
Thrown when the request conflicts with the current state of the server.Status: 409When it occurs: Attempting to create a resource that already exists, or modifying a resource that has been changed by another request.From conflict.exception.ts:3:
export class ConflictException extends Error implements RequestException { readonly status = 409; readonly name = 'ConflictException'; readonly requestID: string; constructor({ error, message, requestID, }: { error?: string; message?: string; requestID: string; }) { super(); this.requestID = requestID; if (message) { this.message = message; } else if (error) { this.message = `Error: ${error}`; } else { this.message = `An conflict has occurred on the server.`; } }}
Thrown when the request is well-formed but cannot be processed due to semantic errors.Status: 422When it occurs: Business logic validation failures, unmet requirements.From unprocessable-entity.exception.ts:4:
export class UnprocessableEntityException extends Error implements RequestException{ readonly status = 422; readonly name = 'UnprocessableEntityException'; readonly message: string = 'Unprocessable entity'; readonly code?: string; readonly requestID: string; constructor({ code, errors, message, requestID, }: { code?: string; errors?: UnprocessableEntityError[]; message?: string; requestID: string; }) { super(); this.requestID = requestID; if (message) this.message = message; if (code) this.code = code; if (errors) { const requirement = errors.length === 1 ? 'requirement' : 'requirements'; this.message = `The following ${requirement} must be met:\n`; for (const { code } of errors) { this.message = this.message.concat(`\t${code}\n`); } } }}
Example:
import { UnprocessableEntityException } from '@workos-inc/node';try { await workos.userManagement.createUser({ email: 'user@example.com', password: '123', // Too weak });} catch (error) { if (error instanceof UnprocessableEntityException) { console.error('Validation failed:', error.message); console.error('Error code:', error.code); console.error('Request ID:', error.requestID); // Show validation errors to user }}
Thrown when the rate limit for API requests has been exceeded.Status: 429When it occurs: Making too many requests in a short period.From rate-limit-exceeded.exception.ts:7:
export class RateLimitExceededException extends GenericServerException { readonly name = 'RateLimitExceededException'; constructor( message: string, requestID: string, /** * The number of seconds to wait before retrying the request. */ readonly retryAfter: number | null, ) { super(429, message, {}, requestID); }}
Example:
import { RateLimitExceededException } from '@workos-inc/node';async function makeRequestWithRetry() { try { return await workos.userManagement.listUsers(); } catch (error) { if (error instanceof RateLimitExceededException) { console.error('Rate limit exceeded'); console.error('Request ID:', error.requestID); if (error.retryAfter) { console.log(`Retry after ${error.retryAfter} seconds`); await new Promise(resolve => setTimeout(resolve, error.retryAfter * 1000) ); return makeRequestWithRetry(); // Retry } } throw error; }}
Thrown for general server errors that don’t fit other categories.Status: Variable (5xx server errors)When it occurs: Internal server errors, service unavailable, unexpected errors.From generic-server.exception.ts:3:
export class GenericServerException extends Error implements RequestException { readonly name: string = 'GenericServerException'; readonly message: string = 'The request could not be completed.'; constructor( readonly status: number, message: string | undefined, readonly rawData: unknown, readonly requestID: string, ) { super(); if (message) { this.message = message; } }}
Thrown when the response body cannot be parsed as JSON.Status: 500When it occurs: Malformed JSON in API response, network corruption.From parse-error.ts:3:
All exceptions include a requestID property that can be used to debug issues with WorkOS support:
try { await workos.userManagement.getUser('user_123');} catch (error) { if ('requestID' in error) { console.error(`Request ID for support: ${error.requestID}`); }}