Skip to content

Connect the daemon

agent-wechat runs a native WeChat daemon (wx serve). puppet-bridge talks to its public HTTP API v1 and the event stream — it does not embed QR login.

What you need

  1. wx serve listening (default http://127.0.0.1:6174)
  2. An account already connected / logged in on the daemon
  3. A bearer token with scopes read + send (+ media for attachments; + admin for group edits and friendship mutations)
Terminal window
wx serve
wx tokens create puppet-bridge \
--accounts <ACCOUNT_ID> \
--scopes read --scopes send --scopes media

Environment

Variable Required Default
AGENT_WECHAT_URL no http://127.0.0.1:6174
AGENT_WECHAT_TOKEN yes —
AGENT_WECHAT_ACCOUNT yes daemon account id

How to get these values

Start from a machine where the agent-wechat CLI (wx) can talk to your daemon.

1. AGENT_WECHAT_URL

Default local listener:

http://127.0.0.1:6174

Confirm the daemon is up:

Terminal window
wx status --json
# or
curl -sS http://127.0.0.1:6174/v1/status

If you changed the listen address (or use HTTPS / a reverse proxy / tunnel), put that origin in AGENT_WECHAT_URL — without the /v1 suffix. Example: https://wechat.example.com.

2. AGENT_WECHAT_ACCOUNT

List accounts on the daemon and copy the id of an online account:

Terminal window
wx accounts list --json

Example shape:

{
"items": [
{
"id": "account-14675489135295484865",
"alias": "account-1",
"connection": "online",
"ownUsername": "wxid_..."
}
]
}

Use that id as AGENT_WECHAT_ACCOUNT. Prefer a row with connection: "online" (or authenticated: true). If none exist yet, create and log in first (wx accounts create, wx auth login) — see the agent-wechat setup docs.

3. AGENT_WECHAT_TOKEN

Option A — scoped token (recommended for puppet-bridge)

Create a token limited to this account and the scopes you need. The secret is printed once in the token field:

Terminal window
wx tokens create puppet-bridge \
--accounts <ACCOUNT_ID> \
--scopes read \
--scopes send \
--scopes media \
--json

Add --scopes admin if you need group edits or friendship add/accept. Add --scopes read-state if you will mark chats read.

Option B — owner token on the same machine

On first start the daemon writes an owner token to <dataDir>/api-token. Find dataDir with wx status --json (common defaults: ~/.local/share/agent-wechat, or a desktop Application Support hub).

Terminal window
# replace with dataDir from wx status --json if different
export AGENT_WECHAT_TOKEN="$(cat ~/.local/share/agent-wechat/api-token)"

Prefer Option A for bots: narrower scopes, easier to revoke (wx tokens revoke).

Put it together

Terminal window
export AGENT_WECHAT_URL=http://127.0.0.1:6174
export AGENT_WECHAT_ACCOUNT=<id from wx accounts list>
export AGENT_WECHAT_TOKEN=<token from wx tokens create>
# optional: write a local .env for the sample bots
cat > .env <<EOF
AGENT_WECHAT_URL=$AGENT_WECHAT_URL
AGENT_WECHAT_ACCOUNT=$AGENT_WECHAT_ACCOUNT
AGENT_WECHAT_TOKEN=$AGENT_WECHAT_TOKEN
EOF

Good to know

API tokens and WeChat logins are separate. Revoking a token does not log out of WeChat, and logging out does not revoke tokens. Keep tokens out of git and URLs.

Run the sample

Terminal window
export AGENT_WECHAT_URL=http://127.0.0.1:6174
export AGENT_WECHAT_TOKEN=<token>
export AGENT_WECHAT_ACCOUNT=<ACCOUNT_ID>
npm run start:ripe:agent-wechat:info

Or construct the puppet yourself:

import { WechatyBuilder } from 'wechaty'
import { PuppetAgentWechat } from 'wechaty-puppet-bridge'
const puppet = new PuppetAgentWechat({
baseUrl: process.env.AGENT_WECHAT_URL || 'http://127.0.0.1:6174',
token: process.env.AGENT_WECHAT_TOKEN!,
account: process.env.AGENT_WECHAT_ACCOUNT!,
})
const bot = WechatyBuilder.build({ puppet })
await bot.start()

How inbound messages work

  1. Checks daemon status (state is ready while serving)
  2. Confirms the account connection is online (calls POST /connect if needed)
  3. Opens GET /v1/accounts/{account}/events/stream?newest=true
  4. On message.committed, loads the message over REST and emits Wechaty message
  5. Acknowledges the cursor with POST /consumers/{name}/ack

Supported vs not

Supported: text (with group @), image / file / video / voice send and download, URL cards, recall, contact and room lists, room create / add / del / quit / rename, contact remark and avatar, room announcement get/clear, friendship search / add / accept, conversation read-mark.

Not supported here: QR login inside puppet-bridge, mini-program / location / contact-card send, setting a non-empty room announcement, tags, changing self name / signature.

Bridge-only helpers (mute, pin, room my-name) exist on the Bridge but have no matching Wechaty Puppet API.

Options

See Options for the full constructor fields.