Skip to content

Repository files navigation

A custom rate limiter and API credit system similar to what you see in Gemini/OpenAI/Claude APIs. This was intended as a learning project trying to push the edges of CAP theorem and latency. Completely serverless architecture written for cloudflare workers along with a serverless redis for fun. Below is AI slop for more info :)

API Management Platform

A production-ready serverless API management platform built with Cloudflare Workers and Upstash Redis. This platform provides a comprehensive solution for API key management, rate limiting, credit tracking, and multi-tenant plan management.

πŸš€ Features

  • API Key Management: Generate, validate, and revoke API keys with crypto-safe security
  • Role-Based Access Control (RBAC): Control access with different permission levels (Admin, Developer, Reader)
  • Subscription Plans: Four service tiers (Free, Standard, Pro, Test) with customizable limits
  • Real-time Rate Limiting: Apply rate limits based on the user's plan with sliding window algorithm
  • Credit System: Track and limit API usage with a precise credit-based system
  • Multi-tenancy: Support for multiple tenants with completely isolated resources
  • Performance Optimized: Ultra-fast response times with intelligent caching and smart cache management
  • Comprehensive Testing: 100% coverage with automated E2E test suite including detailed latency tracking
  • Production Ready: Deployed globally with monitoring and analytics

πŸ›  Tech Stack

  • Cloudflare Workers: Serverless compute platform with global edge deployment
  • Upstash Redis: Serverless Redis database with sub-10ms latency
  • Hono: Ultra-lightweight framework optimized for Cloudflare Workers
  • TypeScript: For complete type safety and developer experience
  • Zod: Runtime schema validation for bulletproof request handling

πŸ“Š Subscription Plans

Plan Rate Limit Credits Access
FREE 50/hour 1,000 Public APIs, Basic TODOs
STANDARD 500/hour 10,000 All v1 APIs, TODOs, Posts
PRO 5,000/hour 100,000 All endpoints (*)
TEST 3/5sec 9 All endpoints (for testing)

πŸš€ Quick Start

Prerequisites

  • Node.js (16+)
  • Upstash Redis account
  • Cloudflare account

Installation

  1. Clone the repository:

    git clone https://github.com/yourusername/api-management-platform.git
    cd api-management-platform
  2. Install dependencies:

    npm install
  3. Create .dev.vars file with your Upstash Redis credentials:

    UPSTASH_REDIS_REST_URL=your-upstash-redis-url
    UPSTASH_REDIS_REST_TOKEN=your-upstash-redis-token
  4. Start development server:

    npm run dev
  5. Deploy to production:

    npm run deploy

πŸ“– API Endpoints

Tenant Management

  • POST /tenant - Create a new tenant
  • GET /tenant/:tenantId - Get tenant information
  • GET /tenant - List all tenants (admin only)
  • PATCH /tenant/:tenantId/plan - Update tenant's plan
  • POST /tenant/:tenantId/credits - Add credits to a tenant

API Key Management

  • POST /keys/create - Generate a new API key
  • POST /keys/verify - Validate an API key
  • GET /keys/:keyId - Get API key details
  • GET /keys/tenant/:tenantId - List all keys for a tenant
  • DELETE /keys/:keyId - Revoke an API key
  • PATCH /keys/:keyId - Update API key details

API Proxy (Production)

  • GET /proxy/todos/:id - Access JSONPlaceholder todos API
  • GET /proxy/posts/:id - Access JSONPlaceholder posts API
  • POST /proxy/custom - Proxy custom API requests

System Health & Monitoring

  • GET /health - System health check and bootstrap
  • GET /cache-stats - Real-time cache performance metrics

⚑ Response Headers

All API responses include comprehensive headers for monitoring:

X-Credits-Used: 1
X-Credits-Remaining: 999
X-Credits-Cost: 1
X-Credits-Pre-Call: 0
X-RateLimit-Limit: 50
X-RateLimit-Remaining: 49
X-Cache-Hit: validation-cache,always-fresh
X-Lookup-Time: 45

πŸ§ͺ Testing

Run Comprehensive E2E Test with Latency Tracking

npm test
# OR
npm run test:comprehensive

The comprehensive test validates with detailed latency metrics:

  • βœ… API Key Validation (valid/invalid/empty/missing keys) - Tracks latency per validation type
  • βœ… Credit Logic (accurate tracking and deduction) - Measures credit update performance
  • βœ… Rate Limiting (proper 429 responses when limits exceeded) - Times rate limit enforcement
  • βœ… Credit Exhaustion (proper 402 responses when credits depleted) - Tracks exhaustion detection speed

Sample Test Output:

βœ… PASS Key Validation: 4/4 | Latency (ms): avg 245, min 156, max 334
βœ… PASS Credit Logic: 3/3 | Latency (ms): avg 289, min 267, max 312  
βœ… PASS Rate Limiting: 1/1 | Latency (ms): avg 198, min 167, max 245
βœ… PASS Credit Exhaustion: 1/1 | Latency (ms): avg 234, min 189, max 298

