mirror of
https://github.com/Routstr/protocol.git
synced 2026-08-09 07:44:59 +00:00
1.9 KiB
1.9 KiB
RIP-01: OpenAI-API Proxy with Cashu Payments
Defines the HTTP proxy interface forwarding OpenAI-compatible API requests, with per-request micropayment handling via Cashu tokens.
Endpoints
POST /{path:path}
Forward proxied requests to the upstream AI service at UPSTREAM_BASE_URL.
Authorization
- Header:
Authorization: Bearer <api-key>orBearer <cashu-token> - API keys: prefixed
sk-, validated against stored hashed keys. - Cashu tokens: prefixed
cashu, redeemed via Cashu L3 mint.
Payment Flow
- Validation: Verify bearer key and ensure balance ≥
COST_PER_REQUESTmsats. - Charge Base: Deduct
COST_PER_REQUESTfrom user balance; record request count. - Forward: Send request upstream, applying
UPSTREAM_API_KEYoverride if set. - Token-Based Pricing (Optional): For chat/completions when
MODEL_BASED_PRICING=true:- Parse upstream
usage.prompt_tokensandusage.completion_tokens. - Compute
input_msats= prompt_tokens/1000 × model.prompt_price. - Compute
output_msats= completion_tokens/1000 × model.completion_price. - Charge or refund the difference via balance adjustment.
- Emit SSE event
data: {"cost": {...}}appended to stream.
- Parse upstream
Responses
- Streaming (
text/event-stream): Proxy SSE chunks and inject a final cost event. - Non-Streaming (
application/json): Embed a top-level"cost": {...}field in JSON.
Error Codes
401 Unauthorized: Missing or invalid API key/Cashu token.402 Payment Required: Insufficient balance.502 Bad Gateway: Upstream service unavailable.500 Internal Server Error: Unexpected errors.
Configuration
COST_PER_REQUEST: Base cost in msats (envCOST_PER_REQUEST).MODEL_BASED_PRICING: Enable token pricing (envMODEL_BASED_PRICING).COST_PER_1K_INPUT_TOKENS&COST_PER_1K_OUTPUT_TOKENS: Fallback pricing ifmodels.jsonnot present.