---
title: "n8n"
description: "通过 TwexAPI 构建 n8n Twitter 工作流，实现推文搜索、用户查询、热门推文、AI Agent 与游标安全 handoff。"
---

用 TwexAPI 构建 n8n Twitter 工作流。搜索推文、查询用户、读取热门推文并运行 AI Agent。本集成使用 TwexAPI 社区节点、HTTP Request、Webhook 与 MCP Client Tool 节点。

TwexAPI 使用单一 API 密钥与 REST 基址 `https://api.twexapi.io`。

## 选择 n8n Twitter 集成方式

<CardGroup cols={2}>
  <Card title="TwexAPI 社区专属节点" icon="plug">
    安装 `@twexapi-dev/n8n-nodes-x-api-scraper`，支持推文搜索、用户搜索、资料查询、热门推文与余额检查。
  </Card>

  <Card title="HTTP Request + MCP" icon="workflow">
    任意已文档化 REST 路由用 HTTP Request。AI Agent需先发现端点时用 MCP Client Tool。
  </Card>
</CardGroup>

## 前置条件

* [TwexAPI API 密钥](https://twexapi.io/dashboard)
* n8n Cloud 或自托管 n8n
* 以下配方可选 Slack 与 Google Sheets 凭证

## 安装社区节点

在 **Settings → Community Nodes** 安装 `@twexapi-dev/n8n-nodes-x-api-scraper`。

```bash
npm install @twexapi-dev/n8n-nodes-x-api-scraper
```

源码：[twexapi-dev/n8n-nodes-x-api-scraper](https://github.com/twexapi-dev/n8n-nodes-x-api-scraper)

### 支持的操作

| 任务 | n8n 操作 | TwexAPI 路由 |
| --- | --- | --- |
| 搜索推文 | Tweet: search | `POST /twitter/advanced_search/page` |
| 搜索用户 | User: search | `GET /twitter/search-user/{keyword}/{target_count}` |
| 查询资料 | User: get | `GET /twitter/{screen_name}/about` |
| 读取热门推文 | Trend: get | `GET /twitter/global-trending/tweets` |
| 查看余额 | Account: get balance | `GET /balance` |

粉丝分页、列表、社区与经批准发帖请用 [REST API](/api-reference/overview)。TwexAPI 不提供原生账号监控或事件 webhook —  recurring 作业请用定时 REST 或无代码平台 webhook。

## API 密钥凭证

为 TwexAPI 创建可复用 n8n 凭证：

<CardGroup cols={3}>
  <Card title="身份认证与鉴权" icon="key-round">
    TwexAPI API 凭证或 HTTP Request 凭证选择 Header Auth。
  </Card>

  <Card title="请求头名称" icon="braces">
    Header 名称设为 `Authorization`。
  </Card>

  <Card title="请求头数值" icon="shield-check">
    凭证值粘贴 `Bearer YOUR_API_KEY`。
  </Card>
</CardGroup>

每个调用 `https://api.twexapi.io` 的 HTTP Request 节点使用该凭证。

具备 MCP 的 AI Agent 节点：MCP Client Tool 配置 `https://api.twexapi.io/mcp`，header `x-api-key: YOUR_API_KEY`。

## 结果 handoff

下一节点仅需稳定 handoff 字段时，在每个 HTTP Request 或 TwexAPI 节点后使用 Edit Fields。原始响应勿进入 Slack 消息、Sheets 行与重试队列。

<CardGroup cols={2}>
  <Card title="推文搜索分页" icon="search">
    存储请求查询；将每条推文 `id`、`text`、作者用户名、`created_at` 映射为 `tweet_id`、`text`、`author_username`、`created_at`；保留 `has_next_page` 与 `next_cursor` 供页循环。
  </Card>

  <Card title="用户画像记录" icon="users">
    源 `id` 存为 `user_id`，外加 `username`、`name`、粉丝数与查询或搜索输入。
  </Card>

  <Card title="趋势话题榜单" icon="trending-up">
    存储各趋势名、主题、内容标签与国家。请求区域与工作流运行一并保留。
  </Card>

  <Card title="发推或回复写入" icon="send">
    写操作需已保存 cookie 或 `auth_token`。从 n8n 发帖前用 [CLI](/sdks/cli) `--dry-run` 预览。
  </Card>
</CardGroup>

## 配方 1：MCP AI Agent X 研究

用此配方将 AI Agent 与 TwexAPI 集成。AI Agent可搜索实时推文、汇总趋势、查看资料并比较粉丝活动。

1. **添加 AI Agent 节点**

    用首选聊天模型创建 AI Agent 工作流。

2. **添加 MCP 客户端工具**

    Server Transport 设为 `HTTP Streamable`。MCP Endpoint URL 设为 `https://api.twexapi.io/mcp`。Authentication 设为 Header Auth，名称 `x-api-key`，值为 TwexAPI API 密钥。

3. **配置 Agent 业务指令**

    指导 Agent 先调用 `explore`，再 `twexapi_request`，且仅返回 JSON。

建议Agent 系统 prompt：

```text
Use TwexAPI for every X research request. Call explore before twexapi_request. Search tweets, inspect accounts, and cite the returned post URLs. Prefer small, targeted searches before broad exports.
```

示例用户 prompt：

```text
Find recent posts about AI agents in fintech, group them by theme, and list 5 accounts worth monitoring.
Return only JSON with route_used, tweets, has_more, and next_cursor.
```

MCP 服务器暴露 `explore` 用于端点发现、`twexapi_request` 用于认证调用。

## 配方 2：推文搜索到 Slack

用 TwexAPI 社区节点或 HTTP Request 搜索推文，再告警 Slack 频道。

### 工作流形状

<CardGroup cols={2}>
  <Card title="定时调度或手动触发器" icon="calendar-clock">
    定时运行或测试时手动触发。
  </Card>

  <Card title="搜索推文" icon="search">
    用 TwexAPI 节点或 HTTP Request 调用 `POST /twitter/advanced_search/page`。
  </Card>

  <Card title="拆分数据行" icon="split">
    对返回推文 Split Out。
  </Card>

  <Card title="发送通知告警" icon="message-square">
    向 Slack 发送格式化文本。
  </Card>
</CardGroup>

### HTTP Request 示例

```json
{
  "method": "POST",
  "url": "https://api.twexapi.io/twitter/advanced_search/page",
  "headers": {
    "Authorization": "Bearer YOUR_API_KEY",
    "Content-Type": "application/json"
  },
  "body": {
    "searchTerms": ["AI agents lang:en"],
    "sortBy": "Latest",
    "nextCursor": ""
  }
}
```

### Slack 消息

Slack 前使用 Set 节点：

```text
@{{$json.author_username || $json.user?.screen_name}}
{{$json.text || $json.full_text}}
https://x.com/i/web/status/{{$json.tweet_id || $json.id}}
```

## 配方 3：搜索结果到 Google Sheets

团队需在电子表格中查看推文搜索结果时使用。

### 工作流形状

<CardGroup cols={2}>
  <Card title="定时调度触发器" icon="calendar-clock">
    每日或每小时导出；测试时手动执行。
  </Card>

  <Card title="搜索推文" icon="search">
    用 TwexAPI 节点或 HTTP Request 搜索并存储 `next_cursor`。
  </Card>

  <Card title="拆分数据行" icon="split">
    对返回推文 Split Out。
  </Card>

  <Card title="追加数据行" icon="file-spreadsheet">
    Google Sheets Append Row 映射下方结果字段。
  </Card>
</CardGroup>

映射到 Google Sheets 的字段：

<CardGroup cols={2}>
  <Card title="推文 ID" icon="hash">
    映射到 `id` 或 `tweet_id`。
  </Card>

  <Card title="作者信息" icon="at-sign">
    映射到作者用户名或 screen name。
  </Card>

  <Card title="文本内容" icon="type">
    映射到 `text` 或 `full_text`。
  </Card>

  <Card title="创建时间" icon="calendar">
    映射到 `created_at` 或 `createdAt`。
  </Card>

  <Card title="URL" icon="link">
    由 `https://x.com/i/web/status/{id}` 构建。
  </Card>
</CardGroup>

较大作业在 `has_next_page` 或 `has_more` 为 true 时循环，将 `next_cursor` 传回搜索 body。

## 常用 REST 操作

<CardGroup cols={2}>
  <Card title="搜索推文" icon="search">
    用 `POST /twitter/advanced_search/page` 运行 X 查询搜索。
  </Card>

  <Card title="获取用户资料" icon="user-round">
    用 `GET /twitter/{screen_name}/about` 获取 X 资料。
  </Card>

  <Card title="获取粉丝列表" icon="users">
    用 `POST /v3/twitter/users/followers` 读取粉丝列表。
  </Card>

  <Card title="获取热门趋势" icon="trending-up">
    用 `GET /twitter/global-trending/tweets` 读取全球热门推文。
  </Card>

  <Card title="发布推文" icon="send">
    用 `POST /twitter/tweets/create` 发布文本或媒体帖。
  </Card>

  <Card title="发送私信" icon="message-circle">
    用 `POST /v3/twitter/send-dm` 发送 DM。
  </Card>
</CardGroup>

## 测试清单

* 在每个 HTTP Request 或 TwexAPI 节点上使用 n8n Test Step。
* 确认每个 REST 请求含 `Authorization: Bearer YOUR_API_KEY`。
* 确认每个 MCP Client Tool 请求含 `x-api-key`。
* 分页作业每页后存储 `next_cursor`。
* `429` 时等待后重试并保留游标。

## n8n Twitter 自动化常见问题

### 如何通过 TwexAPI 将 n8n 连接到 Twitter？

安装 TwexAPI 社区节点，或创建 Header Auth `Authorization: Bearer YOUR_API_KEY`，在每个 TwexAPI 请求上选择该凭证。

### n8n Twitter 搜索如何工作？

通过社区节点或 HTTP Request 调用 `POST /twitter/advanced_search/page`。每页保存查询与游标。

### n8n 能否发帖到 Twitter？

可以，通过 `POST /twitter/tweets/create`。写操作需 cookie 或 `auth_token`。先用 [CLI](/sdks/cli) 预览载荷。

### 能否构建 n8n Twitter AI Agent？

可以。n8n Twitter AI Agent可通过 `https://api.twexapi.io/mcp` 的 MCP 搜索推文、资料与趋势。

### 自托管 n8n 能否使用 TwexAPI？

可以。允许出站 HTTPS 到 `https://api.twexapi.io`。

## 下一步

* 阅读 [Agent MCP Handoff](/mcp/agent-handoff) 了解严格 JSON handoff 模式。
* 阅读 [CLI](/sdks/cli) 预览写操作 dry-run。
* 构建 AI Agent 工作流时使用 [MCP Tools](/mcp/tools)。
