Skip to content

feat(apikey): a BlockRun API key works everywhere a wallet key does - #59

Merged
VickyXAI merged 1 commit into
mainfrom
feat/api-key-auth
Sep 5, 2026
Merged

feat(apikey): a BlockRun API key works everywhere a wallet key does#59
VickyXAI merged 1 commit into
mainfrom
feat/api-key-auth

Conversation

@VickyXAI

@VickyXAI VickyXAI commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

What

The SDK accepts a brk_live_… key from user.blockrun.ai anywhere it accepts a wallet private key. Requests go to api.blockrun.ai, authenticate with Authorization: Bearer …, and draw prepaid credit instead of signing an on-chain payment.

No new client type and no new constructor. The private_key parameter that all fourteen client classes already take now recognises a brk_ prefix, and BLOCKRUN_API_KEY is read when it is empty — so every existing skill and script gains the rail without a code change.

blockrun_llm/apikey.py holds the rail in one module: precedence, auth_headers, poll-URL resolution, and the two refusals. Each client's httpx.Client carries the key as a default header, which is why sixteen request sites did not have to be edited one by one.

Why it needed more than a header

Each of these fails silently rather than at import time:

api.blockrun.ai serves /v1/… at the root and answers /api/v1/… with wrong_host the account rail needs its own base URL
poll_url is minted relative to the x402 gateway's host, so it arrives as /api/v1/… resolved unchanged, every async job (video, slow images) polls a 404 until its budget runs out
the async submit answers 202 on the first POST here ImageClient raised API error: 202, VideoClient raised "Expected 402 on first POST" — both broken for keys before they started
setup_agent_wallet() minted a keyfile unconditionally with a key configured there is nothing to sign with, so it now writes nothing to disk

SolanaLLMClient / AsyncSolanaLLMClient take the key too, and no longer require the optional x402 SDK when one is present: on the account rail there is no transfer to sign.

Wallet users

Unaffected, and verified live after the change:

LLMClient  mode=wallet  addr=0xCC8c…65EF8  balance=$33.7603  free chat ok

Precedence: an explicit argument, then BLOCKRUN_API_KEY, then the wallet variables. Nothing changes until that variable is set, and passing a wallet key explicitly always opts back out. client.payment_mode makes the choice inspectable.

BLOCKRUN_API_KEY_URL is deliberately not BLOCKRUN_API_URL — the latter names an x402 gateway, and an API-key client following it would send the key to a host configured for a different rail.

Wallet-only helpers

get_balance(), get_balance_testnet() and onramp() raise a ValueError naming the helper and pointing at the dashboard. Returning 0 would be the worst available answer: indistinguishable from an empty wallet, and an agent gating on it would stop calling a well-funded account. get_wallet_address() returns "". A 402 on this rail is a credit refusal, not a challenge, so it raises a PaymentError quoting the gateway's reason and the top-up page.

Every client also raised its own "nothing configured" wording listing only wallet routes, which stopped being the whole truth the moment a key became a credential. missing_credential_error() lists both — the two tests asserting the old wording are updated.

Tests

30 new tests in tests/unit/test_apikey.py; 699 passed, 15 skipped, ruff and black clean.

tests/conftest.py clears BLOCKRUN_API_KEY for every test. Without it, every "no credential configured" test fails on the machine of anyone who actually has a key exported — which is everyone working on this.

Verified live against the real gateway with a real key: chat (paid + free), list_models, list_image_models, Grok Live Search, Exa, RPC, and the wallet-only refusals.

README

Opened with "No API keys required", which is no longer true. Now leads with the two rails, adds the API-key path to Quick Start and a full Option A section (signup, key minting, top-up — min $5, with the 5.5% + $0.30 fee charged once at purchase rather than per call — precedence, and what changes). Solana now leads where the two chains are named: sub-second settlement with the facilitator paying the fee makes it the better default; Base is unchanged and still what the bare LLMClient uses. The environment table listed two variables and omitted SOLANA_WALLET_KEY entirely; it now lists nine.

🤖 Generated with Claude Code

https://claude.ai/code/session_01T5RKUETYjxwNRqkURLnatJ

Every paid path assumed x402 — a 402 challenge, a local signature, a retry —
which made holding a wallet the price of admission. A key from user.blockrun.ai
now works in the same place, across all fourteen client classes, with no new
client type and no signature change: the private_key parameter accepts a brk_
key, and BLOCKRUN_API_KEY is read when it is empty.

Four things beyond the header, each a silent failure rather than an import
error:

- api.blockrun.ai serves /v1 at the root and answers /api/v1 with wrong_host,
  so the account rail needs its own base URL.
- poll_url is minted relative to the x402 gateway's host, so it arrives as
  /api/v1/... — resolved unchanged, every async job polls a 404 to timeout.
- The async submit answers 202 on the FIRST post here. ImageClient raised
  "API error: 202" and VideoClient raised "Expected 402 on first POST", so both
  were broken for keys before they began.
- setup_agent_wallet() minted a keyfile unconditionally. With a key configured
  there is nothing to sign with, so it writes nothing.

SolanaLLMClient and AsyncSolanaLLMClient take the key too, and no longer need
the optional x402 SDK when one is present: on the account rail there is no
transfer to sign, so the chain stops being a question.

blockrun_llm.apikey holds the rail in one module — precedence, auth_headers,
poll-URL resolution, the two refusals — rather than fourteen copies that drift.
Each client's httpx.Client carries the key as a default header, so the sixteen
request sites did not have to be edited one by one.

Wallet users are untouched: precedence is explicit argument, then
BLOCKRUN_API_KEY, then the wallet variables. BLOCKRUN_API_KEY_URL is
deliberately separate from BLOCKRUN_API_URL — the latter names an x402 gateway,
and following it would send the key to a host configured for another rail.

Wallet-only helpers refuse rather than answer wrongly. get_balance() returning
0 is indistinguishable from an empty wallet, and an agent gating on it would
stop calling a funded account.

tests/conftest.py clears BLOCKRUN_API_KEY for every test: without it, the
"no credential" tests fail on the machine of anyone who has a key exported.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01T5RKUETYjxwNRqkURLnatJ
@VickyXAI
VickyXAI merged commit 44c8e9a into main Sep 5, 2026
4 checks passed
@VickyXAI
VickyXAI deleted the feat/api-key-auth branch September 5, 2026 16:19
VickyXAI pushed a commit that referenced this pull request Sep 5, 2026
`--check` was green because it only validates the keys a marker actually
renders, and those five were current. The rest of the snapshot had drifted:
withFallback 48 → 34, withFallbackAllEntries 89 → 73, aliases 229 → 259, plus
three mcp context keys the artifact now carries. No rendered number changes, so
this is not a docs fix — it stops the next marker added for one of those keys
from rendering a stale value on day one.

Produced by `scripts/sync-brand-numbers.mjs --refresh`, no hand-edited digits.

Supersedes #51, which proposed the same refresh against a 26 Aug main. Its five
rendered numbers have since landed independently, so the branch had nothing left
to deliver and conflicted with the API-key documentation merged in #59/#60.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QMLNFbR6Jg2HBFpRUGBjyh
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