12 lines
438 B
TypeScript
12 lines
438 B
TypeScript
import { homedir } from "os"
|
|
import { join } from "path"
|
|
|
|
/** Read the ANTHROPIC_API_KEY from ~/.env. Returns undefined if not found. */
|
|
export async function getApiKey(): Promise<string | undefined> {
|
|
const envFile = Bun.file(join(homedir(), ".env"))
|
|
if (!(await envFile.exists())) return undefined
|
|
|
|
const envContent = await envFile.text()
|
|
return envContent.match(/^(?:export\s+)?ANTHROPIC_API_KEY=["']?([^"'\s]+)["']?/m)?.[1]
|
|
}
|