1. Get an ElevenLabs API key
- Create an account: https://elevenlabs.io
- Log in and open Profile -> API Keys (or go to the developers/API keys page).
- Click Create API Key.
- Name the key (e.g. "PowerShell Script") and save it when shown.
- Copy the key immediately as you wonβt see the full value again; only the last 4 characters will be visible later.
2. Use the key in PowerShell
- Send it in the
xi-api-keyheader with every ElevenLabs API request.
Avoid hardcoding:
# Option A: Use environment variable (recommended)
$apiKey = $env:ELEVENLABS_API_KEY
# Set once: $env:ELEVENLABS_API_KEY = "your_key_here"
# Option B: Read from secure file (one-time setup: Read-Host -AsSecureString | ConvertFrom-SecureString | Out-File "elevenlabs.key")
$apiKey = (Get-Content "elevenlabs.key" | ConvertTo-SecureString).GetNetworkCredential().Password
# Option C: Prompt at runtime
$apiKey = Read-Host -Prompt "Enter ElevenLabs API Key" -AsSecureString
$apiKey = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($apiKey))
Example API call:
$headers = @{
"xi-api-key" = $apiKey
"Content-Type" = "application/json"
}
Invoke-RestMethod -Uri "https://api.elevenlabs.io/v1/voices" -Headers $headers
3. Security
- Add
elevenlabs.keyor similar files to.gitignore. - On ElevenLabs, you can restrict the key by feature and set credit limits.
Repository link - https://github.com/r123singh/ps-magic
Top comments (0)