Skip to content

A Python library that helps you easily use Marzban's API panel

License

Notifications You must be signed in to change notification settings

mewhrzad/marzpy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

marzpy

A Python library that helps you easily use Marzban's API panel

Important

Status: Working on new update 🔥

installation

pip install marzpy --upgrade

requirements : aiohttp

How To Use

from marzpy import Marzban
import asyncio
        
async def main():
    panel = Marzban("username","password","https://example.com")
    token = await panel.get_token()
    #await panel.anyfunction(token)

asyncio.run(main())

Features

Thanks To

Contributors

Examples

Get Token

from marzpy import Marzban

panel = Marzban("username","password","https://example.com")

mytoken = await panel.get_token()

Get Current admin

admin = await panel.get_current_admin(token=mytoken)
print(admin) #output: {'username': 'admin', 'is_sudo': True}

Create Admin

info = {'username':'test','password':'pasword','is_sudo':False}
rsault = await panel.create_admin(token=mytoken,data=info)
print(result) #output: success

Modify Admin

target_admin = "test"
info = {'password':'newpassword','is_sudo':False}
result = await panel.change_admin_password(username=target_admin,token=mytoken,data=info)
print(result) #output: success

Remove Admin

target_admin = "test"
result = await panel.delete_admin(username=target_admin,token=mytoken)
print(result) #output: success

Get All Admins

result = await panel.get_all_admins(token=mytoken)
print(result) 
#output: [{'username': 'test', 'is_sudo': True}, {'username': 'test1', 'is_sudo': False}]

User Subscription

subscription_url = "https://sub.yourdomain.com/sub/eyJhbGciOiJIUzI8NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJNbWRDcmFaeSIsImFjY2VzcyI8InN1YnNjcmlwdGlvbiIsImlhdCI1MTY5NDk1NTkxMH0.o75ML5835SPXpVPKXcvEIUxMTwSy-4XGS9NIdWOAmXY"
result = await panel.get_subscription(subscription_url)
print(result) #output: Configs

User Subscription info

subscription_url =  "https://sub.yourdomain.com/sub/eyJhbGciOiJIUzI8NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJNbWRDcmFaeSIsImFjY2VzcyI8InN1YnNjcmlwdGlvbiIsImlhdCI1MTY5NDk1NTkxMH0.o75ML5835SPXpVPKXcvEIUxMTwSy-4XGS9NIdWOAmXY"
result = await panel.get_subscription_info(subscription_url)
print(result) #output: User information (usage,links,inbounds,....)

Get System Stats

result = await panel.get_system_stats(token=mytoken)
print(result) #output: system stats Memory & CPU usage ...

Get Inbounds

result = await panel.get_inbounds(token=mytoken)
print(result) #output: list of inbounds

Get Hosts

result = await panel.get_hosts(token=mytoken)
print(result) #output: list of hosts

Modify Hosts

hosts = {
  "VMess TCP": [
    {
      "remark": "somename",
      "address": "someaddress",
      "port": 0,
      "sni": "somesni",
      "host": "somehost",
      "security": "inbound_default",
      "alpn": "",
      "fingerprint": ""
    }
  ]
}
# **Backup first**
result = await panel.modify_hosts(token=mytoken,data=hosts)
print(result) #output: hosts

Get Core Stats

result = await panel.get_xray_core(token=mytoken)
print(result)
 #output: {'version': '1.8.1', 'started': True, 'logs_websocket': '/api/core/logs'}

Restart Core

result = await panel.restart_xray_core(token=mytoken)
print(result)
 #output: success

Get Core Config

result = await panel.get_xray_config(token=mytoken)
print(result) #output: your xray core config

Modify Core Config

new_config={"your config"}
result = await panel.modify_xray_config(token=mytoken,config=new_config)
print(result) #output: success

Add User

from marzpy.api.user import User

user = User(
    username="Mewhrzad",
    proxies={
        "vmess": {"id": "35e7e39c-7d5c-1f4b-8b71-508e4f37ff53"},
        "vless": {"id": "35e7e39c-7d5c-1f4b-8b71-508e4f37ff53"},
    },
    inbounds={"vmess": ["VMess TCP"], "vless": ["VLESS TCP REALITY"]},
    expire=0,
    data_limit=0,
    data_limit_reset_strategy="no_reset",
    status="active"
)
result = await panel.add_user(user=user, token=token) #return new User object

