Skip to main content

sub2api_utils

A user-side Python programming interface for Sub2API (the open-source project Wei-Shaw/sub2api). Pure standard library, zero third-party dependencies (Python 3.10+).

No CLI, no GUI — just an importable object-oriented interface.

HTTP connection reuse (0.1.1)

Requests reuse HTTP/1.1 TCP/TLS connections on a best-effort basis, including requests from different login sessions to the same origin. Tokens and request headers remain per request; sharing a connection does not share authentication. No third-party dependencies are required.

  • Each thread has its own pool with at most 8 idle connections and a 30-second idle expiry. Different origins, proxies, proxy credentials, and TLS verification settings use separate connections.
  • Responses are fully read before a connection returns to the pool. Server Connection: close, incomplete responses, and failed connections are not reused.
  • If a cached connection was closed by the peer, GET/HEAD may reconnect once. POST requests are not automatically replayed by the transport.
  • Environment and Windows system proxies are detected as before. Pass proxy="" to a session or user set to force a direct connection, or pass an HTTP proxy URL explicitly. An installed global urllib opener is not used; configure proxies through this parameter instead.
  • close_connections() closes the current thread's cached connections. Remaining pools are also cleaned up on normal process exit.
from sub2api_utils import Sub2ApiUserInfoSet, close_connections

users = Sub2ApiUserInfoSet(base_url=BASE_URL, timeout=30, proxy="")
try:
    users.add_user_info("you@example.com", "your-password")
    keys = users.get_api_key_list()
finally:
    close_connections()

Project layout

sub2api_utils/
├── sub2api_utils/
│   ├── __init__.py     # public interface exports
│   └── main.py         # implementation (standard library only)
├── pyproject.toml      # packaging metadata
└── README.md

Quick start

from datetime import datetime
from sub2api_utils import Sub2ApiLoginSession, DayPeriod, SimpleStat

BASE_URL = "https://your-sub-to-api-website"

# 1) Log in (the password is validated immediately; raises Sub2APIError if wrong)
session = Sub2ApiLoginSession(BASE_URL, "you@example.com", "your-password")

# 2) List all API keys of this user
keys = session.get_api_key_list()
for k in keys:
    print(k.get_str_form())          # sk-xxx
    print(k.get_user_email())        # the email the key belongs to
    print(k.get_key_name())          # the key's name on the site, e.g. 'Main Key'

# 3) Actual usage (in $) of a key over an arbitrary time range, precise to the hour
key = keys[0]
usd = key.get_stat("your-password",
                   datetime(2026, 9, 25, 14, 0),   # start (inclusive)
                   datetime(2026, 9, 28, 9, 0))     # end (exclusive), [start, end)
print(usd)

# 4) Per-day usage for every day in a date period (reuses the same login session)
daily = key.get_stat_in_period("your-password", DayPeriod(2026, 9, 25, 3))
print(daily)                          # [day1, day2, day3]

# 5) A manually constructed key works too (it logs in lazily on first use)
from sub2api_utils import Sub2ApiKey
manual = Sub2ApiKey(BASE_URL, "you@example.com", "sk-xxx")
print(manual.get_stat("your-password", datetime(2026, 9, 25), datetime(2026, 9, 26)))

# 6) Managing multiple users + a usage matrix
from sub2api_utils import Sub2ApiUserInfoSet
us = Sub2ApiUserInfoSet(base_url=BASE_URL)        # base_url is required
us.add_user_info("a@example.com", "pwd-a")        # password is validated on add
us.add_user_info("b@example.com", "pwd-b")
matrix = us.get_stat_matrix(DayPeriod(2026, 9, 25, 2))
# matrix = { "a@example.com": {"sk-1": [d1, d2], ...}, ... }
us.del_user_info("a@example.com")

# 7) Simple statistics
stat = SimpleStat(matrix["b@example.com"]["sk-2"])
print(stat.average(), stat.variation(), stat.stdvar())

Classes and methods

