A simple JavaScript library for obfuscating non-sensitive API keys in client-side CodePen projects
SaltyKeys.js is a specialized JavaScript library designed to help developers working with CodePen projects that need to use non-sensitive API keys. It provides a client-side approach to obfuscating and verifying API keys, binding them to a specific CodePen context to help prevent casual extraction.
Keys are bound to specific CodePen IDs, making them usable only in the originating environment. This helps prevent simple copy-paste extraction of your keys.
Uses a combination of encoding techniques, timestamps, and random nonces to obscure API keys beyond simple base64 encoding.
Can be configured for different environments beyond CodePen with custom URL patterns and environment settings.
When developing on CodePen or similar platforms, you might need to use API keys for external services. Exposing these keys directly in client-side code makes them easily accessible to anyone viewing your pen. SaltyKeys helps address this challenge by:
Add SaltyKeys.js to your project:
<script src="SaltyKeys.js"></script>
Configure the library with your preferred settings:
SaltyKeys.configure({
urlPattern: /codepen\.io\/[^/]+\/(?:pen|debug|fullpage|fullembedgrid)\/([^?#]+)/,
cacheEnabled: true,
environment: 'codepen'
});
In the console or a separate script, generate your salted key:
const apiKey = 'my-api-key-123'; // Your actual API key
const saltedKey = SaltyKeys.generateSaltedKey(apiKey);
console.log('Your salted key:', saltedKey);
Use the salted key in your CodePen project instead of the raw API key:
// In your main script
const saltedKey = 'your-salted-key-from-console';
const actualApiKey = SaltyKeys.getApiKey(saltedKey);
if (actualApiKey) {
// Use the API key with your service
initializeApiService(actualApiKey);
} else {
console.error('Failed to retrieve API key');
}
SaltyKeys is appropriate for:
SaltyKeys is not appropriate for:
For proper API key security in production applications:
Create a server endpoint that makes API calls on behalf of your client-side code.
Store API keys in environment variables on your server, never in client-side code.
Prevent abuse by implementing rate limits on your API endpoints.
Regularly rotate your API keys to limit the impact of any exposure.
┌─────────────┐ ┌─────────────┐ ┌───────────────┐
│ │ │ │ │ │
│ Client │──────│ API Proxy │──────│ External API │
│ Browser │ │ Server │ │ Service │
│ │ │ │ │ │
└─────────────┘ └─────────────┘ └───────────────┘
No keys Secure key
management
Explore the interactive demo or check out the GitHub repository for more information.