Test typically completes in ~6-8 seconds with 100% pass rate and detailed performance insights.

Development Commands

npm run dev          # Start development server
npm test            # Run comprehensive E2E tests with latency tracking
npm run deploy      # Deploy to Cloudflare Workers
npm run format      # Format code with Prettier

πŸ”§ Adding Third-Party APIs

  1. Configure API mapping in src/config/api-mappings.ts:

    "weather/forecast": {
      internalPath: "weather/forecast",
      targetUrl: "https://api.weather.com/v1/forecast",
      description: "Weather forecast API",
      creditCost: 5
    }
  2. Update plan access in src/config/plans.ts:

    [PlanType.STANDARD]: {
      endpoints: [
        "weather/*",  // Allow weather APIs
        // ... other endpoints
      ],
    }
  3. Add authentication in proxy route if needed

πŸ— Architecture

High-Performance Middleware Stack

  • Credit Sync Middleware: Optimized validation caching with always-fresh credit data
  • Smart Cache Management: Threshold-based cleanup (no arbitrary timers)
  • Rate Limiting: Sliding window with Upstash Redis
  • Permission Checking: Role-based access control
  • Analytics: Minimal overhead tracking for monitoring

Intelligent Caching Strategy

Cache Layers:
β”œβ”€β”€ Validation Cache (5min TTL) β†’ API key metadata  
β”œβ”€β”€ Always Fresh Credits β†’ Real-time credit tracking
β”œβ”€β”€ Smart Cleanup β†’ Threshold-based cache management
└── Memory Monitoring β†’ ~15MB cache limit with 90% efficiency

Data Storage Pattern

Redis Keys:
β”œβ”€β”€ apikey:{keyId} β†’ Full API key data
β”œβ”€β”€ lookup:{key} β†’ keyId mapping  
β”œβ”€β”€ tenant:{tenantId} β†’ Tenant information
β”œβ”€β”€ credits:{tenantId} β†’ Real-time credit tracking
└── ratelimit:{keyId} β†’ Rate limiting counters

🌐 Production Deployment

Live API: https://api.groundng.site

The platform is deployed and running with:

  • ⚑ Ultra-fast response times with optimized middleware
  • 🧠 Smart cache management - threshold-based cleanup
  • πŸ”’ Secure API key management with crypto-safe generation
  • πŸ“Š Real-time credit and rate limit tracking with synchronous updates
  • 🌍 Global edge deployment via Cloudflare Workers
  • πŸ’Ύ Serverless Redis with Upstash for sub-10ms operations
  • πŸ“ˆ Performance monitoring with built-in cache statistics

πŸ”’ Security Features

  • Cryptographically secure API key generation
  • Rate limiting with sliding window algorithm
  • Credit exhaustion protection (402 Payment Required)
  • Multi-tenant isolation with complete data segregation
  • Input validation with Zod schemas
  • Comprehensive error handling with detailed logging
  • Secure Redis operations with atomic transactions

πŸ“ˆ Performance Optimizations

Current Performance Metrics

  • Credit Validation: Cached for 5 minutes, always-fresh credit data
  • Cache Efficiency: 90%+ hit rates with smart cleanup algorithms
  • Memory Management: ~15MB cache limit with threshold-based cleanup
  • Redis Operations: Batched operations and connection reuse
  • Rate Limiting: Sub-10ms enforcement with sliding windows
  • Global Deployment: Edge computing via Cloudflare Workers

Cache Management Strategy

  • Threshold-Based Cleanup: No arbitrary timers, cleanup triggered by actual usage
  • Memory Efficiency: Conservative 15MB limit for 128MB worker environment
  • Smart Expiration: Expired entries cleaned first, oldest valid entries removed as needed
  • Performance Monitoring: Real-time cache statistics at /cache-stats

πŸ”§ Monitoring & Analytics

Built-in Monitoring Endpoints

  • /health - System health and version information
  • /cache-stats - Real-time cache performance metrics
  • Response headers include detailed performance data

Cache Statistics

{
  "total": 1234,
  "valid": 1100, 
  "expired": 134,
  "expiredRatio": 11,
  "memoryEstimateMB": "3.7MB",
  "utilizationPercent": "25%",
  "nextCleanup": {
    "sizeTriggered": "43766 entries away",
    "expiredTriggered": "20% expired threshold"
  }
}

πŸ“ License

ISC License

πŸ™ Acknowledgements

  • Upstash - Serverless Redis infrastructure with global replication
  • Cloudflare Workers - Global serverless platform with edge computing
  • Hono - High-performance web framework optimized for edge environments

About

A custom rate limiter and API credit system similar to what you see in Gemini/OpenAI/Claude APIs. This was intended as a learning project trying to push the edges of CAP theorem and latency. Completely serverless architecture written for cloudflare workers along with a serverless redis for fun

Resources

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages