{"id":31956335,"url":"https://github.com/profullstack/api-key-manager","last_synced_at":"2025-10-14T14:49:59.846Z","repository":{"id":292679997,"uuid":"981596444","full_name":"profullstack/api-key-manager","owner":"profullstack","description":null,"archived":false,"fork":false,"pushed_at":"2025-05-13T03:33:41.000Z","size":69,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-10-02T23:20:08.878Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/profullstack.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-05-11T13:21:53.000Z","updated_at":"2025-05-13T03:33:45.000Z","dependencies_parsed_at":"2025-05-11T15:19:13.618Z","dependency_job_id":"6c415694-93d0-462b-bcc9-9418bbaba9d8","html_url":"https://github.com/profullstack/api-key-manager","commit_stats":null,"previous_names":["profullstack/api-key-manager"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/profullstack/api-key-manager","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/profullstack%2Fapi-key-manager","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/profullstack%2Fapi-key-manager/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/profullstack%2Fapi-key-manager/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/profullstack%2Fapi-key-manager/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/profullstack","download_url":"https://codeload.github.com/profullstack/api-key-manager/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/profullstack%2Fapi-key-manager/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279019141,"owners_count":26086685,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-14T02:00:06.444Z","response_time":60,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-10-14T14:48:33.376Z","updated_at":"2025-10-14T14:49:59.841Z","avatar_url":"https://github.com/profullstack.png","language":"JavaScript","readme":"# @profullstack/api-key-manager\n\nA simple, flexible API key management system with generation, validation, and rate limiting.\n\n## Features\n\n- Generate and manage API keys\n- Validate API keys\n- Rate limiting\n- Customizable storage adapters (memory, Redis, database)\n- Permission-based access control\n- Key expiration\n- Express/Connect/Hono middleware\n\n## Installation\n\n```bash\nnpm install @profullstack/api-key-manager\n```\n\n## Basic Usage\n\n```javascript\nimport { createApiKeyManager } from '@profullstack/api-key-manager';\n\n// Create an API key manager with default options (in-memory storage)\nconst apiKeyManager = createApiKeyManager();\n\n// Create an API key\nconst apiKey = await apiKeyManager.createKey({\n  userId: 'user123',\n  name: 'Development API Key',\n  permissions: {\n    read: true,\n    write: true\n  }\n});\n\nconsole.log(`API Key: ${apiKey.key}`);\n\n// Validate an API key\nconst keyInfo = await apiKeyManager.validateKey('api_1234567890abcdef');\n\nif (keyInfo) {\n  console.log(`Valid API key for user: ${keyInfo.userId}`);\n  console.log(`Permissions: ${JSON.stringify(keyInfo.permissions)}`);\n} else {\n  console.log('Invalid API key');\n}\n```\n\n## API Reference\n\n### Creating an API Key Manager\n\n```javascript\nimport { createApiKeyManager, MemoryAdapter } from '@profullstack/api-key-manager';\n\n// With default options (in-memory storage)\nconst apiKeyManager = createApiKeyManager();\n\n// With custom options\nconst customApiKeyManager = createApiKeyManager({\n  adapter: new MemoryAdapter(), // Or use a custom adapter\n  prefix: 'myapp_', // Custom API key prefix\n  keyLength: 24, // Custom key length in bytes\n  rateLimit: {\n    windowMs: 60 * 1000, // 1 minute\n    maxRequests: 100 // 100 requests per minute\n  }\n});\n```\n\n### Managing API Keys\n\n#### Creating an API Key\n\n```javascript\nconst apiKey = await apiKeyManager.createKey({\n  userId: 'user123', // Required\n  name: 'Development API Key', // Required\n  permissions: { // Optional\n    read: true,\n    write: true,\n    admin: false\n  },\n  expiresAt: '2025-12-31T23:59:59Z', // Optional\n  metadata: { // Optional\n    environment: 'development',\n    createdBy: 'admin'\n  }\n});\n```\n\n#### Getting API Keys for a User\n\n```javascript\nconst keys = await apiKeyManager.getKeys('user123');\n\nkeys.forEach(key =\u003e {\n  console.log(`${key.name} (${key.id})`);\n  console.log(`Active: ${key.isActive}`);\n  console.log(`Created: ${key.createdAt}`);\n  console.log(`Permissions: ${JSON.stringify(key.permissions)}`);\n});\n```\n\n#### Getting an API Key by ID\n\n```javascript\nconst key = await apiKeyManager.getKeyById('key123', 'user123');\n\nif (key) {\n  console.log(`Found key: ${key.name}`);\n} else {\n  console.log('Key not found or does not belong to user');\n}\n```\n\n#### Updating an API Key\n\n```javascript\nconst updatedKey = await apiKeyManager.updateKey('key123', 'user123', {\n  name: 'Updated API Key',\n  isActive: true,\n  permissions: {\n    read: true,\n    write: false\n  },\n  expiresAt: new Date('2026-01-01'),\n  metadata: {\n    environment: 'production'\n  }\n});\n```\n\n#### Deleting an API Key\n\n```javascript\nconst deleted = await apiKeyManager.deleteKey('key123', 'user123');\n\nif (deleted) {\n  console.log('API key deleted successfully');\n} else {\n  console.log('API key not found or does not belong to user');\n}\n```\n\n### Validating API Keys\n\n```javascript\nconst keyInfo = await apiKeyManager.validateKey('api_1234567890abcdef');\n\nif (keyInfo) {\n  // API key is valid\n  console.log(`User ID: ${keyInfo.userId}`);\n  console.log(`Permissions: ${JSON.stringify(keyInfo.permissions)}`);\n  \n  // Check specific permissions\n  if (keyInfo.permissions.admin) {\n    // Allow admin actions\n  }\n} else {\n  // API key is invalid, expired, or inactive\n}\n```\n\n### Rate Limiting\n\n```javascript\n// Check if a request is within rate limits\nconst allowed = await apiKeyManager.checkRateLimit('key123');\n\nif (allowed) {\n  // Process the request\n} else {\n  // Return rate limit exceeded error\n}\n```\n\n### Using as Middleware\n\n```javascript\nimport express from 'express';\nimport { createApiKeyManager } from '@profullstack/api-key-manager';\n\nconst app = express();\nconst apiKeyManager = createApiKeyManager();\n\n// Add API key middleware to routes that require authentication\napp.use('/api', apiKeyManager.middleware());\n\napp.get('/api/data', (req, res) =\u003e {\n  // The API key info is available in req.apiKey\n  const userId = req.apiKey.userId;\n  const permissions = req.apiKey.permissions;\n  \n  // Check permissions\n  if (!permissions.read) {\n    return res.status(403).json({ error: 'Permission denied' });\n  }\n  \n  // Process the request\n  res.json({ data: 'Some protected data' });\n});\n\napp.listen(3000);\n```\n\n## Storage Adapters\n\n### Memory Adapter (Default)\n\nStores API keys in memory. Suitable for development or testing.\n\n```javascript\nimport { createApiKeyManager, MemoryAdapter } from '@profullstack/api-key-manager';\n\nconst apiKeyManager = createApiKeyManager({\n  adapter: new MemoryAdapter()\n});\n```\n\n### Redis Adapter\n\nStores API keys in Redis. Suitable for production use.\n\n```javascript\nimport { createApiKeyManager } from '@profullstack/api-key-manager';\nimport { RedisAdapter } from '@profullstack/api-key-manager/redis';\nimport { createClient } from 'redis';\n\nconst redisClient = createClient({\n  url: 'redis://localhost:6379'\n});\n\nawait redisClient.connect();\n\nconst apiKeyManager = createApiKeyManager({\n  adapter: new RedisAdapter(redisClient)\n});\n```\n\n### Database Adapter\n\nStores API keys in a database. Suitable for production use.\n\n```javascript\nimport { createApiKeyManager } from '@profullstack/api-key-manager';\nimport { DatabaseAdapter } from '@profullstack/api-key-manager/database';\nimport { Pool } from 'pg';\n\nconst pool = new Pool({\n  connectionString: 'postgresql://user:password@localhost:5432/database'\n});\n\nconst apiKeyManager = createApiKeyManager({\n  adapter: new DatabaseAdapter(pool)\n});\n```\n\n## Creating Custom Adapters\n\nYou can create custom adapters by implementing the adapter interface:\n\n```javascript\nclass CustomAdapter {\n  async saveKey(apiKey) { /* ... */ }\n  async getKeyById(keyId) { /* ... */ }\n  async getKeyByValue(keyValue) { /* ... */ }\n  async getKeysByUserId(userId) { /* ... */ }\n  async updateKey(keyId, updatedKey) { /* ... */ }\n  async deleteKey(keyId) { /* ... */ }\n  async checkRateLimit(keyId, rateLimit) { /* ... */ }\n}\n```\n\n## Examples\n\nSee the [examples](./examples) directory for complete usage examples.\n\n## License\n\nMIT","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprofullstack%2Fapi-key-manager","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprofullstack%2Fapi-key-manager","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprofullstack%2Fapi-key-manager/lists"}