print(result.username) #-> Mewhrzad, #user.proxies, #user.inbounds, #user.expire, #user.data_limit, #userdata_limit_reset_strategy, #user.status, #user.used_traffic, #user.lifetime_used_traffic, #user.created_at, #user.links, #user.subscription_url, #user.excluded_inbounds

Get User

result = await panel.get_user("Mewhrzad",token=mytoken) #return User object
print(result.subscription_url)

Modify User

new_user = User(
    username="test",
    proxies={
        "vmess": {"id": "35e4e39c-7d5c-4f4b-8b71-558e4f37ff53"},
        "vless": {"id": "35e4e39c-7d5c-4f4b-8b71-558e4f37ff53"},
    },
    inbounds={"vmess": ["VMess TCP"], "vless": ["VLESS TCP REALITY"]},
    expire=0,
    data_limit=0,
    data_limit_reset_strategy="no_reset",
    status="active",
)
result = await panel.modify_user("Mewhrzad", token=mytoken, user=new_user)
print(result.subscription_url) #output: modified user object

Remove User

result = await panel.delete_user("test", token=mytoken)
print(result) #output: success

Reset User Data Usage

result = await panel.reset_user_traffic("test", token=mytoken)
print(result) #output: success

Reset All Users Data Usage

result = await panel.reset_all_users_traffic(token=mytoken)
print(result) #output: success

Get All Users

result = await panel.get_all_users(token=mytoken) #return list of users
for user in result:
    print(user.username) 

Get User Usage

result = await panel.get_user_usage("mewhrzad",token=mytoken)
print(result) 
#output: [{'node_id': None, 'node_name': 'MTN', 'used_traffic': 0}, 
#{'node_id': 1, 'node_name': 'MCI', 'used_traffic': 0}]

Get All User Templates

result = await panel.get_all_templates(token=mytoken) #return template list object
for template in result:
    print(template.name)

Add User Template

from marzpy.api.template import Template

temp = Template(
    name="new_template",
    inbounds={"vmess": ["VMESS TCP"], "vless": ["VLESS TCP REALITY"]},
    data_limit=0,
    expire_duration=0,
    username_prefix=None,
    username_suffix=None,
)
result = await panel.add_template(token=mytoken, template=temp)  # return new Template object
print(result.name) #output: new_template

Get User Template

template_id = 11
result = await panel.get_template_by_id(token=mytoken, id=template_id) # return Template object
print(result.name) #output: new_template

Modify User Template

from marzpy.api.template import Template

temp = Template(
    name="new_template2",
    inbounds={"vmess": ["VMESS TCP"], "vless": ["VLESS TCP REALITY"]},
    data_limit=0,
    expire_duration=0,
    username_prefix=None,
    username_suffix=None,
)
result = await panel.modify_template_by_id(
    id=1, token=mytoken, template=temp)  # return Modified Template object
print(result.name) #output: new_template2

Remove User Template

result = await panel.delete_template_by_id(id=1, token=mytoken)
print(result) #output: success

Add Node

from marzpy.api.node import Node

my_node = Node(
    name="somename",
    address="test.example.com",
    port=62050,
    api_port=62051,
    certificate="your_cert",
    id=4,
    xray_version="1.8.1",
    status="connected",
    message="string",
)

result = await panel.add_node(token=mytoken, node=my_node)  # return new Node object
print(result.address)

Get Node

result = await panel.get_node_by_id(id=1, token=mytoken)  # return exist Node object
print(result.address) #output: address of node 1

Modify Node

from marzpy.api.node import Node

my_node = Node(
    name="somename",
    address="test.example.com",
    port=62050,
    api_port=62051,
    certificate="your_cert",
    id=4,
    xray_version="1.8.1",
    status="connected",
    message="string",
)

result = await panel.modify_node_by_id(id=1, token=mytoken,node=my_node)  # return modified Node object
print(result.address) #output:test.example.com

Remove Node

result = await panel.delete_node(id=1, token=mytoken)
print(result) #output: success

Get All Nodes

result = await panel.get_all_nodes(token=mytoken)  # return List of Node object
for node in result:
    print(node.address)

Reconenct Node

result = await panel.reconnect_node(id=1,token=mytoken)
print(result) #output: success

Get Node Usage

result = await panel.get_nodes_usage(token=mytoken)
for node in result:
    print(node)
#output:{'node_id': 1, 'node_name': 'N1', 'uplink': 1000000000000, 'downlink': 1000000000000}
# {'node_id': 2, 'node_name': 'N2', 'uplink': 1000000000000, 'downlink': 1000000000000}