| 1 | /** |
| 2 | * Validates an Anthropic API key format |
| 3 | * @param apiKey The API key to validate |
| 4 | * @returns true if the API key is valid, false otherwise |
| 5 | */ |
| 6 | export function isValidAnthropicApiKey( |
| 7 | apiKey: string | null | undefined, |
| 8 | ): boolean { |
| 9 | if (!apiKey || typeof apiKey !== "string") { |
| 10 | return false; |
| 11 | } |
| 12 | |
| 13 | // Anthropic API keys must start with "sk-ant-" and have additional characters |
| 14 | return apiKey.startsWith("sk-ant-") && apiKey.length > "sk-ant-".length; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Gets a user-friendly error message for invalid API keys |
| 19 | * @param apiKey The API key that failed validation |
| 20 | * @returns A descriptive error message |
| 21 | */ |
| 22 | export function getApiKeyValidationError( |
| 23 | apiKey: string | null | undefined, |
| 24 | ): string { |
| 25 | if (!apiKey || typeof apiKey !== "string") { |
| 26 | return "API key is required"; |
| 27 | } |
| 28 | |
| 29 | if (!apiKey.startsWith("sk-ant-")) { |
| 30 | return 'API key must start with "sk-ant-"'; |
| 31 | } |
| 32 | |
| 33 | if (apiKey.length <= "sk-ant-".length) { |
| 34 | return "API key is too short"; |
| 35 | } |
| 36 | |
| 37 | return "Invalid API key format"; |
| 38 | } |