API Key Management and Security
Relevant source files
The following files were used as context for generating this wiki page:
- VoiceInk/Services/AIService.swift
- VoiceInk/Views/AI Models/APIKeyManagementView.swift
- VoiceInk/Services/CloudTranscription/SonioxTranscriptionService.swift
- VoiceInk/Services/CloudTranscription/GeminiTranscriptionService.swift
- VoiceInk/Views/AI Models/CloudModelCardRowView.swift
- VoiceInk/Services/ImportExportService.swift
- VoiceInk/PowerMode/PowerModeConfigView.swift
VoiceInk integrates with various third-party cloud services for features like AI-powered text enhancement and cloud-based transcription. Access to these services requires API keys, and the application provides a centralized system for managing their storage, verification, and use. The core of this system is the AIService class, which orchestrates interactions between the user interface, local storage, and the external service providers.
This document outlines the architecture and security practices for handling API keys within the VoiceInk application. It covers the supported providers, the key lifecycle from input to deletion, the verification process, and important security considerations such as storage and data export policies.
Core Architecture
The API key management system is primarily handled by the AIService class, which acts as a single source of truth for the currently selected AI provider and its associated credentials.
Sources: VoiceInk/Services/AIService.swift
Key Components
| Component | Description | File |
|---|---|---|
AIService | An ObservableObject that manages the state of AI providers, API keys, and model selections. It handles key verification, saving, and clearing. | VoiceInk/Services/AIService.swift |
AIProvider | An enum that defines all supported third-party services, their base URLs, and whether they require an API key. | VoiceInk/Services/AIService.swift |
APIKeyManagementView | A SwiftUI View that provides the user interface for selecting providers and managing their API keys. | VoiceInk/Views/AI Models/APIKeyManagementView.swift |
CloudModelCardView | A SwiftUI View for individual cloud transcription models that includes an expandable section for API key configuration. | VoiceInk/Views/AI Models/CloudModelCardRowView.swift |
Class Diagram
This diagram shows the relationship between the main service class and the provider enumeration.
Supported AI Providers
The AIProvider enum lists all integrated cloud services. Most providers require an API key, with the notable exception of Ollama, which is designed for local connections.
| Provider | Requires API Key | Base URL |
|---|---|---|
Cerebras | Yes | https://api.cerebras.ai/v1/chat/completions |
GROQ | Yes | https://api.groq.com/openai/v1/chat/completions |
Gemini | Yes | https://generativelanguage.googleapis.com/v1beta/openai/chat/completions |
Anthropic | Yes | https://api.anthropic.com/v1/messages |
OpenAI | Yes | https://api.openai.com/v1/chat/completions |
OpenRouter | Yes | https://openrouter.ai/api/v1/chat/completions |
Mistral | Yes | https://api.mistral.ai/v1/chat/completions |
ElevenLabs | Yes | https://api.elevenlabs.io/v1/speech-to-text |
Deepgram | Yes | https://api.deepgram.com/v1/listen |
Soniox | Yes | https://api.soniox.com/v1 |
Ollama | No | http://localhost:11434 (default) |
Custom | Yes | User-defined |
Sources: VoiceInk/Services/AIService.swift:5-72, VoiceInk/Services/AIService.swift:180-184
API Key Lifecycle
The lifecycle of an API key involves user input, verification against the provider's endpoint, secure storage for future use, and eventual deletion.
This flowchart illustrates the API key management process from the user's perspective.
1. Input and Verification
When a user enters an API key in APIKeyManagementView or CloudModelCardView and clicks "Verify and Save", the AIService.saveAPIKey(_:completion:) method is invoked.
- The
saveAPIKeymethod immediately callsverifyAPIKey. verifyAPIKeydispatches to a provider-specific verification function (e.g.,verifyOpenAICompatibleAPIKey,verifyAnthropicAPIKey).- The specific function constructs a lightweight test request to a known endpoint for that provider (e.g., a model list or a simple chat completion).
- The request is sent with the provided API key in the
Authorizationheader. - The key is considered valid if the API returns an
HTTP 200 OKstatus code. Any other status code results in a verification failure.
The following sequence diagram details the verification flow for an OpenAI-compatible provider.
2. Storage
Upon successful verification, the API key is stored in UserDefaults. The storage key is dynamically generated based on the provider's name to avoid conflicts.
- Format:
"[PROVIDER_NAME]APIKey" - Example: For the GROQ provider, the key is stored under the
UserDefaultskey"GROQAPIKey".
// VoiceInk/Services/AIService.swift:325-326
self.apiKey = key
self.userDefaults.set(key, forKey: "\(self.selectedProvider.rawValue)APIKey")
When the user switches between providers in the UI, AIService automatically loads the corresponding key from UserDefaults if it exists.
Sources: VoiceInk/Services/AIService.swift:203-206, VoiceInk/Services/AIService.swift:326
3. Usage
Services that perform network requests, such as SonioxTranscriptionService and GeminiTranscriptionService, retrieve the API key directly from UserDefaults when needed. This decouples the transcription services from the AIService state.
// VoiceInk/Services/CloudTranscription/SonioxTranscriptionService.swift:19-23
private func getAPIConfig(for model: any TranscriptionModel) throws -> APIConfig {
guard let apiKey = UserDefaults.standard.string(forKey: "SonioxAPIKey"), !apiKey.isEmpty else {
throw CloudTranscriptionError.missingAPIKey
}
return APIConfig(apiKey: apiKey)
}
Sources: VoiceInk/Services/CloudTranscription/SonioxTranscriptionService.swift:19-23, VoiceInk/Services/CloudTranscription/GeminiTranscriptionService.swift:88-91
4. Deletion
Users can remove an API key via the "Remove Key" button in APIKeyManagementView or the options menu in CloudModelCardView. This action calls the AIService.clearAPIKey() method, which removes the key from UserDefaults and resets the validation status.
// VoiceInk/Services/AIService.swift:513-518
func clearAPIKey() {
guard selectedProvider.requiresAPIKey else { return }
apiKey = ""
isAPIKeyValid = false
userDefaults.removeObject(forKey: "\(selectedProvider.rawValue)APIKey")
NotificationCenter.default.post(name: .aiProviderKeyChanged, object: nil)
}
Sources: VoiceInk/Services/AIService.swift:513-518, VoiceInk/Views/AI Models/APIKeyManagementView.swift:302-304
Security Considerations
Key Storage
API keys are stored in the application's standard UserDefaults property list file. While this is a common practice for macOS applications, it means the keys are stored in plain text on the user's local disk. The UI mitigates exposure by using SecureField for key entry, which obscures the characters.
Sources: VoiceInk/Services/AIService.swift:326, VoiceInk/Views/AI Models/APIKeyManagementView.swift:321
Import and Export
A significant security measure is the explicit exclusion of API keys from the settings export feature. The ImportExportService gathers all user-configurable settings but intentionally omits API keys to prevent them from being accidentally shared or stored in an insecure backup file.
The exportSettings function in ImportExportService gathers numerous settings but does not read or include any values associated with API keys from UserDefaults. The documentation comment within the file reinforces this.
Export your custom prompts, power modes, word replacements, keyboard shortcuts, and app preferences to a backup file. API keys are not included in the export.
Sources: VoiceInk/Services/ImportExportService.swift:233-234
Power Mode Configurations
When a Power Mode is configured to use a specific AI provider, it stores the provider's name (selectedAIProvider) and the selected model (selectedAIModel). It does not store the API key itself. The system relies on the globally managed API key for that provider at the time of execution.
Sources: VoiceInk/PowerMode/PowerModeConfigView.swift:708-719