@splashcodex/api-key-manager 5.0.0 → 5.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +35 -1
  2. package/bin/cli.js +50 -0
  3. package/package.json +14 -3
package/README.md CHANGED
@@ -11,6 +11,7 @@
11
11
  - **Built-in Persistence** — `FileStorage` (survives restarts) and `MemoryStorage` included.
12
12
  - **Singleton Pattern** — Thread-safe singletons with `getInstance()` and `Result<T>` pattern.
13
13
  - **Multi-Provider Vault** — Manage multiple providers (`gemini`, `openai`, `anthropic`) from a single entry point.
14
+ - **Centralized API Gateway** — Built-in Fastify proxy server to centralize AI requests for multiple apps securely.
14
15
 
15
16
  ## Features
16
17
 
@@ -30,6 +31,13 @@
30
31
  npm install @splashcodex/api-key-manager
31
32
  ```
32
33
 
34
+ ### 🚀 Auto-Scaffold (New!)
35
+ The fastest way to get started is to run the `init` command in your project directory:
36
+ ```bash
37
+ npx @splashcodex/api-key-manager init
38
+ ```
39
+ This will automatically create a `.env` template and a `demo.ts` file showing the Gemini Preset in action.
40
+
33
41
  ## Quick Start (v5 Presets)
34
42
 
35
43
  The fastest way to get started in any CodeDex repository.
@@ -50,8 +58,34 @@ const response = await gemini.execute(async (key) => {
50
58
  });
51
59
  ```
52
60
 
61
+ ### Centralized API Gateway (New!)
62
+
63
+ Run a standalone proxy server that acts as a single point of entry for all your microservices or repositories. It centralizes key management, circuit breaking, and rate limiting across applications.
64
+
65
+ 1. Install global/local and export your keys:
66
+ ```bash
67
+ export GOOGLE_GEMINI_API_KEY="AIzaSy...1,AIzaSy...2"
68
+ ```
69
+
70
+ 2. Start the gateway:
71
+ ```bash
72
+ npm run gateway
73
+ # Server runs on http://localhost:9000
74
+ ```
75
+
76
+ 3. Call the proxy from ANY language without managing keys in the client app:
77
+ ```bash
78
+ curl -X POST http://localhost:9000/v1/generate \
79
+ -H "Content-Type: application/json" \
80
+ -d '{"provider": "gemini", "prompt": "Hello!"}'
81
+ ```
82
+ Route supports both `/v1/generate` (Standard JSON) and `/v1/stream` (Server-Sent Events). Monitoring available at `/v1/health` and `/v1/providers`.
83
+
84
+ ---
85
+
53
86
  ### Multi-Provider Vault
54
- Perfect for gateways or complex bots handling multiple models.
87
+
88
+ Manage ALL your provider keys across your system from one pool.Perfect for gateways or complex bots handling multiple models.
55
89
 
