DEV Community

Cover image for ElevenLabs API key for Voices script.
Ramandeep Singh
Ramandeep Singh

Posted on

ElevenLabs API key for Voices script.

1. Get an ElevenLabs API key

  1. Create an account: https://elevenlabs.io
  2. Log in and open Profile -> API Keys (or go to the developers/API keys page).
  3. Click Create API Key.
  4. Name the key (e.g. "PowerShell Script") and save it when shown.
  5. 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-key header 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))
Enter fullscreen mode Exit fullscreen mode

Example API call:

$headers = @{
    "xi-api-key" = $apiKey
    "Content-Type" = "application/json"
}
Invoke-RestMethod -Uri "https://api.elevenlabs.io/v1/voices" -Headers $headers
Enter fullscreen mode Exit fullscreen mode

3. Security

  • Add elevenlabs.key or 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)