poolswitch-node
v0.1.0
Published
Embedded and proxy-ready Node.js client for PoolSwitch.
Maintainers
Readme
poolswitch-node
poolswitch-node lets JavaScript and TypeScript apps use PoolSwitch in two ways:
- directly inside your app with the embedded
PoolSwitchClient - against a local or shared proxy with
PoolSwitchProxyClient
Install
npm install poolswitch-nodeUsage
Embedded client
import { PoolSwitchClient } from "poolswitch-node";
const client = new PoolSwitchClient({
upstreamBaseUrl: "https://api.openai.com",
keys: [
{ id: "openai-free-1", value: process.env.OPENAI_KEY_1! },
{ id: "openai-free-2", value: process.env.OPENAI_KEY_2! }
],
strategy: "quota_failover"
});
async function main() {
const response = await client.post("/v1/chat/completions", {
json: {
model: "gpt-4o-mini",
messages: [{ role: "user", content: "Hello from the embedded client" }]
}
});
console.log(response.choices[0].message.content);
console.log(client.status().keys);
}
main().catch((error) => {
console.error(error.reason, error.status, error.data);
});Proxy client
const { PoolSwitchProxyClient } = require("poolswitch-node");
const client = new PoolSwitchProxyClient("http://localhost:8080", {
timeout: 30000,
headers: {
"x-app-name": "demo"
}
});
async function main() {
const response = await client.post("/v1/chat/completions", {
json: {
model: "gpt-4.1-mini",
messages: [{ role: "user", content: "Hello from the proxy" }]
}
});
console.log(response.status);
console.log(response.data);
}
main().catch((error) => {
console.error(error.status, error.data);
});API
new PoolSwitchClient(options)
options.upstreamBaseUrl: Upstream API base URL such ashttps://api.openai.comoptions.keys: Array of API keys as strings or{ id, value, monthlyQuota }objectsoptions.strategy:round_robin,least_used,random, orquota_failoveroptions.retryAttempts: Maximum attempts per requestoptions.cooldownSeconds: Cooldown for quota-exhausted keysoptions.authHeaderName: Header name for key injection, defaultAuthorizationoptions.authScheme: Auth scheme, defaultBeareroptions.headers: Default headers sent with every requestoptions.timeout: Request timeout in millisecondsoptions.fetchImpl: Custom fetch implementation for tests or alternate runtimes
new PoolSwitchProxyClient(baseUrl, options?)
baseUrl: Proxy base URL such ashttp://localhost:8080options.headers: Default headers sent with every requestoptions.timeout: Request timeout in millisecondsoptions.fetchImpl: Custom fetch implementation for testing or alternate runtimes
Embedded client methods
client.get(path, options?)client.post(path, options?)client.put(path, options?)client.patch(path, options?)client.delete(path, options?)client.status()
Embedded responses return parsed JSON for JSON APIs, or plain text for text responses.
Proxy client methods
client.request(method, path, options?)
Supported options:
headers: Per-request headersjson: JSON-serializable request bodybody: Raw request bodyquery: Query string objecttimeout: Per-request timeout overridesignal: OptionalAbortSignal
Returns:
statusokheadersdatatext
Errors:
- Throws
PoolSwitchErrorfor non-2xx responses or transport failures. error.dataincludes parsed JSON when available.
Convenience methods
client.get(path, options?)client.post(path, options?)client.put(path, options?)client.patch(path, options?)client.delete(path, options?)
Notes
- Requires Node.js 18 or newer because it uses the built-in
fetch. - Supports both ESM
importand CommonJSrequire. - JSON responses are parsed automatically when possible.
- Non-JSON responses are returned as text.
