py3xui


Namepy3xui JSON
Version 0.3.1 PyPI version JSON
download
home_pageNone
SummarySync and Async Object-oriented Python SDK for the 3x-ui app.
upload_time2024-11-19 16:30:27
maintainerNone
docs_urlNone
authorNone
requires_pythonNone
licenseMIT License
keywords vpn 3x-ui sdk api
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <div align="center" markdown>
<img src="https://github.com/iwatkot/py3xui/assets/118521851/42c5d579-6202-4a9e-88f3-2d844fdd95b6">

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

<p align="center">
    <a href="#Overview">Overview</a> •
    <a href="#Quick-Start">Quick Start</a> •
    <a href="#Examples">Examples</a> •
    <a href="#Bugs-and-Feature-Requests">Bugs and Feature Requests</a> •
    <a href="https://pypi.org/project/py3xui/">PyPI</a>
</p>

[![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/iwatkot/py3xui)](https://github.com/iwatkot/py3xui/releases)
[![GitHub issues](https://img.shields.io/github/issues/iwatkot/py3xui)](https://github.com/iwatkot/py3xui/issues)
[![Build Status](https://github.com/iwatkot/py3xui/actions/workflows/checks.yml/badge.svg)](https://github.com/iwatkot/py3xui/actions)
[![Checked with mypy](https://www.mypy-lang.org/static/mypy_badge.svg)](https://mypy-lang.org/)
[![PyPI - Downloads](https://img.shields.io/pypi/dm/py3xui)](https://pypi.org/project/py3xui/)<br>
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/py3xui)](https://pypi.org/project/py3xui/)
[![PyPI - Version](https://img.shields.io/pypi/v/py3xui)](https://pypi.org/project/py3xui/)
[![Maintainability](https://api.codeclimate.com/v1/badges/c03ca2bca0191cb4a2ae/maintainability)](https://codeclimate.com/github/iwatkot/py3xui/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/c03ca2bca0191cb4a2ae/test_coverage)](https://codeclimate.com/github/iwatkot/py3xui/test_coverage)

</div>

## Overview
This SDK is designed to interact with the [3x-ui](https://github.com/MHSanaei/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.<br>
Used dependencies:
- `requests` for synchronous API
- `httpx` for asynchronous API
- `pydantic` for models

Supported Python versions:
- 3.11
- 3.12

Since the 3x-ui app is under development, the SDK may not be compatible with all versions of the app. The developer of SDK is not related to the 3x-ui app, therefore the latest versions of the software are not guaranteed to be compatible with the SDK. <br>
The SDK does not support versions of the 3x-ui older than `2.3.7`.

## 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.<br>
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.<br>
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
```bash
pip install py3xui
```

### Create a new instance of the SDK
It's recommended to use an environment variable to store the API credentials:
```python
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:
```python
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:
```python
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:<br>
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/`.<br>
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.<br>
Same as for other credentials, you can use an environment variable to store the token:
```python
...
os.environ["XUI_TOKEN"] = "your-token"

api = Api.from_env()
```

Or pass it directly, when creating an instance:
```python
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.
```python
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:
```python
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.
```python
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:
- [Synchronous API](py3xui/api/README.md)
- [Asynchronous API](py3xui/async_api/README.md)
- [Client](py3xui/client/README.md)
- [Inbound](py3xui/inbound/README.md)

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.<br>

### Get inbounds list
```python
from py3xui import Api, Inbound

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

### Add a new inbound
```python
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
```python
from py3xui import Api, Client

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

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

### Add a new client
```python
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.<br>
You're also welcome to contribute to the project by opening a pull request.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "py3xui",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "vpn, 3x-ui, sdk, api",
    "author": null,
    "author_email": "iwatkot <iwatkot@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/11/01/6e4ec31cf5ef7ff0f08c2b7f0572197a16f3a9c2b3c1e7ec33260185e16c/py3xui-0.3.1.tar.gz",
    "platform": null,
    "description": "<div align=\"center\" markdown>\n<img src=\"https://github.com/iwatkot/py3xui/assets/118521851/42c5d579-6202-4a9e-88f3-2d844fdd95b6\">\n\nSync and Async Object-oriented Python SDK for the 3x-ui API.\n\n<p align=\"center\">\n    <a href=\"#Overview\">Overview</a> \u2022\n    <a href=\"#Quick-Start\">Quick Start</a> \u2022\n    <a href=\"#Examples\">Examples</a> \u2022\n    <a href=\"#Bugs-and-Feature-Requests\">Bugs and Feature Requests</a> \u2022\n    <a href=\"https://pypi.org/project/py3xui/\">PyPI</a>\n</p>\n\n[![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/iwatkot/py3xui)](https://github.com/iwatkot/py3xui/releases)\n[![GitHub issues](https://img.shields.io/github/issues/iwatkot/py3xui)](https://github.com/iwatkot/py3xui/issues)\n[![Build Status](https://github.com/iwatkot/py3xui/actions/workflows/checks.yml/badge.svg)](https://github.com/iwatkot/py3xui/actions)\n[![Checked with mypy](https://www.mypy-lang.org/static/mypy_badge.svg)](https://mypy-lang.org/)\n[![PyPI - Downloads](https://img.shields.io/pypi/dm/py3xui)](https://pypi.org/project/py3xui/)<br>\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/py3xui)](https://pypi.org/project/py3xui/)\n[![PyPI - Version](https://img.shields.io/pypi/v/py3xui)](https://pypi.org/project/py3xui/)\n[![Maintainability](https://api.codeclimate.com/v1/badges/c03ca2bca0191cb4a2ae/maintainability)](https://codeclimate.com/github/iwatkot/py3xui/maintainability)\n[![Test Coverage](https://api.codeclimate.com/v1/badges/c03ca2bca0191cb4a2ae/test_coverage)](https://codeclimate.com/github/iwatkot/py3xui/test_coverage)\n\n</div>\n\n## Overview\nThis SDK is designed to interact with the [3x-ui](https://github.com/MHSanaei/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.<br>\nUsed dependencies:\n- `requests` for synchronous API\n- `httpx` for asynchronous API\n- `pydantic` for models\n\nSupported Python versions:\n- 3.11\n- 3.12\n\nSince the 3x-ui app is under development, the SDK may not be compatible with all versions of the app. The developer of SDK is not related to the 3x-ui app, therefore the latest versions of the software are not guaranteed to be compatible with the SDK. <br>\nThe SDK does not support versions of the 3x-ui older than `2.3.7`.\n\n## Quick Start\nYou 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.<br>\nAfter 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.<br>\nOn 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.\n\n### Installation\n```bash\npip install py3xui\n```\n\n### Create a new instance of the SDK\nIt's recommended to use an environment variable to store the API credentials:\n```python\nimport os\n\nos.environ[\"XUI_HOST\"] = \"http://your-3x-ui-host.com:2053\"\nos.environ[\"XUI_USERNAME\"] = \"your-username\"\nos.environ[\"XUI_PASSWORD\"] = \"your-password\"\n```\n\nTo work synchronously:\n```python\nfrom py3xui import Api\n\n# Using environment variables:\napi = Api.from_env()\n\n# Or using the credentials directly:\napi = Api(\"http://your-3x-ui-host.com:2053\", \"your-username\", \"your-password\")\n```\n\nTo work asynchronously:\n```python\nfrom py3xui import AsyncApi\n\n# Using environment variables:\napi = AsyncApi.from_env()\n\n# Or using the credentials directly:\napi = AsyncApi(\"http://your-3x-ui-host.com:2053\", \"your-username\", \"your-password\")\n```\n\n*\ufe0f\u20e3 If you're using a custom URI Path, ensure that you've added it to the host, for example:<br>\nIf 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/`.<br>\nOtherwise, all API requests will fail with a `404` error.\n\n*\ufe0f\u20e3 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.<br>\nSame as for other credentials, you can use an environment variable to store the token:\n```python\n...\nos.environ[\"XUI_TOKEN\"] = \"your-token\"\n\napi = Api.from_env()\n```\n\nOr pass it directly, when creating an instance:\n```python\napi = Api(\"http://your-3x-ui-host.com:2053\", \"your-username\", \"your-password\", \"your-token\")\n```\n\n### Using TLS and custom certificates\nInteracting 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.\n\n#### Case 1: Disabling TLS verification\nFor 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.\n```python\napi = Api(\"http://your-3x-ui-host.com:2053\", \"your-username\", \"your-password\", use_tls_verify=False)\n```\n\u2757 Warning: Never disable TLS verification in production.\n\n#### Case 2: Using \u0441ustom \u0441ertificates\nIf 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:\n```python\napi = Api(\n    \"http://your-3x-ui-host.com:2053\",\n    \"your-username\",\n    \"your-password\",\n    custom_certificate_path=\"/path/to/your/certificate.pem\",\n)\n```\nThis allows you to maintain TLS verification by providing a trusted certificate explicitly.\n\n### Login\nNo 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.\n```python\nfrom py3xui import Api, AsyncApi\n\napi = Api.from_env()\napi.login()\n\nasync_api = AsyncApi.from_env()\nawait async_api.login()\n```\n\n## Examples\nYou'll find detailed docs with usage examples for both APIs and for used models in the corresponding package directories:\n- [Synchronous API](py3xui/api/README.md)\n- [Asynchronous API](py3xui/async_api/README.md)\n- [Client](py3xui/client/README.md)\n- [Inbound](py3xui/inbound/README.md)\n\nIn 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.<br>\n\n### Get inbounds list\n```python\nfrom py3xui import Api, Inbound\n\napi = Api.from_env()\napi.login()\ninbounds: List[Inbound] = api.inbound.get_list()\n```\n\n### Add a new inbound\n```python\nfrom py3xui import Api\nfrom py3xui.inbound import Inbound, Settings, Sniffing, StreamSettings\n\napi = Api.from_env()\napi.login()\n\nsettings = Settings()\nsniffing = Sniffing(enabled=True)\n\ntcp_settings = {\n    \"acceptProxyProtocol\": False,\n    \"header\": {\"type\": \"none\"},\n}\nstream_settings = StreamSettings(security=\"reality\", network=\"tcp\", tcp_settings=tcp_settings)\n\ninbound = Inbound(\n    enable=True,\n    port=443,\n    protocol=\"vless\",\n    settings=settings,\n    stream_settings=stream_settings,\n    sniffing=sniffing,\n    remark=\"test3\",\n)\n\napi.inbound.add(inbound)\n```\n\n### Get a client by email\n```python\nfrom py3xui import Api, Client\n\napi = Api.from_env()\napi.login()\n\nclient: Client = api.client.get_by_email(\"some-email\")\n```\n\n### Add a new client\n```python\nfrom py3xui import Api, Client\n\napi = Api.from_env()\napi.login()\n\nnew_client = Client(id=str(uuid.uuid4()), email=\"test\", enable=True)\ninbound_id = 1\n\napi.client.add(inbound_id, [new_client])\n```\n\n## Bugs and Feature Requests\nIf you find a bug or have a feature request, please open an issue on the GitHub repository.<br>\nYou're also welcome to contribute to the project by opening a pull request.\n",
    "bugtrack_url": null,
    "license": "MIT License",
    "summary": "Sync and Async Object-oriented Python SDK for the 3x-ui app.",
    "version": "0.3.1",
    "project_urls": {
        "Homepage": "https://github.com/iwatkot/py3xui",
        "Repository": "https://github.com/iwatkot/py3xui"
    },
    "split_keywords": [
        "vpn",
        " 3x-ui",
        " sdk",
        " api"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4ec25f69150ddd7473f00750ce8ebd6ca1c40df4054eb6f4adad19c763e408c4",
                "md5": "4e4e1b4db9bd4fa922ef92b124b2fe7a",
                "sha256": "fa093c851322a488f9710198a94303386c760039e32d3f503fca07ce7418cddd"
            },
            "downloads": -1,
            "filename": "py3xui-0.3.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "4e4e1b4db9bd4fa922ef92b124b2fe7a",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 35696,
            "upload_time": "2024-11-19T16:30:26",
            "upload_time_iso_8601": "2024-11-19T16:30:26.000707Z",
            "url": "https://files.pythonhosted.org/packages/4e/c2/5f69150ddd7473f00750ce8ebd6ca1c40df4054eb6f4adad19c763e408c4/py3xui-0.3.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "11016e4ec31cf5ef7ff0f08c2b7f0572197a16f3a9c2b3c1e7ec33260185e16c",
                "md5": "75844f39fd89888672c418bf33737db7",
                "sha256": "d98b8018271bdc217aed3f77228bb1330c43b4c30ec90b5d3b260f76f5a3ced1"
            },
            "downloads": -1,
            "filename": "py3xui-0.3.1.tar.gz",
            "has_sig": false,
            "md5_digest": "75844f39fd89888672c418bf33737db7",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 29299,
            "upload_time": "2024-11-19T16:30:27",
            "upload_time_iso_8601": "2024-11-19T16:30:27.678205Z",
            "url": "https://files.pythonhosted.org/packages/11/01/6e4ec31cf5ef7ff0f08c2b7f0572197a16f3a9c2b3c1e7ec33260185e16c/py3xui-0.3.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-19 16:30:27",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "iwatkot",
    "github_project": "py3xui",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "py3xui"
}
        
Elapsed time: 0.65556s