marzban-sdk
v4.1.0
Published
The complete TypeScript SDK for the Marzban API — fully typed endpoints, auto token refresh, resilient retries, a WebSocket reconnect state machine for log streaming, HMAC-verified webhooks, Zod validation and a terminal shutdown lifecycle. Isomorphic for
Maintainers
Readme
MarzbanSDK
The complete TypeScript SDK for the Marzban API.
Typed endpoints, auto token refresh, retries, WebSocket log streaming, webhooks and runtime validation — isomorphic for Node.js and the browser.
Documentation · Quick Start · API Reference
marzban-sdk bundles the infrastructure layer a serious Marzban integration
needs — auth, retries, resilient WebSocket reconnects, webhooks, structured
errors, and a clean shutdown lifecycle — so you ship features, not plumbing.
The typed API surface itself is generated straight from the official OpenAPI
spec, so every endpoint, parameter and response has full autocomplete and
never drifts from what the panel actually accepts.
Install
npm install marzban-sdkAlso works with pnpm add, yarn add, or bun add.
Quick start
import { createMarzbanSDK } from 'marzban-sdk'
// One call authenticates and wires up every API module.
// Token refresh and retries are handled for you.
const sdk = await createMarzbanSDK({
baseUrl: 'https://panel.example.com',
username: 'admin',
password: 'secret',
})
const { users } = await sdk.user.getUsers({ status: 'active', limit: 10 })
// Stream real-time logs from the core over WebSocket — auto-reconnects on drop
const stream = await sdk.logs.connectByCore({
onMessage: data => console.log(data),
})Features
- 🔠 End-to-end type safety — every endpoint, parameter and response is fully typed, generated straight from the official OpenAPI spec, with matching Zod schemas.
- 🌐 Truly cross-runtime — one package, one identical API across Node.js, Bun, Deno and the browser.
- 🔑 Flexible authentication — log in on init or hand it an existing JWT; expired sessions refresh transparently, so your code never touches a token.
- 🔁 Built-in resilience — exponential back-off with jitter for transient HTTP failures, plus a full WebSocket reconnect state machine (time-budgeted, replay-deduplicated) for log streams.
- 🎯 Classified errors —
AuthError,HttpError,ConfigurationErrorand webhook errors all extendSdkErrorwith a machine-readable code and type-guard helpers. - 📡 Real-time logs & webhooks — WebSocket log streams from the core and nodes, plus HMAC-verified inbound webhooks with typed event subscriptions.
- 🔐 Custom CA support — pass a Node
httpsAgentto trust a self-hosted panel's self-signed certificate, without disabling TLS verification. - 🧹 Explicit shutdown —
await sdk.destroy()is idempotent and terminal: it closes WebSocket streams, clears webhook listeners and the stored token, and every guarded operation on that instance rejects withSdkDestroyedErrorfrom then on.
Documentation
Full guides, configuration reference and the complete typed API live at ilmar7786.github.io/marzban-sdk:
- Installation & Quick Start
- API modules & Configuration options
- Error handling & validation
- Webhooks & WebSocket logs
- Utilities
- Lifecycle & shutdown
Contributing
This package lives in the marzban-sdk
monorepo — see the root CONTRIBUTING.md
for how to submit a patch. Found a bug or have an idea?
Open an issue.
