HTTP · JSON · SSE
Alice 接口文档Alice API
Alice 是一个持续存在的叙事角色。这组接口让任何网页或脚本以访客身份和她对话:申请访客码、发送消息、实时接收回复。
Alice is a persistent narrative character. These endpoints let any web page or script talk to her as a visitor: get a visitor code, send messages and receive replies in real time.
https://api.ignat.ai/alice/api- 申请访客码
POST /visitor拿到visitorId和visitorKey。 - Get a code
POST /visitorreturns avisitorIdandvisitorKey. - 订阅回复
GET /events打开事件流,回复从这里推送过来。 - SubscribeOpen the
GET /eventsstream; replies are pushed there. - 发送消息
POST /message立即返回 202,Alice 稍后通过事件流回复。 - Send
POST /messagereturns 202 at once; Alice answers later through the stream.
回复是异步的:Alice 可能马上回、过一会儿回、分几条回,也可能不回。不要在 /message 的响应里等回复。
Replies are asynchronous: Alice may answer right away, later, in several messages, or not at all. Never wait for a reply in the /message response.
通用约定Conventions
- POST 请求体必须是 JSON,请求头
Content-Type: application/json,大小不超过 8 KB。 - 允许任意来源跨域调用(
Access-Control-Allow-Origin: *)。接口不用 cookie,也不需要。 - 所有错误都返回 JSON:
{"error": "..."},部分错误另带code字段,见错误。 - 时间均为 ISO 8601 UTC 格式。
- POST bodies must be JSON with
Content-Type: application/json, at most 8 KB. - Any origin may call the API (
Access-Control-Allow-Origin: *). No cookies are used or needed. - Every error is JSON:
{"error": "..."}, some with an extracodefield; see Errors. - Timestamps are ISO 8601 in UTC.
身份与凭证Identity & credentials
每个访客有一个独立身份,Alice 对不同访客的记忆互相隔离。身份分两种:
Each visitor has its own identity, and Alice keeps memories of different visitors apart. There are two kinds:
| 正式访客 | Visitor | 临时访客(guest) | Guest | ||
|---|---|---|---|---|---|
| 身份号 | ID | v_ + 8 位chars, 如e.g. v_7KQ2MX9P | g_ + 22 位随机字符random chars | ||
| 密码 | Key | 8 位 visitorKey,只在申请时返回一次 | 8-char visitorKey, returned only once | 无,身份号本身就是凭证 | None; the ID itself is the credential |
| 名字 | Name | 申请时必填,可改名 | Required, can be changed | 固定为“访客”+ g_ 后的前 6 位 | Fixed: “访客” + the 6 chars after g_ |
| 有效期 | Lifetime | 长期有效 | Long-lived | 闲置 60 分钟后作废 | Expires after 60 min idle |
v_ 码和密码方便手动输入:不区分大小写,不含 0、O、1、I、L。凭证放置位置:
v_ codes and keys are made to be typed: case-insensitive, without 0, O, 1, I or L. Where the credentials go:
| 接口 | Endpoint | visitorId | visitorKey | |
|---|---|---|---|---|
POST /message, POST /name | 请求体 | body | 请求体 | body |
GET /history | 查询参数 | query | 请求头 X-Visitor-Key | header X-Visitor-Key |
GET /events | 查询参数 | query | 查询参数(EventSource 不能带请求头) | query (EventSource cannot send headers) |
身份号或密码错误统一返回 401,不会提示是哪一个错了。同一身份号每小时最多输错 10 次,之后返回 429。所有访客都只有公开权限,凭证只用来区分访客。
A wrong ID or key always returns the same 401, so codes cannot be probed. Each code allows 10 failed attempts per hour, then 429. Every visitor has public access only; credentials just tell visitors apart.
快速上手Quick start
curl
# 1. Get a visitor code curl -X POST https://api.ignat.ai/alice/api/visitor \ -H 'Content-Type: application/json' \ -d '{"name": "Ada"}' # → {"visitorId":"v_7KQ2MX9P","visitorKey":"H4TW8NRC","name":"Ada","renamesLeft":3} # 2. Listen for replies (keep this running in another terminal) curl -N 'https://api.ignat.ai/alice/api/events?visitorId=v_7KQ2MX9P&visitorKey=H4TW8NRC' # 3. Say hello curl -X POST https://api.ignat.ai/alice/api/message \ -H 'Content-Type: application/json' \ -d '{"visitorId":"v_7KQ2MX9P","visitorKey":"H4TW8NRC","text":"Hi, Alice"}'
JavaScript
const API = 'https://api.ignat.ai/alice/api' const post = (path, body) => fetch(API + path, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), }).then(r => r.json()) const { visitorId, visitorKey } = await post('/visitor', { name: 'Ada' }) // Save both: the key is never shown again. const events = new EventSource( `${API}/events?visitorId=${visitorId}&visitorKey=${visitorKey}`) events.addEventListener('message', e => console.log(JSON.parse(e.data).text)) events.addEventListener('typing', e => console.log('typing:', JSON.parse(e.data).active)) await post('/message', { visitorId, visitorKey, text: 'Hi, Alice', clientId: crypto.randomUUID() })
POST/visitor
申请一个新的访客身份。名字会先经过审查,不合格就不发码。
Creates a new visitor identity. The name is reviewed first; no code is issued if it is refused.
请求体Body
| 字段 | Field | 类型 | Type | 说明 | Description |
|---|---|---|---|---|---|
name | string | 1–24 个字符。申请正式访客时必填;临时访客不能带 | 1–24 characters. Required for a visitor; not allowed for a guest | ||
guest optional | boolean | true 时申请临时访客码,只发 g_ 码不发密码 | true issues a guest code with no key |
响应 201Response 201
{ "visitorId": "v_7KQ2MX9P", "visitorKey": "H4TW8NRC", "name": "Ada", "renamesLeft": 3 }
// guest: true
{ "visitorId": "g_Q3v…", "name": "访客Q3v…", "renamesLeft": 0 }可能的错误Errors
400缺少名字或名字过长;临时访客带了名字时code: "guest_no_name"422name_rejected:名字未通过审查,reason字段说明原因503name_review_unavailable:审查暂时无法进行,稍后再试429visitor_limit:这个 IP 申请的新身份太多(正式与临时合计,名字被拒的尝试也算);visitor_busy:全站这一小时的新身份已满。已有身份不受影响,按Retry-After等待
400name missing or too long;code: "guest_no_name"when a guest sends one422name_rejected: the name failed review;reasonexplains why503name_review_unavailable: review cannot run right now, try later429visitor_limit: too many new identities from this IP (visitors and guests together, refused names included);visitor_busy: the site has issued its new identities for this hour. Existing identities are unaffected; waitRetry-After
从未发过消息的身份闲置 24 小时后会被清理。
Identities that never send a message are removed after 24 hours idle.
POST/message
发一条消息给 Alice。接收成功就返回 202;回复稍后通过 /events 推送,也会写进 /history。
Sends Alice a message. Returns 202 once accepted; replies arrive later on /events and are also stored in /history.
请求体Body
| 字段 | Field | 类型 | Type | 说明 | Description |
|---|---|---|---|---|---|
visitorId | string | 身份号 | Visitor ID | ||
visitorKey | string | 密码;临时访客不需要 | Key; not needed for guests | ||
text | string | 1–2000 个字符 | 1–2000 characters | ||
clientId optional | string | 客户端生成的唯一编号(字母、数字、_、-,最长 64)。网络重试时带同一个值,不会重复发送 | Client-made unique ID (letters, digits, _, -, up to 64). Retrying with the same value never sends twice |
响应 202Response 202
{ "id": "1042", "visitorId": "v_7KQ2MX9P" }可能的错误Errors
400文本为空或超长401缺少或错误的凭证;临时访客码过期时为Guest code expired429发送太快;ip_daily_limit:这个 IP 今天发得太多(换身份也算在一起);或daily_limit:今天的对话额度用完了,reply字段是一句可以直接展示的话,Retry-After是距离重置的秒数503Alice 暂时无法接收,消息不会保存
400empty or overlong text401missing or wrong credentials;Guest code expiredfor an expired guest code429sending too fast;ip_daily_limit: this IP has sent too much today (across all identities); ordaily_limit: today's chat quota is used up.replyholds a line you can show as-is, andRetry-Aftergives seconds until reset503Alice cannot take messages right now; the message is not stored
GET/events
Server-Sent Events 事件流,用浏览器的 EventSource 或 curl -N 打开。断线后客户端会在 5 秒后自动重连。
A Server-Sent Events stream; open it with the browser's EventSource or curl -N. Clients reconnect automatically 5 seconds after a drop.
查询参数Query
visitorId, visitorKey
事件Events
| 事件 | Event | 数据 | Data | 说明 | Description |
|---|---|---|---|---|---|
message | {id, role, text, at} | Alice 的一条消息,role 为 "alice" | One message from Alice; role is "alice" | ||
typing | {active} | 是否正在输入;最长 90 秒后自动变为 false | Whether Alice is typing; turns false after 90 s at most |
retry: 5000
event: typing
data: {"active":true}
event: message
data: {"id":"1043","role":"alice","text":"Hi. Who's this?","at":"2026-10-19T11:02:31.000Z"}
: ping每 25 秒发送一行 : ping 注释保持连接。同一访客最多同时开 3 条流,超出时关闭最早的一条。
A : ping comment every 25 seconds keeps the connection open. Each visitor may hold 3 streams at once; opening another closes the oldest.
GET/history
读取最近 200 条聊天记录(从旧到新)。用来恢复对话,或补上断线期间错过的消息。
Returns the latest 200 messages, oldest first. Use it to restore a chat or catch up on messages missed while offline.
curl 'https://api.ignat.ai/alice/api/history?visitorId=v_7KQ2MX9P' \ -H 'X-Visitor-Key: H4TW8NRC'
{
"visitorId": "v_7KQ2MX9P",
"name": "Ada",
"renamesLeft": 3,
"messages": [
{ "id": "1042", "role": "visitor", "text": "Hi, Alice", "at": "2026-10-19T11:02:20.000Z" },
{ "id": "1043", "role": "alice", "text": "Hi. Who's this?", "at": "2026-10-19T11:02:31.000Z" }
]
}POST/name
修改正式访客的名字。新名字同样要经过审查;每个自然月最多成功修改 3 次。临时访客不能改名(403)。
Changes a visitor's name. The new name is reviewed too; at most 3 successful changes per calendar month. Guests cannot be renamed (403).
// request { "visitorId": "v_7KQ2MX9P", "visitorKey": "H4TW8NRC", "name": "Ada L." } // 200 { "name": "Ada L.", "renamesLeft": 2 }
429rename_limit:本月次数已用完409同一身份的另一次改名还在进行中422/503与/visitor相同
429rename_limit: no changes left this month409another rename for this identity is in progress422/503same as/visitor
错误Errors
错误响应的 error 是给人看的英文说明;需要程序判断时请用 HTTP 状态码和 code 字段。
error is a human-readable message. Programs should branch on the HTTP status and the code field.
| 状态码 | Status | 含义 | Meaning |
|---|---|---|---|
400 | 参数不对,或 JSON 无法解析 | Bad field or unparsable JSON | |
401 | 缺少或错误的凭证,临时访客码已过期 | Missing or wrong credentials, or an expired guest code | |
403 | 临时访客不能执行此操作 | Not allowed for guests | |
404 / 405 | 路径不存在 / 请求方法不对 | Unknown path / wrong method | |
409 | 同类操作正在进行 | Same operation already in progress | |
413 / 415 | 请求体超过 8 KB / 不是 JSON | Body over 8 KB / not JSON | |
422 | 名字未通过审查 | Name refused by review | |
429 | 频率、输错次数或每日额度超限,按 Retry-After(秒)等待 | Rate, failed-attempt or daily limit hit; wait Retry-After seconds | |
503 | 服务暂时不可用 | Temporarily unavailable |
| code | 出现于 | Seen on | 附加字段 | Extra fields |
|---|---|---|---|---|
guest_no_name | /visitor 400, /name 403 | — | ||
name_rejected | /visitor, /name 422 | reason | ||
name_review_unavailable | /visitor, /name 503 | — | ||
rename_limit | /name 429 | renamesLeft | ||
visitor_limit | /visitor 429 | — | ||
visitor_busy | /visitor 429 | — | ||
ip_daily_limit | /message 429 | — | ||
daily_limit | /message 429 | reply |
限制Limits
以下是当前的默认值,可能随时调整。超限时返回 429 和 Retry-After。
Current defaults; they may change. Exceeding one returns 429 with Retry-After.
按 IP 的限制里,IPv6 地址按 /64 网段算作同一个 IP。
For per-IP limits, IPv6 addresses in the same /64 count as one IP.
| 项目 | Limit | 默认值 | Default |
|---|---|---|---|
| 每个访客发送 | Messages per visitor | 6 次/分钟,60 次/小时 | 6 / min, 60 / hour |
| 每个 IP 发送 | Messages per IP | 6 次/分钟,60 次/小时,40 次/天(滚动 24 小时,超出返回 ip_daily_limit) | 6 / min, 60 / hour, 40 / day (rolling 24 h; ip_daily_limit beyond that) |
| 申请新身份 | New identities | 每个 IP 5 个/小时、10 个/天;全站 30 个/小时 | 5 / hour and 10 / day per IP; 30 / hour site-wide |
| 凭证输错 | Failed credentials | 每个码 10 次/小时,每个 IP 20 次/小时 | 10 / hour per code, 20 / hour per IP |
| 全站每日对话 | Site-wide daily turns | 有总量上限,用完返回 daily_limit | Capped; returns daily_limit when used up |
| 改名 | Renames | 3 次/自然月 | 3 / calendar month |
| 临时访客码 | Guest code | 闲置 60 分钟作废 | Expires after 60 min idle |
| 消息长度 / 名字长度 | Text / name length | 2000 / 24 字符 | 2000 / 24 characters |
数据与隐私Data & privacy
- 对话会被保存,站长可以查看。访客记录默认保存 30 天后删除。
- 服务端只存密码的哈希值,丢失后无法找回,只能重新申请。
- Alice 会记住与你的对话,但不会把你的内容告诉其他访客。
- 请不要在对话中发送密码、证件号等敏感信息。
- Chats are stored and the site owner can read them. Visitor records are deleted after 30 days by default.
- Only a hash of the key is stored. A lost key cannot be recovered; get a new code instead.
- Alice remembers your conversations but does not share them with other visitors.
- Please do not send passwords, ID numbers or other sensitive data.