Api Gateway

👤 jlacroix82 📦 v1.1.0 ⭐ 4.6 ⬇️ 273 下載
💻 開發程式設計 免費

📖 技能介紹


name: api-gateway description: Smart proxy for external API calls with retry, caching, rate limiting, and fallback providers. Key management with masked display. Zero external dependencies.


API Gateway ⚡

Stop duplicating API logic. Start routing through one smart gateway.

The Problem

Every agent call to an external API needs its own retry logic, caching, rate limit handling, and key management. That's the same code repeated across every skill.

API Gateway centralizes all of that into one tool.

Quick Start

Make an API call with retry and caching

node skills/api-gateway/api-gateway.js --call openai https://api.openai.com/v1/chat/completions '{"model":"gpt-4","messages":[{"role":"user","content":"hello"}]}'

Automatically retries up to 3 times with exponential backoff, caches responses for 5 minutes.

Dry run (preview without executing)

node skills/api-gateway/api-gateway.js --call --dry-run openai https://api.openai.com/v1/chat/completions

Manage API keys

# List configured keys (masked)
node skills/api-gateway/api-gateway.js --keys

# Add a key
node skills/api-gateway/api-gateway.js --keys add openai sk-abc123

# Remove a key
node skills/api-gateway/api-gateway.js --keys remove openai

Check cache status

# Show cache entries
node skills/api-gateway/api-gateway.js --cache

# Clear cache
node skills/api-gateway/api-gateway.js --cache --clear

Check rate limit status

node skills/api-gateway/api-gateway.js --rate openai

Set fallback provider

node skills/api-gateway/api-gateway.js --fallback openai anthropic

If the primary provider fails or is rate-limited, automatically tries the fallback.

Status overview

node skills/api-gateway/api-gateway.js --status

Circuit breaker status

node skills/api-gateway/api-gateway.js --circuit --status

Reset a circuit

node skills/api-gateway/api-gateway.js --circuit <provider> --reset

Features

Retry with Exponential Backoff

小蔥技能7w4.net持續更新中。

  • 3 retry attempts by default
  • Exponential backoff (1s, 2s, 4s)
  • All failures logged with attempt count

Response Caching

  • 5-minute TTL on cached responses
  • Cache key = provider + endpoint + body
  • Auto-evicts expired entries
  • Cache hit detection prevents redundant calls

Rate Limit Handling

  • Tracks remaining requests from x-ratelimit-remaining headers
  • Auto-fallback when rate limited (if configured)
  • Status check via --rate

Key Management

  • Masked display (first 4 + last 4 chars)
  • Per-provider key storage in memory/api-gateway/keys.json
  • Add/remove keys without exposing full values

Fallback Providers

  • Configurable per-provider fallback chain
  • Automatic failover on rate limit or error
  • No manual intervention needed

Circuit Breaker

  • Tracks failure rates per provider
  • Opens circuit after 5 consecutive failures
  • Auto-recovers after 30s cooldown (HALF-OPEN state)
  • Prevents cascading failures and saves API costs
  • CLI visibility: --circuit --status + --circuit <name> --reset

Configuration

Data files stored in: memory/api-gateway/

  • keys.json — API key storage
  • fallbacks.json — Fallback provider mappings
  • cache.json — Response cache
  • rate-limits.json — Rate limit tracking
  • request-log.json — Request history (last 1000)
  • circuit-state.json — Circuit breaker state per provider

Override data directory:

--dir /path/to/data

Agent Protocol

When making API calls:

  1. Use the gateway--call <provider> <endpoint> [body] instead of direct fetch
  2. Add keys first--keys add before first use
  3. Set fallbacks--fallback primary secondary for critical providers
  4. Check cache--cache during heartbeats to monitor usage
  5. Dry run--call --dry-run before executing important calls

Security Notes

  • Keys stored as plain text in JSON (same risk as .env files)
  • For production, integrate with a secrets manager
  • Masked output prevents accidental exposure in logs
  • Request log limited to 1000 entries

Comparison

Approach Retry Cache Rate Limit Fallback
Manual fetch
Custom wrapper ⚠️ ⚠️ ⚠️
API Gateway

API Gateway gives you retry + caching + rate limiting + fallback in one call.

Design Principles

  1. Zero setup — Works immediately, no config needed
  2. No dependencies — Pure Node.js http/https, no npm packages
  3. Resilient — Retry, fallback, and rate limit handling built in
  4. Transparent — Masked keys, cache status, request logging
  5. Composable — Works with any HTTP API, not just specific providers

🤖 AI 評測

這是一個質量不錯的 API 閘道器工具,能幫助統一管理外部 API 呼叫,避免重複編寫重試、快取、熔斷等邏輯。它使用簡單、文件詳細、不需要安裝額外依賴。主要優點是功能齊全、安全考慮周到(金鑰不會在日誌中暴露);不足之處是金鑰儲存在本地檔案中,使用時需要注意檔案安全。總體推薦,適合需要頻繁呼叫外部 API 的場景。

📊 多維度評分

適應性4.7
規範性4.2
有效性4.7
可靠性4.9
可信度4.5

📁 包含檔案 (8 個)

📄 README.md 3.1 KB
📄 SKILL.md 4.8 KB
📄 _meta.json 128 B
📄 api-gateway.js 19.7 KB
📄 clawhub.yaml 332 B
📄 skill-card.md 2 KB
📄 test/run-tests.js 3.4 KB
📄 tests/run-self-tests.js 6.5 KB