MarzbanSDK
Configuration

Config Options

Every MarzbanSDK configuration field — `baseUrl`, auth, `timeout`, `retries`, logger and webhook secret — with defaults and Zod validation rules.

The object is passed to createMarzbanSDK or the MarzbanSDK constructor. All fields are validated by Zod before the SDK boots — invalid config throws a immediately.

Reference

FieldTypeRequiredDefaultDescription
baseUrlstringYesBase URL of the Marzban instance. Must be a valid URL with protocol. Example: https://vpn.example.com
usernamestringYesAdmin username for authentication (non-empty).
passwordstringYesAdmin password for authentication (non-empty).
tokenstringNoExisting JWT token. If provided, the SDK uses it directly instead of calling the login endpoint.
authenticateOnInitbooleanNotrueWhen true, createMarzbanSDK calls authorize() before returning. Set to false for deferred or manual auth.
timeoutnumberNo30000HTTP request timeout in milliseconds. Pass 0 to disable (wait forever).
retriesnumberNo3Number of automatic retries for failed HTTP requests and WebSocket reconnections. Uses exponential backoff.
loggerfalse | | Noenv-awareLogging configuration. See Logging.
webhook{ secret?: string }NoWebhook configuration. Set secret to enable HMAC-SHA256 signature verification.
httpAgentNoNode.js http.Agent (or compatible) used for http: requests. Ignored in browsers.
httpsAgentNoNode.js https.Agent (or compatible) used for https: requests and the WebSocket log stream. Ignored in browsers. See Self-signed certificates below.

Minimal config

import { createMarzbanSDK } from 'marzban-sdk'

const sdk = await createMarzbanSDK({
  baseUrl: 'https://vpn.example.com',
  username: 'admin',
  password: 'secret',
})

Full config example

import { createMarzbanSDK } from 'marzban-sdk'

const sdk = await createMarzbanSDK({
  baseUrl: 'https://vpn.example.com',
  username: 'admin',
  password: 'secret',
  timeout: 15_000,        // 15 seconds
  retries: 5,
  authenticateOnInit: true,
  logger: {
    level: 'debug',
    timestamp: true,
  },
  webhook: {
    secret: process.env.MARZBAN_WEBHOOK_SECRET,
  },
})

Supply an existing token

If you manage JWT tokens yourself, skip the login step by providing the token field:

const sdk = await createMarzbanSDK({
  baseUrl: 'https://vpn.example.com',
  username: 'admin',
  password: 'secret',
  token: 'eyJhbGciOi...', // already-obtained JWT
  authenticateOnInit: false,
})

If the supplied token expires, the SDK will re-authenticate using username and password automatically. Both fields are still required.

Self-signed certificates & custom CA

Self-hosted Marzban panels are frequently served behind a self-signed or internal-CA certificate. Rather than disabling TLS verification, pass a Node https.Agent configured with the CA to trust:

import { readFileSync } from 'node:fs'
import https from 'node:https'
import { createMarzbanSDK } from 'marzban-sdk'

const sdk = await createMarzbanSDK({
  baseUrl: 'https://panel.example.com',
  username: 'admin',
  password: 'secret',
  httpsAgent: new https.Agent({ ca: readFileSync('ca.pem') }),
})

The same httpsAgent also covers sdk.logs — see WebSocket Logs. httpAgent/httpsAgent are Node-only: they're ignored (with a warning) in the browser, where there's no way to hand a custom CA to the native fetch/WebSocket implementations.

Don't set rejectUnauthorized: false on the agent to work around a certificate error — that disables verification entirely, for every connection the agent makes, not just the one you're debugging. Trust the specific CA instead, as shown above.

Retry behaviour

When retries > 0, failed HTTP requests are retried with exponential backoff:

AttemptDelay
11 s
22 s
34 s
… (capped at 30 s)

WebSocket reconnections after 403 Forbidden also use the same retries limit.

Set retries: 0 to disable automatic retries.

Validation errors

If you pass an invalid config (e.g. missing baseUrl, non-integer timeout), the SDK throws a before any network call is made:

import { isConfigurationError } from 'marzban-sdk'

try {
  const sdk = await createMarzbanSDK({ baseUrl: '', username: '', password: '' })
} catch (err) {
  if (isConfigurationError(err)) {
    console.error(err.message) // "Invalid SDK configuration"
    console.error(err.details) // Zod validation issues
  }
}

On this page