UNPKG

marzban-sdk

Version:

Fully typed client SDK for the Marzban API, supporting both browser and Node.js environments.

228 lines (158 loc) β€’ 8.79 kB
<div align="center"> <img src="./docs/logo.png" alt="MarzbanSDK" width="320px" height="320px" /> </div> # πŸš€ MarzbanSDK <div align="center"> [![npm version](https://img.shields.io/npm/v/marzban-sdk)](https://www.npmjs.com/package/marzban-sdk/v/latest) [![npm downloads](https://img.shields.io/npm/dm/marzban-sdk)](https://www.npmjs.com/package/marzban-sdk) [![total downloads](https://img.shields.io/npm/dt/marzban-sdk)](https://www.npmjs.com/package/marzban-sdk) [![license](https://img.shields.io/npm/l/marzban-sdk)](https://github.com/Ilmar7786/marzban-sdk/blob/main/LICENSE) [![GitHub stars](https://img.shields.io/github/stars/Ilmar7786/marzban-sdk)](https://github.com/Ilmar7786/marzban-sdk) </div> **MarzbanSDK** is a fully typed, auto-generated client library for interacting with the [Marzban](https://github.com/Gozargah/Marzban) API. It works seamlessly in both **Node.js** and **browser environments**, giving developers a clean, strongly-typed interface to Marzban’s full feature set β€” including real-time WebSocket support, token refresh handling, and robust retry mechanisms. πŸ‘‰ [View on GitHub](https://github.com/Ilmar7786/marzban-sdk) ## πŸ“– Table of Contents - [✨ Features](#-features) - [πŸ“¦ Installation](#-installation) - [πŸš€ Quick Start](#-quick-start) - [πŸ“‘ Configuration Options](#-configuration-options) - [πŸ” Authorization Control](#-authorization-control) - [πŸ” How It Works](#-how-it-works) - [πŸ“š API Documentation](#-api-documentation) - [🎯 Error Handling](#-error-handling) - [πŸ”’ Data Validation](#-data-validation) - [πŸ“¨ Webhook Support](#-webhook-support) - [πŸ› οΈ Utilities](#-utilities) - [πŸ“‘ WebSocket Support](#-websocket-support) - [🀝 Contributing](#-contributing) - [πŸ“œ License](#-license) - [⭐ Support the Project](#-support-the-project) ## ✨ Features - βœ… **First-class TypeScript Support** – Autocomplete and type safety for all inputs and responses. - πŸ” **Manual or Automatic Authorization** – Choose between explicit login with full error handling, or backward-compatible automatic login. - πŸ”„ **Auto Token Refresh** – Seamless handling of session expiration. - πŸ” **Built-in Retry Logic** – Robust handling of network errors and downtime. - 🎯 **[Classified Error System](./docs/ERRORS.md)** – Type-safe error handling with detailed error codes and guards. - πŸ”’ **[Zod Validation](./docs/VALIDATION.md)** – Runtime type validation for configuration and API payloads. - πŸ“¨ **[Webhook Support](./docs/WEBHOOK.md)** – Real-time event handling with signature verification and payload validation. - πŸ› οΈ **[Utility Functions](./docs/UTILITIES.md)** – Helpers for bytes conversion, datetime calculations, and template variables. - πŸ“‘ **[Real-time WebSocket Logging](./docs/WEBSOCKET.md)** – Stream logs from the core and nodes with ease. - πŸ“˜ **Generated from OpenAPI** – Always up-to-date with the official Marzban API. ## πŸ“¦ Installation Install MarzbanSDK via npm: ```sh npm install marzban-sdk ``` Or using yarn: ```sh yarn add marzban-sdk ``` ## πŸ“‘ Configuration Options The `Config` object is used to initialize the MarzbanSDK instance. Below are all available options: | Name | Type | Required | Default | Description | | -------------------- | ------- | -------- | ------- | -------------------------------------------------------------------------------------------------- | | `baseUrl` | string | Yes | β€” | The base URL of the Marzban API instance. Example: `https://api.example.com` | | `username` | string | Yes | β€” | The username for authentication. | | `password` | string | Yes | β€” | The password for authentication. | | `token` | string | No | β€” | Optional JWT token for direct authorization. If provided, SDK uses this token for requests. | | `retries` | number | No | 3 | Number of automatic retries for failed HTTP requests. | | `authenticateOnInit` | boolean | No | true | If true (default), SDK authenticates automatically on init. If false, call `authorize()` manually. | ## πŸ” Authorization Control MarzbanSDK gives you full control over authentication: - **Automatic authentication** (default): The SDK logs in as soon as you create an instance. - **Manual authentication**: Set `authenticateOnInit: false` to delay login and handle errors yourself. ```typescript import { createMarzbanSDK, isAuthError } from 'marzban-sdk' const sdk = await createMarzbanSDK({ baseUrl: 'https://api.example.com', username: 'admin', password: 'secret', authenticateOnInit: false, // Manual mode }) try { await sdk.authorize() // Explicit login // Now you can make authenticated requests } catch (e) { if (isAuthError(e)) { // Handle authentication failure console.error('Invalid credentials') } } ``` You can also force re-authentication at any time: ```typescript await sdk.authorize(true) // Force a new login, even if already authenticated ``` See [Error Handling Guide](./docs/ERRORS.md) for comprehensive error handling patterns. ## πŸš€ Quick Start ```typescript import { createMarzbanSDK } from 'marzban-sdk' // Create SDK instance with automatic authentication const sdk = await createMarzbanSDK({ baseUrl: 'https://api.example.com', username: 'your-username', password: 'your-password', }) // Or with manual authentication const sdkManual = await createMarzbanSDK({ baseUrl: 'https://api.example.com', username: 'your-username', password: 'your-password', authenticateOnInit: false, }) await sdkManual.authorize() // Fetch user details const user = await sdk.user.getUser('username') console.log(user) // Get an authorization token const token = sdk.getAuthToken() console.log(token) ``` ## πŸ” How It Works ### **1️⃣ Full Typing and Schema** MarzbanSDK provides full TypeScript typing and schema definitions for all API methods, parameters, and responses. ### **2️⃣ Generated Sources** The SDK is **auto-generated from the OpenAPI specification**, ensuring it stays up-to-date with API changes. - The entry point for the SDK is the **`MarzbanSDK`** class. - All API methods are dynamically generated based on the OpenAPI schema. ## πŸ“š API Documentation For detailed API reference, visit the [API Documentation](./docs/API_DOCUMENTATION.md). ## 🎯 Error Handling MarzbanSDK provides a comprehensive error handling system with classified errors, type-safe guards, and detailed error information. Learn how to handle: - **Authentication errors** – Login and token failures - **Configuration errors** – Invalid SDK setup - **HTTP errors** – Network failures - **Webhook errors** – Invalid signatures and payloads β†’ See [Error Handling Guide](./docs/ERRORS.md) ## πŸ”’ Data Validation All SDK configuration and API payloads are validated using **Zod** at runtime, ensuring type safety both at compile-time and runtime. β†’ See [Validation Guide](./docs/VALIDATION.md) ## πŸ“¨ Webhook Support Handle real-time events from Marzban with full webhook support: - **Signature verification** – HMAC-SHA256 validation - **Payload validation** – Zod-powered type-safe schemas - **Event subscriptions** – Typed listeners with wildcards - **Batch processing** – Handle multiple webhooks at once β†’ See [Webhook Guide](./docs/WEBHOOK.md) ## πŸ› οΈ Utilities Helper utilities for common operations: - **Bytes conversion** – Parse and format data sizes (GB, MB, etc.) - **Datetime** – Date calculations and remaining time - **Template variables** – Work with Marzban host-settings variables β†’ See [Utilities Guide](./docs/UTILITIES.md) ## πŸ“‘ WebSocket Support MarzbanSDK supports WebSocket for **real-time log streaming**. You can receive logs from both the **core server** and individual **nodes**. For more details, check the [WebSocket Guide](./docs/WEBSOCKET.md). ## 🀝 Contributing We welcome contributions! If you'd like to improve MarzbanSDK, please: 1. Fork the repository πŸš€ 2. Create a new branch πŸ”§ 3. Submit a pull request πŸŽ‰ For details, check our [Contribution Guidelines](./docs/CONTRIBUTING.md). ## πŸ“œ License This project is licensed under the MIT License. ## ⭐ Support the Project If you find marzban-sdk useful, please give it a star on [GitHub](https://github.com/Ilmar7786/marzban-sdk)! It helps us stay motivated and grow the project.