Class Method Description
Sub2ApiLoginSession __init__(base_url, user_email, password, *, timeout=30, verify_ssl=True, timezone="Asia/Shanghai") Logs in and validates the password; raises Sub2APIError on failure
get_api_key_list() -> list[Sub2ApiKey] All API keys of the current user
get_api_key_stat(key, begin_time, end_time) -> float Actual usage ($) of the key over [begin, end)
Sub2ApiKey get_str_form() / get_user_email() / get_base_url() Basic info
get_key_name() -> str | None The key's string name on the site (e.g. Main Key)
get_stat(password, begin_time, end_time) -> float Usage (logs in lazily on first call, then reuses the session)
get_stat_in_period(password, days) -> list[float] Per-day usage over the date period
DayPeriod DayPeriod(year, month, day, duration) + four getters Start date + duration in days (positive integer)
Sub2ApiUserInfoSet __init__(*, base_url=...) / add_user_info(email, password) / del_user_info(email) Add (validates password) / remove users
get_api_key_list() -> list[Sub2ApiKey] All keys of all users
get_stat_matrix(days) -> dict[email, dict[key_str, list[float]]] Per-user, per-key, per-day usage matrix
SimpleStat foldl / average / variation / stdvar Left fold / mean / variance / standard deviation
Sub2APIError — Interface error (carries HTTP status, backend code/reason)

Implementation notes (verified against the Wei-Shaw/sub2api source)

The client calls the Sub2API user-side HTTP API:

Purpose Endpoint
Login (username = email) POST /api/v1/auth/login → data.access_token
Key listing (paginated) GET /api/v1/keys, Authorization: Bearer <token>
Hourly / daily usage GET /api/v1/usage/dashboard/trend?granularity=hour|day
Usage records (fallback) GET /api/v1/usage (paginated)
  • Base URL: no site address is hardcoded in the library. Pass base_url explicitly to Sub2ApiLoginSession / Sub2ApiKey / Sub2ApiUserInfoSet.
  • Hour precision: get_stat sums the hourly trend buckets over the exact [begin, end) range, so the boundaries can be precise to the hour (the backend only accepts day-precision range inputs; extra buckets are dropped client-side).
  • Key name: get_key_name() reads the name field of /api/v1/keys. Keys from get_api_key_list() carry the name; a manually constructed key is looked up (and cached) once a login session exists; None is returned when neither is available.
  • Cost basis: the value returned is actual_cost (the amount actually charged, i.e. the real $), not the token count.
  • Fallback: if the backend has no dashboard/trend endpoint, the client automatically aggregates the per-record usage from /api/v1/usage instead; results are equivalent.
  • Timezone: timezone decides where the boundaries of a "day"/"hour" fall (default Asia/Shanghai). When the system lacks tzdata, a built-in fixed-offset table is used, so no tzdata package is required.
  • Session reuse: Sub2ApiKey.get_stat / get_stat_in_period log in on the first call and cache the session for later calls; the session is rebuilt when the password changes. If an access_token expires (HTTP 401), the client re-logs in with the stored password and retries once.
  • 2FA: if the account has TOTP two-factor authentication enabled, the login response has no access_token and Sub2APIError reports it clearly (that flow requires /login/2fa; this library does not automate it).
  • Note: in the class skeleton, get_stat_in_period was originally annotated as returning list[str]; it is implemented as list[float] (per-day dollar amounts, consistent with the inner list[float] of get_stat_matrix).

Metadata

Release files for sub2api-utils 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sub2api-utils 0.1.1
File Size Uploaded Provenance Actions
sub2api_utils-0.1.1.tar.gz 15.7 kB Without provenance Details for sub2api_utils-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sub2api-utils 0.1.1
File Interpreter ABI Platform Actions
sub2api_utils-0.1.1-py3-none-any.whl Python 3 none any Details for sub2api_utils-0.1.1-py3-none-any.whl

Total release size: 30.7 kB

Release files / sub2api_utils-0.1.1.tar.gz

Download URL sub2api_utils-0.1.1.tar.gz
Size 15.7 kB
Tags Source
SHA-256 checksum
How to use checksums
de94ecba41302750594d101c9210b7ff1ff4d88e6a86b6bcbda555782a18ce46
BLAKE2b-256 checksum
How to use checksums
944c18f6fa42e5211f23f590972f4a71326c3b3fc7b186a0342bf07c7a68d4a6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / sub2api_utils-0.1.1-py3-none-any.whl

Download URL sub2api_utils-0.1.1-py3-none-any.whl
Size 15.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
097079499066a63e6d04dc52a6991df0aaa36f6dc3a77926df177db2972a59a1
BLAKE2b-256 checksum
How to use checksums
b0266e8dcd8cfc11ab50bf906369dfc33a230bfa93c0f8a6d608f048e87e82b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for sub2api-utils 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page