<# .SYNOPSIS Ask for the Anthropic API key and store it as a user environment variable. .DESCRIPTION Exists so nobody has to paste a command containing a placeholder. Telling someone to run setx ANTHROPIC_API_KEY "" invites the placeholder being pasted verbatim, and the angle brackets and quotes can leave PowerShell sitting at a continuation prompt with nothing working. Run it with no arguments: powershell -ExecutionPolicy Bypass -File scripts\set_api_key.ps1 The key is read here and handed straight to setx. It is not written to any file in this project, not logged, and not echoed to the screen. #> [CmdletBinding()] param( [switch]$Check ) $ErrorActionPreference = 'Stop' function Test-KeyShape([string]$k) { if ([string]::IsNullOrWhiteSpace($k)) { return 'empty' } if ($k -match '\.\.\.') { return 'placeholder' } if ($k -match '[<>]') { return 'placeholder' } if ($k.Trim().Length -lt 25) { return 'tooshort' } if (-not $k.StartsWith('sk-ant-')) { return 'prefix' } return 'ok' } # --- --Check mode: report what is currently set, without revealing it ---- if ($Check) { $existing = [Environment]::GetEnvironmentVariable('ANTHROPIC_API_KEY', 'User') if (-not $existing) { $existing = [Environment]::GetEnvironmentVariable('ANTHROPIC_API_KEY', 'Process') } $shape = Test-KeyShape $existing switch ($shape) { 'empty' { Write-Output "No ANTHROPIC_API_KEY is set." } 'placeholder' { Write-Output "ANTHROPIC_API_KEY is set to a PLACEHOLDER, not a real key. Run this script without -Check to fix it." } 'tooshort' { Write-Output "ANTHROPIC_API_KEY is set but is too short to be a real key." } 'prefix' { Write-Output "ANTHROPIC_API_KEY is set but does not start with 'sk-ant-'." } 'ok' { Write-Output ("ANTHROPIC_API_KEY looks valid (starts {0}, {1} characters)." -f $existing.Substring(0,11), $existing.Trim().Length) } } return } Write-Output "" Write-Output "Anthropic API key setup" Write-Output "=======================" Write-Output "" Write-Output "Get a key from: https://console.anthropic.com/settings/keys" Write-Output " -> Create Key -> copy it" Write-Output "" Write-Output "A real key starts with 'sk-ant-' and is about 100 characters long." Write-Output "Paste it below and press Enter. Nothing is shown as you type." Write-Output "" $secure = Read-Host -Prompt 'Paste your API key' -AsSecureString $bstr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($secure) try { $key = [Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr).Trim() } finally { [Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr) } switch (Test-KeyShape $key) { 'empty' { Write-Output "" Write-Output "Nothing was entered. No change made." exit 1 } 'placeholder' { Write-Output "" Write-Output "That looks like example text rather than a key (it contains" Write-Output "'...' or angle brackets). No change made -- copy the real key" Write-Output "from the Anthropic console and run this again." exit 1 } 'tooshort' { Write-Output "" Write-Output ("That is only {0} characters. A real key is around 100." -f $key.Length) Write-Output "No change made." exit 1 } 'prefix' { Write-Output "" Write-Output "That does not start with 'sk-ant-', so it is probably not an" Write-Output "Anthropic key. No change made." exit 1 } } setx ANTHROPIC_API_KEY $key | Out-Null # Also set it for THIS session, so the next command works without reopening. $env:ANTHROPIC_API_KEY = $key Write-Output "" Write-Output ("Saved. Key starts {0} and is {1} characters." -f $key.Substring(0,11), $key.Length) Write-Output "" Write-Output "It is active in this window already, and in any new window from now on." Write-Output "" Write-Output "Check it: python -m avwatch doctor" Write-Output "Then run: python -m avwatch digest --dry-run --no-email" Write-Output ""