feat: add GET /proxy/v1/key endpoint for API key balance info#122
Open
gorkie-agent wants to merge 2 commits into
Open
feat: add GET /proxy/v1/key endpoint for API key balance info#122gorkie-agent wants to merge 2 commits into
gorkie-agent wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds a new
GET /proxy/v1/keyendpoint 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/keyendpoint.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:
How it works
Endpoint:
GET /proxy/v1/keyAuth:
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 } }labelis_unlimitedlimitusagelimit_remainingUsage with curl
curl https://ai.hackclub.com/proxy/v1/key \ -H "Authorization: Bearer sk-hc-v1-your-key-here"Implementation details
computeSpentfromsrc/middleware/limits.tsso the existing daily spending calculation (logged request costs + pending charges) is reused rather than duplicated/proxy/v1/keyinsrc/routes/proxy/v1/index.ts, matching the OpenRouter pattern where key info is available alongside other API endpointsrequireApiKeymiddleware for Bearer token authenticationstandardLimiterfor rate limiting (750 requests / 30 min, same as/stats)limitandlimit_remainingarenullwhileusagestill reflects actual spendingFiles changed
src/middleware/limits.ts— exportcomputeSpentsrc/routes/proxy/v1/index.ts— addGET /keyhandler + importcomputeSpentType checking & linting
tsc --noEmitpasses cleanbiome checkpasses clean