Skip to main content

Sync and Async Object-oriented Python SDK for the 3x-ui app.

Project description

Sync and Async Object-oriented Python SDK for the 3x-ui API.

OverviewCompatibility TableQuick StartExamplesBugs and Feature RequestsPyPI

GitHub release (latest SemVer) GitHub issues Build Status Checked with mypy PyPI - Downloads
PyPI - Python Version PyPI - Version Maintainability Test Coverage

Overview

This SDK is designed to interact with the 3x-ui app in a more object-oriented way. It provides both synchronous and asynchronous methods to interact with the app. The SDK is designed to be as simple as possible to use, while still providing a lot of flexibility and uses Pydantic models to validate the data.
Used dependencies:

  • requests for synchronous API
  • httpx for asynchronous API
  • pydantic for models

Supported Python versions:

  • 3.11
  • 3.12

Compatibility Table

Since the 3x-ui app is under development, the SDK may not be compatible with all versions of the app. The table below shows the compatibility between the SDK and the 3x-ui app. Since the developer of SDK is not related to the 3x-ui app, the latest versions of the software are not guaranteed to be compatible with the SDK. It's recommended to use the specified versions of the software to avoid any issues.

py3xui Version 3x-ui Version
>=0.2.2 >=2.3.9, <=2.3.11
0.2.1 >=2.3.7

The SDK does not support older versions of the 3x-ui app.

Quick Start

You can use both synchronous and asynchronous methods to interact with the 3x-ui app. Both APIs have the same methods and return the same data, so it's up to you to choose which one to use.
After installing the SDK, you can create a new instance of the API. When creating a new instance, you can either use environment variables or pass the credentials directly. It's strongly recommended to use environment variables to store the API credentials.
On creation, the Api won't connect to the 3x-ui app, so you can spawn new instances without spending resources. But after creating an instance, you'll need to call the login method to authenticate the user and save the cookie for future requests.

Installation

pip install py3xui

Create a new instance of the SDK

It's recommended to use an environment variable to store the API credentials:

import os

os.environ["XUI_HOST"] = "http://your-3x-ui-host.com:2053"
os.environ["XUI_USERNAME"] = "your-username"
os.environ["XUI_PASSWORD"] = "your-password"

To work synchronously:

from py3xui import Api

# Using environment variables:
api = Api.from_env()

# Or using the credentials directly:
api = Api("http://your-3x-ui-host.com:2053", "your-username", "your-password")

To work asynchronously:

from py3xui import AsyncApi

# Using environment variables:
api = AsyncApi.from_env()

# Or using the credentials directly:
api = AsyncApi("http://your-3x-ui-host.com:2053", "your-username", "your-password")

*️⃣ If you're using a custom URI Path, ensure that you've added it to the host, for example:
If your host is http://your-3x-ui-host.com:2053 and the URI Path is /test/, then the host should be http://your-3x-ui-host.com:2053/test/.
Otherwise, all API requests will fail with a 404 error.

*️⃣ If you're using a secret token, which is set in in the 3x-ui panel, you'll also add it, otherwise all API request will fail.
Same as for other credentials, you can use an environment variable to store the token:

...
os.environ["XUI_TOKEN"] = "your-token"

api = Api.from_env()

Or pass it directly, when creating an instance:

api = Api("http://your-3x-ui-host.com:2053", "your-username", "your-password", "your-token")

Using TLS and custom certificates

Interacting with server over HTTPS requires careful management of TLS verification to ensure secure communications. This SDK provides options for setting TLS configurations, which include specifying custom certificates for increased trust or disabling TLS verification when necessary.

Case 1: Disabling TLS verification

For development, you can disable TLS verification. This is not recommended for production due to the increased risk of security threats like man-in-the-middle attacks.

api = Api("http://your-3x-ui-host.com:2053", "your-username", "your-password", use_tls_verify=False)

❗ Warning: Never disable TLS verification in production.

Case 2: Using сustom сertificates

If you are interacting with a server that uses a self-signed certificate or one not recognized by the standard CA bundle, you can specify a custom certificate path:

api = Api(
    "http://your-3x-ui-host.com:2053",
    "your-username",
    "your-password",
    custom_certificate_path="/path/to/your/certificate.pem",
)

This allows you to maintain TLS verification by providing a trusted certificate explicitly.

Login

No matter which API you're using or if was it created using environment variables or credentials, you'll need to call the login method to authenticate the user and save the cookie for future requests.

from py3xui import Api, AsyncApi

api = Api.from_env()
api.login()

async_api = AsyncApi.from_env()
await async_api.login()

Examples

You'll find detailed docs with usage examples for both APIs and for used models in the corresponding package directories:

In this section, you'll find some examples of how to use the SDK. In the examples, we'll use the synchronous API, but you can use the asynchronous API in the same way, just remember to use await before calling the methods.

Get inbounds list

from py3xui import Api, Inbound

api = Api.from_env()
api.login()
inbounds: List[Inbound] = api.inbound.get_list()

Add a new inbound

from py3xui import Api
from py3xui.inbound import Inbound, Settings, Sniffing, StreamSettings

api = Api.from_env()
api.login()

settings = Settings()
sniffing = Sniffing(enabled=True)

tcp_settings = {
    "acceptProxyProtocol": False,
    "header": {"type": "none"},
}
stream_settings = StreamSettings(security="reality", network="tcp", tcp_settings=tcp_settings)

inbound = Inbound(
    enable=True,
    port=443,
    protocol="vless",
    settings=settings,
    stream_settings=stream_settings,
    sniffing=sniffing,
    remark="test3",
)

api.inbound.add(inbound)

Get a client by email

from py3xui import Api, Client

api = Api.from_env()
api.login()

client: Client = api.client.get_by_email("some-email")

Add a new client

from py3xui import Api, Client

api = Api.from_env()
api.login()

new_client = Client(id=str(uuid.uuid4()), email="test", enable=True)
inbound_id = 1

api.client.add(inbound_id, [new_client])

Bugs and Feature Requests

If you find a bug or have a feature request, please open an issue on the GitHub repository.
You're also welcome to contribute to the project by opening a pull request.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py3xui-0.2.7.tar.gz (28.8 kB view details)

Uploaded Source

Built Distribution

py3xui-0.2.7-py3-none-any.whl (35.0 kB view details)

Uploaded Python 3

File details

Details for the file py3xui-0.2.7.tar.gz.

File metadata

  • Download URL: py3xui-0.2.7.tar.gz
  • Upload date:
  • Size: 28.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for py3xui-0.2.7.tar.gz
Algorithm Hash digest
SHA256 3812c1bf164a5e565585a20f4b088dd0b8f837990430f9c9f877907a0aa302ce
MD5 e2e6654a9253ead98fa5bca958b85345
BLAKE2b-256 de7861dfb89b07c2196ccb0bcb4c3d55192c5d8d459ac924f849fe38568c0ffa

See more details on using hashes here.

File details

Details for the file py3xui-0.2.7-py3-none-any.whl.

File metadata

  • Download URL: py3xui-0.2.7-py3-none-any.whl
  • Upload date:
  • Size: 35.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for py3xui-0.2.7-py3-none-any.whl
Algorithm Hash digest
SHA256 ab3906ed6217664f9dc0ac3aa0c00b50d639bbe60f38836f84430f23cf761b47
MD5 2880f18d4cb3aa5ccee30ee44321ba32
BLAKE2b-256 4a01903c54bfab3d36f1b2d83a5e80735ba817f6defb2c3b0cc8d8681dca6905

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page