56
90
  ```typescript
57
91
  import { MultiManager } from '@splashcodex/api-key-manager/presets/multi';
package/bin/cli.js ADDED
@@ -0,0 +1,50 @@
1
+ #!/usr/bin/env node
2
+ const fs = require('fs');
3
+ const path = require('path');
4
+
5
+ const args = process.argv.slice(2);
6
+ const command = args[0];
7
+
8
+ function printDocs() {
9
+ console.log(`\x1b[92m🌟 @splashcodex/api-key-manager v5.0\x1b[0m`);
10
+ console.log(`\x1b[96m📚 Documentation: https://www.npmjs.com/package/@splashcodex/ApiKeyManager\x1b[0m\n`);
11
+ console.log(`\x1b[93m🚀 Commands:\x1b[0m`);
12
+ console.log(` npx @splashcodex/api-key-manager init \x1b[90m# Scaffold a demo project in the current directory\x1b[0m`);
13
+ console.log(`\n\x1b[95m💡 Tip: Need help? Run init to see it in action!\x1b[0m\n`);
14
+ }
15
+
16
+ function initProject() {
17
+ const cwd = process.cwd();
18
+ const envPath = path.join(cwd, '.env');
19
+ const demoPath = path.join(cwd, 'demo.ts');
20
+
21
+ console.log(`\n\x1b[96m🚀 Initializing @splashcodex/api-key-manager environment...\x1b[0m\n`);
22
+
23
+ // Create .env
24
+ if (!fs.existsSync(envPath)) {
25
+ fs.writeFileSync(envPath, `GOOGLE_GEMINI_API_KEY="your_api_key_1,your_api_key_2"\nOPENAI_API_KEY="sk-..."\n`);
26
+ console.log(`\x1b[92m[✔] Created .env file\x1b[0m`);
27
+ } else {
28
+ console.log(`\x1b[90m[-] .env already exists. Remember to add GOOGLE_GEMINI_API_KEY!\x1b[0m`);
29
+ }
30
+
31
+ // Create demo.ts
32
+ if (!fs.existsSync(demoPath)) {
33
+ const tsCode = `import 'dotenv/config';\nimport { GeminiManager } from '@splashcodex/api-key-manager/presets/gemini';\n\nasync function main() {\n console.log("Initialize GeminiManager...");\n // Automatically parse GOOGLE_GEMINI_API_KEY from env\n const result = GeminiManager.getInstance();\n if (!result.success) {\n console.error("Failed to initialize:", result.error);\n return;\n }\n \n const gemini = result.data;\n \n console.log("Executing resilient request...");\n // Automatically handles key rotation and 429 quota limits\n try {\n const response = await gemini.execute(async (key) => {\n console.log(\`[Network] Sending request with key: \${key.substring(0, 8)}...\`);\n // Replace with actual fetch: await fetch(..., { headers: { 'x-goog-api-key': key } })\n return "Success: Simulated API Response!";\n });\n console.log("Result:", response);\n } catch (e) {\n console.error("All keys exhausted or network failed.", e.message);\n }\n}\n\nmain();\n`;
34
+ fs.writeFileSync(demoPath, tsCode);
35
+ console.log(`\x1b[92m[✔] Created demo.ts\x1b[0m`);
36
+ } else {
37
+ console.log(`\x1b[90m[-] demo.ts already exists.\x1b[0m`);
38
+ }
39
+
40
+ console.log(`\n\x1b[93m🎉 Setup Complete!\x1b[0m`);
41
+ console.log(`To run the demo:`);
42
+ console.log(`\x1b[36m npm install dotenv\x1b[0m`);
43
+ console.log(`\x1b[36m npx ts-node demo.ts\x1b[0m\n`);
44
+ }
45
+
46
+ if (command === 'init') {
47
+ initProject();
48
+ } else {
49
+ printDocs();
50
+ }
package/package.json CHANGED
@@ -1,7 +1,10 @@
1
1
  {
2
2
  "name": "@splashcodex/api-key-manager",
3
- "version": "5.0.0",
3
+ "version": "5.0.2",
4
4
  "description": "Universal API Key Management Gateway with provider presets, persistence, and multi-provider vault",
5
+ "bin": {
6
+ "api-key-manager": "./bin/cli.js"
7
+ },
5
8
  "main": "dist/index.js",
6
9
  "types": "dist/index.d.ts",
7
10
  "exports": {
@@ -72,12 +75,16 @@
72
75
  },
73
76
  "files": [
74
77
  "dist",
75
- "src"
78
+ "src",
79
+ "bin"
76
80
  ],
77
81
  "scripts": {
78
82
  "build": "tsc",
79
83
  "test": "jest --verbose",
80
- "prepublishOnly": "npm run build"
84
+ "prepublishOnly": "npm run build",
85
+ "postinstall": "node -e \"console.log('\\n\\x1b[32m🌟 Successfully installed @splashcodex/api-key-manager v5.0!\\x1b[0m\\n\\x1b[36m📚 Documentation: https://www.npmjs.com/package/@splashcodex/ApiKeyManager\\x1b[0m\\n\\x1b[33m🚀 Quick Start:\\x1b[0m\\n import { GeminiManager } from \\'@splashcodex/api-key-manager/presets/gemini\\';\\n const gemini = GeminiManager.getInstance().data;\\n')\"",
86
+ "gateway": "ts-node gateway/server.ts",
87
+ "gateway:dev": "npx nodemon --watch gateway/ --ext ts --exec ts-node gateway/server.ts"
81
88
  },
82
89
  "keywords": [
83
90
  "api",
@@ -108,5 +115,9 @@
108
115
  "ts-jest": "^29.4.6",
109
116
  "ts-node": "^10.9.2",
110
117
  "typescript": "^5.3.3"
118
+ },
119
+ "dependencies": {
120
+ "@fastify/cors": "^11.2.0",
121
+ "fastify": "^5.7.4"
111
122
  }
112
123
  }