Docs/Python & Node.js SDKs

Python & Node.js SDKs

SDK reference — installation, client configuration, methods, and error handling.

Zero-dependency SDKs for Python and Node.js. Every RoutePlex API feature is exposed with typed responses and a complete error class hierarchy.

Install

bash
# Python 3.8+
pip install routeplex

# Node 18+
npm install @routeplex/node

Initialize

python
from routeplex import RoutePlex
client = RoutePlex(api_key="YOUR_API_KEY")
javascript
import { RoutePlex } from "@routeplex/node";
const client = new RoutePlex({ apiKey: "YOUR_API_KEY" });

chat() — chat completion

Synchronous request. Returns a ChatResponse. See the full request/response schema in the Chat Completions reference.

python
client.chat("Hello!")                                # auto-routing
client.chat("Summarize", strategy="cost")            # strategy-biased
client.chat("Explain", model="gpt-4o-mini")          # manual model
client.chat("Write a test", test_mode=True)          # default-tier only
javascript
await client.chat("Hello!");
await client.chat("Summarize", { strategy: "cost" });
await client.chat("Explain", { model: "gpt-4o-mini" });
await client.chat("Write a test", { testMode: true });

chat_stream() / chatStream() — streaming

Yields events in real time. Control the pacing with stream_mode / streamMode:

  • "buffered" (default) — smooth, sentence-paced chunks
  • "realtime" — lowest-latency character-level events

Event payload schema is documented under Chat Completions → Streaming.

python
for event in client.chat_stream("Tell me a joke", stream_mode="realtime"):
    if event.type == "delta":
        print(event.content, end="", flush=True)
javascript
const stream = await client.chatStream("Tell me a joke", { streamMode: "realtime" });
for await (const event of stream) {
  if (event.type === "delta") process.stdout.write(event.content);
}

estimate() — cost estimation (free, no auth)

Returns an EstimateResponse with estimated_cost_usd, estimated_tokens, and the model that would be selected. Full response schema: Estimate Cost reference.

python
est = client.estimate("Write a blog post about AI")
print(est.estimated_cost_usd)
javascript
const est = await client.estimate("Write a blog post about AI");
console.log(est.estimatedCostUsd);

enhance() — prompt enhancement (free, no auth)

Improves weak prompts before sending to a model.

python
result = client.enhance("tell me about kubernetes")
print(result.enhanced_prompt)
javascript
const r = await client.enhance("tell me about kubernetes");
console.log(r.enhancedPrompt);

list_models() / listModels() — model catalog (free, no auth)

python
for m in client.list_models():
    print(f"{m.id} ({m.provider}) — {m.tier}")
javascript
const models = await client.listModels();

Error Handling

Every API failure raises a typed error. Catch the specific class for targeted handling, or the base RoutePlexError for a blanket catch. HTTP status codes map directly from the API response.

ClassHTTPRetryThrown when
AuthenticationError401✗API key is missing, invalid, or expired
ValidationError400✗Malformed body, unsupported model, or bad parameters
ContentPolicyError451✗Request blocked by moderation
RateLimitError429✓RPM, daily token, or daily cost limit hit
ProviderError5xx✓Upstream model provider failed or timed out
RoutePlexError——Base class — catches everything above

Python — catch and handle

python
from routeplex import (
    RoutePlex,
    AuthenticationError,
    RateLimitError,
    ValidationError,
    ProviderError,
    ContentPolicyError,
)

try:
    response = client.chat("Hello!")
    print(response.output)
except AuthenticationError:
    print("Invalid API key")
except RateLimitError as e:
    print(f"Rate limited: {e.code} — {e.message}")
except ValidationError as e:
    print(f"Bad request: {e.message}")
except ContentPolicyError:
    print("Content blocked by moderation")
except ProviderError:
    print("All upstream providers failed")

Node.js — catch and handle

javascript
import {
  RoutePlex,
  AuthenticationError,
  RateLimitError,
  ValidationError,
  ProviderError,
  ContentPolicyError,
} from "@routeplex/node";

try {
    const response = await client.chat("Hello!");
    console.log(response.output);
} catch (err) {
    if (err instanceof AuthenticationError) {
        console.log("Invalid API key");
    } else if (err instanceof RateLimitError) {
        console.log(`Rate limited: ${err.code} — ${err.message}`);
    } else if (err instanceof ValidationError) {
        console.log(`Bad request: ${err.message}`);
    } else if (err instanceof ContentPolicyError) {
        console.log("Content blocked by moderation");
    } else if (err instanceof ProviderError) {
        console.log("All upstream providers failed");
    } else {
        throw err;
    }
}

For rate-limit back-off, the Retry-After response header carries the recommended wait in seconds. When implementing retry loops, treat AuthenticationError, ValidationError, and ContentPolicyError as non-retriable (fail fast) and RateLimitError / ProviderError as retriable with back-off.

Each SDK error class wraps multiple API error codes. RateLimitError, for example, covers rate_limited, daily_limit_exceeded, and daily_cost_limit_exceeded. For the full code catalog — every possible error.code value with its HTTP status, JSON response schema, and retry guidance — see the Error Codes reference. The end-to-end retry strategy sample is there too.

Dependencies

Neither SDK ships any runtime dependencies. Python uses stdlib urllib + json; Node uses the built-in fetch API (requires Node 18+).

TypeScript Types

The Node.js SDK exports full type definitions — ChatResponse, Usage, EstimateResponse, EnhanceResponse, Model, and every error class.

Links

RoutePlex AI
Always online

Good afternoon!

Ask me anything about RoutePlex — APIs, SDKs, pricing, or setup.

Common questions

Powered by RoutePlex