Skip to main content

x-api-rs

X/Twitter Web 私有 API 的 Rust 核心库、Python 绑定和 CLI 工具。

当前架构

本仓库已经拆分为 Rust workspace:

src/core  # x-api-core:真实业务实现
src/py    # x-api-py:Python 绑定
src/cli   # x-api-cli:命令行工具,二进制名 x-api

核心协议边界统一放在 x_api_core::web。XChat 是 Web 协议下的复杂子域,位于 web::xchat。

Rust 用法

use x_api_core::web::{WebClient, WebProfile};

#[tokio::main]
async fn main() -> x_api_core::Result<()> {
    let cookies = "ct0=xxx; auth_token=yyy; twid=u%3D123456789";
    let client = WebClient::builder()
        .cookies(cookies)
        .profile(WebProfile::default_latest())
        .build()
        .await?;

    let result = client.dm().send_message("123456789", "hello", None).await?;
    println!("{result:?}");
    Ok(())
}

Python 用法

from x_api_rs.web import Client, WebProfile

client = await Client.create(
    cookies,
    proxy_url=None,
    profile=WebProfile.default_latest(),
)

# 只有 auth_token、还没有完整 cookies 时,先换取 cookies 再创建客户端:
res = await Client.auth_token_to_cookies(auth_token, proxy_url=None)
client = await Client.create(res.cookies)  # res.cookies 含 auth_token + ct0 + twid

Web 环境快照

WebEnvironmentSnapshot 可把 cookies、CSRF、auth token、user id、代理、WebProfile、UA/请求头策略、ClientTransaction 材料和可选 XChat keystore 材料导出为 JSON,适合存入数据库后下次离线还原客户端。

快照是明文敏感数据,包含可直接恢复账号会话的 cookies/token,启用 XChat 时还可能包含私钥材料。生产环境应由调用方使用数据库加密、KMS 或应用层加密后存储。

let snapshot_json = client.export_environment()?.to_json()?;
let restored = WebClient::restore_environment(WebEnvironmentSnapshot::from_json(&snapshot_json)?)?;
snapshot_json = client.export_environment_json()
restored = Client.restore_environment_json(snapshot_json)

CLI 用法

x-api --cookies-file cookies.txt dm send --user-id 123 --text "hello"
x-api --cookies-file cookies.txt environment export --allow-plaintext-secrets > state.jsonl
x-api environment restore-check --state-file state.json
x-api auth token-to-cookies --token <auth_token> --allow-plaintext-secrets
x-api version-full
x-api output-schema

CLI 输出保持 JSONL envelope,适合 agent、脚本和管道消费。

验证命令

cargo check --workspace --all-features
cargo test -p x-api-core --all-features --no-run
cargo test -p x-api-cli --all-features

Python wheel 构建需要安装 maturin:

python3 -m maturin build --manifest-path src/py/Cargo.toml --features "dm upload inbox user posts communities settings search xchat"

Metadata

Release files for x-api-rs 3.0.14

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

Source distribution (sdist)

Source distribution for x-api-rs 3.0.14
File Size Uploaded Provenance Actions
x_api_rs-3.0.14.tar.gz 283.7 kB Without provenance Details for x_api_rs-3.0.14.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for x-api-rs 3.0.14
File Interpreter ABI Platform Actions
x_api_rs-3.0.14-cp38-abi3-manylinux_2_28_x86_64.whl CPython 3.8 abi3 Linux glibc 2.28+ x86-64 Details for x_api_rs-3.0.14-cp38-abi3-manylinux_2_28_x86_64.whl
x_api_rs-3.0.14-cp38-abi3-manylinux_2_28_aarch64.whl CPython 3.8 abi3 Linux glibc 2.28+ ARM64 Details for x_api_rs-3.0.14-cp38-abi3-manylinux_2_28_aarch64.whl

Total release size: 10.9 MB

Release files / x_api_rs-3.0.14.tar.gz

Download URL x_api_rs-3.0.14.tar.gz
Size 283.7 kB
Tags Source
SHA-256 checksum
How to use checksums
0179b274ce48e113d1419f2ee93aa81cbdeebf877cf79fec7d576037c439ae67
BLAKE2b-256 checksum
How to use checksums
9e27e46872f5a83555a97ef33379b8b7c886578f8f56738e329ea61222370e16
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

Release files / x_api_rs-3.0.14-cp38-abi3-manylinux_2_28_x86_64.whl

Download URL x_api_rs-3.0.14-cp38-abi3-manylinux_2_28_x86_64.whl
Size 5.5 MB
Tags CPython 3.8 Linux glibc 2.28+ x86-64 abi3
SHA-256 checksum
How to use checksums
118dba17707f89e83c5958b0609162d09d20f6330b672e347e94de05a0e5ab73
BLAKE2b-256 checksum
How to use checksums
427988b18a42b930be6e72c0f0d4a42c47f3c7b352cc5c25be754a80339e17bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

Release files / x_api_rs-3.0.14-cp38-abi3-manylinux_2_28_aarch64.whl

Download URL x_api_rs-3.0.14-cp38-abi3-manylinux_2_28_aarch64.whl
Size 5.2 MB
Tags CPython 3.8 Linux glibc 2.28+ ARM64 abi3
SHA-256 checksum
How to use checksums
19896250ef5638d0ef5f7d71b738eaa01d8135b3f6d9050e8e51d329b202417e
BLAKE2b-256 checksum
How to use checksums
37be29efeaff011a7f8e72cc484189fb97698024046fa8ee616d1af7ba248258
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

Security for x-api-rs 3.0.14

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.

Release history Release notifications | RSS feed

This release

3.0.14 This release

3 release files No provenance

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