Skip to content

feat: add GET /proxy/v1/key endpoint for API key balance info#122

Open
gorkie-agent wants to merge 2 commits into
hackclub:mainfrom
gorkie-agent:feat/key-info-endpoint
Open

feat: add GET /proxy/v1/key endpoint for API key balance info#122
gorkie-agent wants to merge 2 commits into
hackclub:mainfrom
gorkie-agent:feat/key-info-endpoint

Conversation

@gorkie-agent

Copy link
Copy Markdown

What

Adds a new GET /proxy/v1/key endpoint that returns information about the API key used for authentication, including the daily spending limit, current usage, and remaining balance.

This is similar to OpenRouter's GET /api/v1/key endpoint.

Why

Currently there's no programmatic way to check how much of your daily spending limit you've used or have remaining. This is useful for:

  • Monitoring usage from scripts and tools
  • Building dashboards or alerts
  • Programmatic budget management

How it works

Endpoint: GET /proxy/v1/key
Auth: Authorization: Bearer sk-hc-v1-... (same as all other proxy endpoints)

Example response

{
  "data": {
    "label": "My Key",
    "is_unlimited": false,
    "limit": 4,
    "usage": 1.23,
    "limit_remaining": 2.77
  }
}
Field Type Description
label string The name assigned to the API key
is_unlimited boolean Whether the key has unlimited spending
limit number | null Daily spending limit in USD (null if unlimited)
usage number Today's spending in USD (logged costs + pending charges)
limit_remaining number | null Remaining daily balance in USD (null if unlimited)

Usage with curl

curl https://ai.hackclub.com/proxy/v1/key \
  -H "Authorization: Bearer sk-hc-v1-your-key-here"

Implementation details

  • Exports computeSpent from src/middleware/limits.ts so the existing daily spending calculation (logged request costs + pending charges) is reused rather than duplicated
  • Mounts the route at /proxy/v1/key in src/routes/proxy/v1/index.ts, matching the OpenRouter pattern where key info is available alongside other API endpoints
  • Uses the existing requireApiKey middleware for Bearer token authentication
  • Includes standardLimiter for rate limiting (750 requests / 30 min, same as /stats)
  • For unlimited keys, limit and limit_remaining are null while usage still reflects actual spending

Files changed

  • src/middleware/limits.ts — export computeSpent
  • src/routes/proxy/v1/index.ts — add GET /key handler + import computeSpent

Type checking & linting

  • tsc --noEmit passes clean
  • biome check passes clean

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant