Skip to content

Instantly share code, notes, and snippets.

@metrofx
Created April 8, 2026 09:37
Show Gist options
  • Select an option

  • Save metrofx/dd5403ce047ce642752997af75e4f735 to your computer and use it in GitHub Desktop.

Select an option

Save metrofx/dd5403ce047ce642752997af75e4f735 to your computer and use it in GitHub Desktop.
OpenRouter API Key Status Checker
#!/usr/bin/env python3
"""
OpenRouter API Key Status Checker
Usage:
python check_openrouter_key.py <API_KEY>
python check_openrouter_key.py -k <API_KEY>
python check_openrouter_key.py --test [--scenario ok|low|exhausted|unlimited]
"""
import argparse
import json
import os
import sys
import requests
# ANSI Colors
RED = '\033[0;31m'
GREEN = '\033[0;32m'
YELLOW = '\033[1;33m'
BLUE = '\033[0;34m'
CYAN = '\033[0;36m'
MAGENTA = '\033[0;35m'
NC = '\033[0m'
# --- Test data ---
TEST_SCENARIOS = {
"ok": {
"label": "sk-or-v1-test-ok", "limit": 50.25, "limit_remaining": 17.53,
"limit_reset": "monthly", "usage": 32.72, "usage_daily": 5.50,
"usage_weekly": 15.25, "usage_monthly": 32.72,
"byok_usage": 0, "byok_usage_daily": 0, "byok_usage_weekly": 0,
"byok_usage_monthly": 0, "is_free_tier": False, "include_byok_in_limit": False
},
"low": {
"label": "sk-or-v1-test-low", "limit": 100.00, "limit_remaining": 5.00,
"limit_reset": "daily", "usage": 95.00, "usage_daily": 95.00,
"usage_weekly": 95.00, "usage_monthly": 95.00,
"byok_usage": 0, "byok_usage_daily": 0, "byok_usage_weekly": 0,
"byok_usage_monthly": 0, "is_free_tier": False, "include_byok_in_limit": False
},
"exhausted": {
"label": "sk-or-v1-test-exhausted", "limit": 50.00, "limit_remaining": 0.00,
"limit_reset": "monthly", "usage": 50.00, "usage_daily": 10.00,
"usage_weekly": 30.00, "usage_monthly": 50.00,
"byok_usage": 0, "byok_usage_daily": 0, "byok_usage_weekly": 0,
"byok_usage_monthly": 0, "is_free_tier": False, "include_byok_in_limit": False
},
"unlimited": {
"label": "sk-or-v1-test-unlimited", "limit": None, "limit_remaining": None,
"limit_reset": None, "usage": 123.45, "usage_daily": 10.00,
"usage_weekly": 50.00, "usage_monthly": 123.45,
"byok_usage": 0, "byok_usage_daily": 0, "byok_usage_weekly": 0,
"byok_usage_monthly": 0, "is_free_tier": False, "include_byok_in_limit": False
},
}
def parse_args():
parser = argparse.ArgumentParser(
description="OpenRouter API Key Status Checker",
formatter_class=argparse.RawTextHelpFormatter
)
parser.add_argument("api_key", nargs="?", help="OpenRouter API key")
parser.add_argument("-k", "--key", dest="api_key_flag", help="OpenRouter API key")
parser.add_argument("--test", action="store_true", help="Run in test mode")
parser.add_argument(
"--scenario", default=None,
choices=["ok", "low", "exhausted", "unlimited"],
help="Test scenario (default: ok)"
)
return parser.parse_args()
def fetch_key_data(api_key: str) -> dict:
resp = requests.get(
"https://openrouter.ai/api/v1/key",
headers={"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"},
timeout=10
)
if resp.status_code != 200:
print(f"{RED}Error: HTTP {resp.status_code}{NC}")
try:
print(json.dumps(resp.json(), indent=2))
except Exception:
print(resp.text)
sys.exit(1)
return resp.json().get("data", {})
def progress_bar(percent: int, width: int = 30, color: str = GREEN) -> str:
filled = int((percent * width) / 100)
empty = width - filled
bar = "█" * filled + "░" * empty
return f"{color}[{bar}{NC}{color}] {percent}%{NC}"
def main():
args = parse_args()
api_key = args.api_key_flag or args.api_key
print(f"{CYAN}━━━━{NC}")
print(f"{CYAN} OpenRouter API Key Status Checker{NC}")
print(f"{CYAN}━━━━{NC}\n")
# --- Get data ---
if args.test:
print(f"{YELLOW}Running in TEST MODE{NC}")
scenario = args.scenario or os.environ.get("TEST_SCENARIO", "ok")
data = TEST_SCENARIOS.get(scenario, TEST_SCENARIOS["ok"])
else:
if not api_key:
print(f"{RED}Error: No API key provided{NC}")
sys.exit(1)
api_key = api_key.strip()
if not api_key.startswith("sk-or-"):
print(f"{YELLOW}Warning: API key doesn't match expected format (sk-or-*){NC}")
data = fetch_key_data(api_key)
# --- Parse fields ---
label = data.get("label") or "N/A"
limit = data.get("limit")
limit_remaining = data.get("limit_remaining")
limit_reset = data.get("limit_reset") or "none"
usage = data.get("usage") or 0
usage_daily = data.get("usage_daily") or 0
usage_weekly = data.get("usage_weekly") or 0
usage_monthly = data.get("usage_monthly") or 0
byok_usage = data.get("byok_usage") or 0
is_free_tier = data.get("is_free_tier", False)
include_byok = data.get("include_byok_in_limit", False)
is_unlimited = limit is None
# --- Status logic ---
if is_unlimited:
percent_used = 0
limit_display = "Unlimited"
limit_remaining_display = "Unlimited"
status_color = GREEN
status = "Unlimited"
else:
percent_used = max(0, min(100, round((1 - limit_remaining / limit) * 100)))
limit_display = f"${limit:.2f}"
limit_remaining_display = f"${limit_remaining:.2f}"
if limit_remaining <= 0:
status_color = RED
status = "EXHAUSTED"
elif percent_used >= 90:
status_color = YELLOW
status = "LOW"
else:
status_color = GREEN
status = "OK"
# --- Output ---
print(f"{BLUE}Key Information:{NC}")
print(f" Label: {CYAN}{label}{NC}")
print(f" Free Tier: {is_free_tier}\n")
print(f"{BLUE}Credit Status:{NC}")
print(f" Limit: {CYAN}{limit_display}{NC}")
print(f" Remaining: {status_color}{limit_remaining_display}{NC}")
print(f" Status: {status_color}{status}{NC}")
print(f" Reset: {CYAN}{limit_reset}{NC}\n")
print(f"{BLUE}Usage Tracking:{NC}")
print(f" All Time: {CYAN}${usage:.2f}{NC}")
print(f" Today: {CYAN}${usage_daily:.2f}{NC}")
print(f" This Week: {CYAN}${usage_weekly:.2f}{NC}")
print(f" This Month: {CYAN}${usage_monthly:.2f}{NC}\n")
if byok_usage > 0:
print(f"{BLUE}BYOK Usage:{NC}")
print(f" Total: {CYAN}${byok_usage:.2f}{NC}")
print(f" In Limit: {CYAN}{include_byok}{NC}\n")
if not is_unlimited:
print(f"{BLUE}Usage:{NC}")
print(f" {progress_bar(percent_used, color=status_color)}\n")
print(f"{CYAN}━━━━{NC}\n")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment