Create a WorkOS client instance. The SDK offers multiple initialization methods:
Environment Variables
String Parameter
Options Object
Public Client (PKCE)
import { WorkOS } from '@workos-inc/node';// Automatically reads WORKOS_API_KEY and WORKOS_CLIENT_ID from environmentconst workos = new WorkOS();
This is the recommended approach for server-side applications.
import { WorkOS } from '@workos-inc/node';// Pass API key directly as stringconst workos = new WorkOS('sk_test_1234567890');
import { WorkOS } from '@workos-inc/node';// Pass options object with API key and client IDconst workos = new WorkOS({ apiKey: 'sk_test_1234567890', clientId: 'client_1234567890',});
import { WorkOS } from '@workos-inc/node';// For browser/mobile apps - no API key neededconst workos = new WorkOS({ clientId: 'client_1234567890',});
Use this for applications that cannot securely store an API key (mobile apps, CLI tools, browser apps).
For applications that cannot securely store a secret (mobile, CLI, desktop apps), use PKCE:
import { WorkOS } from '@workos-inc/node';const workos = new WorkOS({ clientId: 'client_1234567890' });// Generate auth URL with PKCEconst { url, codeVerifier } = await workos.userManagement.getAuthorizationUrlWithPKCE({ provider: 'authkit', redirectUri: 'myapp://callback', clientId: workos.clientId!, });// Store codeVerifier securely (e.g., iOS Keychain, Android Keystore)// Redirect user to url// After callback, exchange code for tokensconst { accessToken, refreshToken } = await workos.userManagement.authenticateWithCode({ code: authorizationCode, codeVerifier, // Retrieved from secure storage clientId: workos.clientId!, });
For PKCE flows, you must store the codeVerifier securely on-device between generating the authorization URL and handling the callback. The verifier must survive app restarts during the authentication flow.