-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathapi-key.ts
More file actions
37 lines (29 loc) · 988 Bytes
/
api-key.ts
File metadata and controls
37 lines (29 loc) · 988 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* API key resolution for management commands.
*
* Priority chain:
* 1. --api-key flag
* 2. WORKOS_API_KEY environment variable
* 3. Active environment's stored API key
*/
import { getActiveEnvironment } from './config-store.js';
import { exitWithError } from '../utils/output.js';
const DEFAULT_BASE_URL = 'https://api.workos.com';
export interface ApiKeyOptions {
apiKey?: string;
}
export function resolveApiKey(options?: ApiKeyOptions): string {
if (options?.apiKey) return options.apiKey;
const envVar = process.env.WORKOS_API_KEY;
if (envVar) return envVar;
const activeEnv = getActiveEnvironment();
if (activeEnv?.apiKey) return activeEnv.apiKey;
exitWithError({
code: 'no_api_key',
message: 'No API key configured. Run `workos env add` to configure an environment, or set WORKOS_API_KEY.',
});
}
export function resolveApiBaseUrl(): string {
const activeEnv = getActiveEnvironment();
return activeEnv?.endpoint || DEFAULT_BASE_URL;
}