如何用 TwexAPI 发布 X 帖子
TwexAPI 提供 POST https://api.twexapi.io/twitter/tweets/create,用于创建 X 帖子和回复。因为这是写入端点,处理方式应该不同于只读搜索 API:先审核内容,安全保存账号凭据,并记录每一次发布尝试。
本文聚焦“已审批内容”的受控发布流程。它不适用于骚扰式回复、低质批量发布或误导性自动化。
端点和必填字段
当你要用账号 cookie 或 auth token 发布内容时,使用 POST /twitter/tweets/create。
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
tweet_content | string | 是 | 要发布的文本。发送前先校验长度和内容规则。 |
cookie | string | 是 | X authentication cookie 或 auth_token。请放在密钥管理器或环境变量里。 |
media_url | string 或 null | 否 | 要附加的媒体 URL。 |
reply_tweet_id | string 或 null | 否 | 要回复的 tweet ID。 |
schedule | string 或 null | 否 | 定时发布的 ISO 时间。 |
community_name | string 或 null | 否 | 社区名称或社区 ID。 |
delegated_account_username | string 或 null | 否 | 使用 delegated account 时的用户名。 |
proxy | string 或 null | 否 | 发布设置使用的网络路由。请保持可管理、合规。 |
TwexAPI 也提供 POST /twitter/post-tweet-without-cookie,由 cookie 池处理发布,而不是在请求体里传 cookie。只有当你的运营模型明确依赖托管账号池时再使用它。大多数自有账号发布流程,用显式 cookie 的端点更容易审计。
基础请求
curl --request POST \
--url https://api.twexapi.io/twitter/tweets/create \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '{
"tweet_content": "Product changelog: the CSV export now includes reply counts and quote counts.",
"cookie": "<x_auth_cookie_or_auth_token>"
}'成功响应会返回 code、msg 和 data。data 对象可包含 tweet_id、user_id 和响应 code。
{
"code": 200,
"msg": "success",
"data": {
"tweet_id": "1234567890123456789",
"user_id": "1234567890",
"code": 200
}
}Python 客户端
不要把凭据写进源码。下面的客户端从环境变量读取 Bearer token 和 X cookie,先校验内容,再返回 API 响应。
1import os
2from typing import Any
3
4import requests
5
6API_URL = "https://api.twexapi.io/twitter/tweets/create"
7BEARER_TOKEN = os.environ["TWEXAPI_BEARER_TOKEN"]
8X_COOKIE = os.environ["TWEXAPI_X_COOKIE"]
9
10def validate_post_text(text: str) -> None:
11 if not text or not text.strip():
12 raise ValueError("tweet_content cannot be empty")
13 if len(text) > 280:
14 raise ValueError(f"tweet_content is too long: {len(text)} characters")
15
16def publish_post(
17 text: str,
18 *,
19 media_url: str | None = None,
20 reply_tweet_id: str | None = None,
21 schedule: str | None = None,
22 community_name: str | None = None,
23) -> dict[str, Any]:
24 validate_post_text(text)
25
26 payload: dict[str, Any] = {
27 "tweet_content": text,
28 "cookie": X_COOKIE,
29 }
30
31 if media_url:
32 payload["media_url"] = media_url
33 if reply_tweet_id:
34 payload["reply_tweet_id"] = reply_tweet_id
35 if schedule:
36 payload["schedule"] = schedule
37 if community_name:
38 payload["community_name"] = community_name
39
40 response = requests.post(
41 API_URL,
42 headers={
43 "Authorization": f"Bearer {BEARER_TOKEN}",
44 "Content-Type": "application/json",
45 },
46 json=payload,
47 timeout=30,
48 )
49 response.raise_for_status()
50 return response.json()
51
52if __name__ == "__main__":
53 result = publish_post(
54 "Product changelog: the CSV export now includes reply counts and quote counts."
55 )
56 print(result["data"]["tweet_id"])回复、媒体、定时和社区发布
同一个端点支持多种发布模式。建议每种模式都显式传参,方便日志里看清为什么发了这条内容。
1# Reply to an existing post.
2reply_result = publish_post(
3 "Thanks for reporting this. We shipped a fix in the latest export job.",
4 reply_tweet_id="1234567890123456789",
5)
6
7# Attach media by URL.
8media_result = publish_post(
9 "Here is the updated reporting layout.",
10 media_url="https://example.com/reporting-layout.png",
11)
12
13# Schedule a future post with an ISO timestamp.
14scheduled_result = publish_post(
15 "Maintenance window starts in one hour.",
16 schedule="2026-07-01T12:00:00Z",
17)
18
19# Publish inside a community by name or ID.
20community_result = publish_post(
21 "Sharing the release notes for community feedback.",
22 community_name="1234567890123456789",
23)发布回复前,先确认目标 tweet 确实是你要回复的那条。定时发布时,也要在自己的系统里保存计划发布时间,而不是只依赖 API 请求。
审批优先的发布流程
更稳的发布系统会在 API 调用前保留草稿状态。至少记录:
draft_idaccount_idtweet_content- 可选的
media_url、reply_tweet_id、schedule和community_name approved_byapproved_at- 发布后的
tweet_id
1def publish_approved_draft(draft: dict[str, Any]) -> dict[str, Any]:
2 if draft.get("status") != "approved":
3 raise ValueError("Draft must be approved before publishing")
4
5 result = publish_post(
6 draft["tweet_content"],
7 media_url=draft.get("media_url"),
8 reply_tweet_id=draft.get("reply_tweet_id"),
9 schedule=draft.get("schedule"),
10 community_name=draft.get("community_name"),
11 )
12
13 return {
14 "draft_id": draft["draft_id"],
15 "tweet_id": result.get("data", {}).get("tweet_id"),
16 "user_id": result.get("data", {}).get("user_id"),
17 "api_code": result.get("code"),
18 "api_msg": result.get("msg"),
19 }这样 API 调用保持简单,内容审批、重试策略和审计记录交给你的应用处理。
常见坑
- 不要把
cookie写死在脚本、日志或仓库里。 - 不要把未经复核的搜索结果直接自动发布。
- 调用端点前先校验文本长度。
- 使用
reply_tweet_id前,确认目标帖子就是你要回复的内容。 - 保存响应里的
tweet_id,避免重试时重复发布。 - 营销活动、客户回复和社区帖都应保留人工审批路径。
小结
POST /twitter/tweets/create 是 TwexAPI 用于发布 X 帖子和回复的直接端点。最好的实现不是复杂的自动化循环,而是可审计的简单流程:草稿、审批、发布、保存响应、复核失败。
这样既能使用程序化发布,又不会变成不可控的自动发帖。