详解 AT URI 和 bsky.app 链接
面向开发者的 at:// URI 语法:authority、集合 NSID 和记录键,bsky.app 链接如何映射到 AT URI,以及如何在代码中转换。
AT URI 指向什么
AT URI 指向一个账户,或指向其仓库中的一条记录。人们使用 bsky.app 链接,而应用和 API 使用的地址就是 AT URI。它的语法来自 AT URI 规范:
at://AUTHORITY/COLLECTION/RKEY- Authority:账户,写作 DID(
did:plc:…、did:web:…)或账户代码(handle)。记录是按 DID 存储的,因此保存下来的 URI 应当始终使用 DID:账户代码可能会易主。 - Collection:记录类型,即一个 NSID,例如
app.bsky.feed.post。 - 记录键(
rkey):记录在其集合中的名称。大多数是 TID,即时间戳标识符,例如3l6oveex3ii2l;个人资料的记录键始终是self。
at://did:plc:z72i7hdynmk6r22z27h6tvur 是一个账户,at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post 是它的所有帖文,再加上第三部分就指向一条帖文。在转换工具读取的记录中,URI 没有查询字符串,也没有片段。
从 bsky.app 到 at://,再转回来
Bluesky 应用为五种记录提供页面。转换工具可以双向转换它们:
| bsky.app 链接 | AT URI |
|---|---|
/profile/{actor} | at://{did} |
/profile/{actor}/post/{rkey} | at://{did}/app.bsky.feed.post/{rkey} |
/profile/{actor}/feed/{rkey} | at://{did}/app.bsky.feed.generator/{rkey} |
/profile/{actor}/lists/{rkey} | at://{did}/app.bsky.graph.list/{rkey} |
/starter-pack/{actor}/{rkey} | at://{did}/app.bsky.graph.starterpack/{rkey} |
{actor} 是账户代码或 DID:bsky.app 两者都接受。使用 DID 的链接在账户更改账户代码时永远不会失效。其他集合的记录(一个喜欢、一次关注、其他应用的记录)没有 bsky.app 页面:转换工具只给出它们的 AT URI 及其各个部分。
要把账户代码转换为 DID,转换工具会向 Bluesky 的 AppView 查询(com.atproto.identity.resolveHandle)。要显示带账户代码的链接,它会读取 DID 文档,并且只有当该账户代码能反向解析到同一个 DID 时才保留它。
在代码中转换
在 TypeScript 中,@atproto/syntax 的 AtUri 可以解析和构建 URI。帖文链接可以这样转换为它的 URI:
import { AtUri } from "@atproto/syntax";
const COLLECTIONS: Record<string, string> = {
post: "app.bsky.feed.post",
feed: "app.bsky.feed.generator",
lists: "app.bsky.graph.list",
};
export function postLinkToAtUri(link: string, did: string): string {
const [, , kind, rkey] = new URL(link).pathname.split("/").filter(Boolean);
const collection = COLLECTIONS[kind];
if (!collection || !rkey) throw new Error("Not a post, feed or list link");
return AtUri.make(did, collection, rkey).toString();
}
const uri = new AtUri("at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post/3l6oveex3ii2l");
uri.host; // DID
uri.collection; // "app.bsky.feed.post"
uri.rkey; // "3l6oveex3ii2l"在 Python 中,AT URI 足够简单,可以手动拆分:
def parse_at_uri(uri: str) -> dict:
if not uri.startswith("at://"):
raise ValueError("not an AT URI")
parts = uri[len("at://"):].split("/")
return {
"authority": parts[0],
"collection": parts[1] if len(parts) > 1 else None,
"rkey": parts[2] if len(parts) > 2 else None,
}请先把账户代码解析为 DID(使用 resolveHandle 或 DID 查找工具),这样你保存的 URI 才能一直有效。
通过 API 或 AI 智能体使用
这个转换工具也可以通过 GET /api/v1/tools/at-uri-converter?uri=… 和 MCP 工具 convert_at_uri 使用:请参阅工具 API 和 MCP 服务器。当记录键是 TID 时,TID 解码器可以读出它所编码的时间。