chat
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.
Added user-specific API key management to the chat application, allowing multiple users to use their own API keys instead of relying on a single .env file.
- Added
apiKeysobject to store API keys per provider:apiKeys: { groq: '', openai: '', // Future support anthropic: '' // Future support }
- Added separate localStorage key
chatapp-api-keysfor API key storage - Implemented API key management methods:
loadApiKeys()- Load API keys from localStorage on initsaveApiKeys()- Persist API keys to localStorageupdateApiKey(provider, key)- Update specific provider's API keygetApiKey(provider)- Retrieve API key for a providerclearApiKeys()- Clear all API keys
- Added "API Keys" section in the right sidebar settings panel
- Groq API Key input field with:
- Password-type input for security
- Toggle visibility button (eye icon)
- Monospace font for better readability
- Link to Groq console for obtaining API keys
- Placeholder for OpenAI API key (coming soon)
- Updated
sendChatRequest()to:- Extract provider from model ID (e.g.,
groq/compound→groq) - Retrieve user's API key for that provider
- Pass API key in job creation request
- Extract provider from model ID (e.g.,
- Updated
getChatParameters()to includesystemPromptin returned parameters
- Added
apiKey?: stringtoChatCompletionOptionsinterface - Added
systemPrompt?: stringtoChatCompletionOptionsinterface
- Updated
complete()andstream()methods to:- Accept API key from
options.apiKeyif provided - Fall back to environment variable if no API key in options
- Prepend system prompt to messages if provided
- Include
imageDetailin request body - Return better error messages when API key is missing
- Accept API key from
- Updated
/api/chatendpoint to:- Accept
apiKeyparameter from request body - Accept all Groq-specific parameters
- Skip
isConfigured()check if API key is provided in request - Pass API key through to provider in options
- Accept
- Updated
ChatJobInputinterface to includeapiKeyandsystemPrompt - Updated
runChatJob()function to:- Extract API key from job input
- Skip
isConfigured()check if API key is provided - Pass API key through to provider in options
- Provide helpful error message when API key is missing
-
Local Storage: API keys are stored in browser's localStorage
- Keys never leave the user's browser except when making API calls
- Each user maintains their own keys
- Keys are stored separately from chat data
-
Password Input: API key input uses
type="password"by default- Users can toggle visibility with eye icon
- Prevents shoulder surfing
-
No Server-Side Storage: Backend never stores user-provided API keys
- Keys are only passed through for API calls
- Falls back to
.envfor backward compatibility
- Click the settings (cog) icon in the chat header
- Scroll to "API Keys" section
- Enter your Groq API key (get one from https://console.groq.com/keys)
- The key is automatically saved to your browser
- Start chatting - your personal API key will be used
- Environment variable still works: set
GROQ_API_KEYin.env - Users without API keys will fall back to env variable
- To require user API keys: remove
GROQ_API_KEYfrom.env
-
Additional Providers
- OpenAI API key support
- Anthropic API key support
- Custom provider configuration
-
Key Validation
- Test API key button
- Show connection status indicator
- Validate key format before saving
-
Enhanced Security
- Optional key encryption in localStorage
- Session-only keys (not persisted)
- Key expiration reminders
-
Multi-User Features
- Profile management
- Multiple key sets per user
- Usage tracking per API key
To test the feature:
- Remove
GROQ_API_KEYfrom your.envfile - Open the chat app
- Try to send a message - should get error about missing API key
- Add your API key in settings
- Send message again - should work with your key
- Refresh page - API key should persist
- Toggle password visibility - should mask/unmask key
- Existing
.envconfiguration still works - Users can continue using shared API keys
- Gradual migration to user-specific keys
- No breaking changes to existing functionality
