DirX 文档
DirX 让 Agent 以统一方式访问互联网 API — 导航、门禁、审计。本文档涵盖 CLI 命令、SDK、配置与错误码。
Agent 入口
如果你正在开发 AI Agent,可以让 Agent 通过以下 URL 获取 DirX 的机器可读参考文档:
或者直接复制以下介绍文案,粘贴给任何 Agent:
Try dirx (dirx.ai) — an open agent API gateway with built-in access control and audit. Install: brew install dirx Full API reference: https://dirx.ai/agent.md Then: dirx ls /
agent.md 返回纯 Markdown,包含所有命令签名、参数和示例 — 专为 LLM 上下文窗口优化。
快速开始
Agent 通过 DirX CLI 以类 Unix 方式访问互联网 API。
# 1. Installbrew install dirx # or: npm install -g @dirxai/cli# 2. Authenticate$ dirx authToken saved to ~/.dirx/credentials# 3. Browse namespaces$ dirx ls /net/ skills/ docs/ policies/ runs/# 4. Read API resources$ dirx ls /net/api.github.com/repos/ users/ orgs/ issues/ actions/$ dirx cat /net/api.github.com/repos/dirxai/dirx{"name":"dirx","full_name":"dirxai/dirx","stars":128}# 5. Search across services$ dirx grep "deploy" /net/api.github.com — GitHub v3 REST APIapi.cloudflare.com — Cloudflare API
命令参考
DirX 提供 8 个核心命令来操作互联网 API:认证、浏览、读取、搜索、写入、删除、编辑、管道。
auth
认证并获取访问令牌。首次使用或令牌过期时必须执行。
Usage
dirx auth [--provider <name>] [--scope <scope>]Example
$ dirx authOpening browser for OAuth2 flow...Token saved to ~/.dirx/credentials$ dirx auth --provider api-keyEnter API key: ***
Options
| Flag | Description |
|---|---|
| --provider <name> | 认证方式(oauth2 / api-key) |
| --scope <scope> | 请求的权限范围 |
ls
列出路径下的资源。类似 Unix ls — 支持 glob 和递归。
Usage
dirx ls [path] [options]Example
$ dirx ls /net/ skills/ docs/ policies/ runs/$ dirx ls /net/api.github.com/ api.cloudflare.com/ api.openai.com/ ...$ dirx ls -l /net/api.github.com/drwxr repos/ 42 endpoints officialdrwxr users/ 12 endpoints officialdrwxr issues/ 8 endpoints official
Options
| Flag | Description |
|---|---|
| -r, --recursive | 递归列出子路径 |
| -l | 长格式,显示权限与元数据 |
| --depth <n> | 递归深度限制 |
cat
读取资源内容。输出 JSON 或原始字节。
Usage
dirx cat <path> [options]Example
$ dirx cat /net/api.github.com/repos/dirxai/dirx{"name":"dirx","full_name":"dirxai/dirx","stars":128,...}$ dirx cat /net/api.openai.com/models{"data":[{"id":"gpt-4o",...},{"id":"gpt-4o-mini",...}]}$ dirx cat /net/api.github.com/repos/dirxai/dirx | jq .stars128
Options
| Flag | Description |
|---|---|
| -j, --json | 强制 JSON 输出 |
| -r, --raw | 原始字节输出 |
| -H, --header | 显示响应头 |
grep
跨服务搜索匹配的端点。支持正则表达式。
Usage
dirx grep <pattern> [path] [options]Example
$ dirx grep "issues" /net/api.github.com/GET /repos/{owner}/{repo}/issues — List issuesPOST /repos/{owner}/{repo}/issues — Create issue$ dirx grep "deploy" /net/api.github.com — GitHub v3 REST APIapi.cloudflare.com — Cloudflare APIapi.vercel.com — Vercel API
Options
| Flag | Description |
|---|---|
| -i | 忽略大小写 |
| -v | 反向匹配(排除) |
| -c, --count | 仅显示匹配数量 |
write
创建或覆盖资源。映射到 POST/PUT。支持 JSON、YAML、纯文本。
Usage
dirx write <path> [options]Example
$ echo '{"title":"Bug report"}' | dirx write /net/api.github.com/repos/dirxai/dirx/issuesCreated: POST /repos/dirxai/dirx/issues -> 201$ dirx write /net/api.cloudflare.com/zones/{z}/dns_records \--body '{"type":"A","name":"app","content":"1.2.3.4"}'Created: POST /zones/{z}/dns_records -> 200
Options
| Flag | Description |
|---|---|
| -f, --force | 覆盖已有资源 |
| --body <json> | 请求体(JSON 字符串) |
| --merge | 合并而非覆盖 |
rm
删除资源。映射到 DELETE。高风险操作 — 需要明确确认或 --force。
Usage
dirx rm <path> [options]Example
$ dirx rm /net/api.github.com/repos/dirxai/dirx/issues/42⚠ DELETE /repos/dirxai/dirx/issues/42 confirm? [y/N] yDeleted: 204$ dirx rm --force /net/api.cloudflare.com/zones/{z}/dns_records/{id}Deleted: 200
Options
| Flag | Description |
|---|---|
| -f, --force | 跳过确认提示 |
| --dry-run | 仅模拟,不实际删除 |
edit
部分更新 — 仅修改指定字段(PATCH 语义)。
Usage
dirx edit <path> [options]Example
$ dirx edit /net/api.github.com/repos/dirxai/dirx \--set description="Agent gateway"Updated: PATCH /repos/dirxai/dirx -> 200$ dirx edit /net/api.cloudflare.com/zones/{z}/settings --set ssl=strictUpdated: PATCH /zones/{z}/settings -> 200
Options
| Flag | Description |
|---|---|
| --set <k>=<v> | 设置字段(可多次使用) |
| --unset <key> | 移除字段 |
bash
复合管道。用 | 和 shell 结构组合命令。
Usage
dirx bash [-c <script>] [--script <file>]Example
# Find repos with 1000+ stars$ dirx bash -c 'dirx ls /net/api.github.com/repos | dirx grep "stars > 1000"'# Cross-service search$ dirx bash -c 'dirx grep "deploy" /net/ | while read svc; dodirx cat "$svc"done'
Options
| Flag | Description |
|---|---|
| -c <script> | 内联脚本 |
| --script <file> | 从文件读取脚本 |
API 提供者
如果你是 API 服务提供方,以下命令帮你将 API 注册到 DirX,让 Agent 能发现并调用。
init
在当前目录初始化 DirX 项目配置。
Usage
dirx init [--force]Example
$ dirx initCreated .dirx/config.jsonCreated .dirx/.gitignore
Options
| Flag | Description |
|---|---|
| --force | 覆盖已有配置 |
claim
通过 DNS 或 HTTP 验证认领 API 域名所有权。
Usage
dirx claim <domain> [options]Example
$ dirx claim api.example.comAdd CNAME record: api.example.com -> verify.dirx.aiPress Enter when ready... Claimed successfully.
Options
| Flag | Description |
|---|---|
| --method dns|http | 校验方式,默认 dns |
| --ttl <sec> | DNS TTL(秒) |
generate
从 OpenAPI/Swagger 规范生成 DIR.md 和 dir.json。
Usage
dirx generate [options]Example
$ dirx generate --spec openapi.jsonGenerated DIR.md (42 endpoints)Generated dir.json
Options
| Flag | Description |
|---|---|
| --spec <path> | OpenAPI 规范路径 |
| -o, --output <dir> | 输出目录 |
| --format md|json | 输出格式 |
register
注册项目到 DirX 网关。上传 DIR.md 和 dir.json。
Usage
dirx register [options]Example
$ dirx registerPublishing to DirX Gateway...Registered: api.example.com -> /net/api.example.com/
Options
| Flag | Description |
|---|---|
| --dry-run | 仅校验,不实际注册 |
| --env staging|prod | 目标环境 |
密钥管理
DirX 支持三级凭证存储:本地 → 密钥管理器 → Vault。API 密钥由网关在调用时自动注入,Agent 无需直接接触密钥。
keys set
将 API 密钥关联到域名。网关在后续请求中自动注入。
Usage
dirx keys set <domain> --token <value> [options]Example
$ dirx keys set api.github.com --token ghp_xxxxxxxxxxxxKey saved for api.github.com (local)$ dirx keys set api.openai.com --token sk-... --syncKey saved for api.openai.com (local + vault)
Options
| Flag | Description |
|---|---|
| --sync | 同步到 DirX Vault(AES-GCM 加密) |
| --overwrite | 覆盖已有密钥 |
keys list
列出已存储的密钥域名(值已脱敏)。
Usage
dirx keys list [options]Example
$ dirx keys listDOMAIN STORAGE MASK UPDATEDapi.github.com local ****xxxx 2026-02-27api.openai.com vault ****sk-.. 2026-02-27api.cloudflare.com local ****xxxx 2026-02-26
Options
| Flag | Description |
|---|---|
| --storage local|vault | 按存储位置过滤 |
keys remove
删除域名的 API 密钥(本地和 Vault)。
Usage
dirx keys remove <domain> [options]Example
$ dirx keys remove api.github.comRemoved key for api.github.com
Options
| Flag | Description |
|---|---|
| --local-only | 仅删除本地密钥,保留 Vault |
SDK 参考
@dirxai/core — TypeScript SDK,用于在代码中集成 DirX:配置、初始化、策略检查、Agent 上下文。
defineConfig
定义 DirX 配置:网关、认证、策略、密钥、审计。
Signature
function defineConfig<T extends DirxConfig>(options: T): T
Example
const config = defineConfig({gateway: "https://gateway.dirx.ai",auth: { provider: "jwt", tokenEndpoint: "/auth/token" },policies: { defaultAction: "allow" },});
init
初始化 DirX 客户端。使用环境变量或显式配置。
Signature
function init(config?: DirxConfig): DirxClient
Example
import { init, defineConfig } from "@dirxai/core";const client = init(defineConfig({gateway: "https://gateway.dirx.ai",auth: { provider: "api-key" },}));
guard
策略检查。返回上下文是否被给定策略允许。
Signature
function guard(ctx: AgentContext, policy: Policy): Promise<boolean>
Example
import { guard } from "@dirxai/core";const allowed = await guard({ agentId: "agent-1", path: "/net/api.github.com/repos" },policy);
AgentContext
Agent 上下文接口。携带身份、路径、动作用于策略评估。
Signature
interface AgentContext {agentId: string;path: string;action?: "read" | "write" | "list" | "delete";metadata?: Record<string, unknown>;}
Example
const ctx: AgentContext = {agentId: "my-agent-001",path: "/net/api.github.com/repos/dirxai/dirx",action: "read",};
配置
项目根目录的 dirx.config.ts 文件定义网关、认证、策略、密钥和审计配置。
| Key | Type | Description |
|---|---|---|
| gateway | string | 网关地址 |
| auth.provider | string | 认证方式(jwt / api-key / oauth2) |
| auth.tokenEndpoint | string | Token 端点 |
| policies.defaultAction | string | 默认策略(allow / deny) |
| policies.rules | Rule[] | 策略规则数组 |
| keys.storage | string | 密钥存储(local / vault / external) |
| keys.vaultEndpoint | string | Vault 端点 |
| audit.enabled | boolean | 是否启用审计 |
| audit.destination | string | 审计目标(stdout / file / remote) |
| audit.level | string | 审计级别(minimal / standard / verbose) |
export default defineConfig({gateway: "https://gateway.dirx.ai",auth: {provider: "jwt",tokenEndpoint: "https://gateway.dirx.ai/auth/token",},policies: {defaultAction: "allow",rules: [{ path: "/net/api.github.com/*", action: "allow" },{ path: "/net/*", action: "deny" },],},keys: { storage: "vault", vaultEndpoint: "https://vault.dirx.ai" },audit: { enabled: true, destination: "remote", level: "standard" },});
环境变量
环境变量可覆盖配置文件中的值,适用于 CI/CD 和容器化部署场景。
| Variable | Default | Description |
|---|---|---|
| DIRX_GATEWAY | gateway.dirx.ai | 网关地址 |
| DIRX_TOKEN | — | 访问令牌 |
| DIRX_AUTH_PROVIDER | jwt | 认证方式 |
| DIRX_KEYS_STORAGE | local | 密钥存储方式 |
| DIRX_VAULT_ENDPOINT | — | Vault 端点 |
| DIRX_AUDIT_ENABLED | false | 是否启用审计 |
| DIRX_AUDIT_DEST | stdout | 审计日志目标 |
| DIRX_LOG_LEVEL | info | 日志级别 |
| DIRX_CONFIG_PATH | ./dirx.config.ts | 配置文件路径 |
错误码
当请求失败时,DirX 会返回结构化的错误码和可读的原因说明,帮助快速定位问题。
| Code | # | Message | Cause | Fix |
|---|---|---|---|---|
| DIRX_ERR_AUTH_FAILED | 1001 | 认证失败 | Token 无效/格式错误/凭证已撤销 | 检查 DIRX_TOKEN;重新 dirx auth |
| DIRX_ERR_TOKEN_EXPIRED | 1002 | Token 过期 | JWT 已过期/刷新失败 | 重新执行 dirx auth |
| DIRX_ERR_FORBIDDEN | 2001 | 策略拒绝 | 策略不允许/IP 未授权 | 检查 policies.rules 配置 |
| DIRX_ERR_PATH_NOT_FOUND | 2002 | 路径不存在 | 路径拼写错误/API 未注册 | dirx ls 验证路径 |
| DIRX_ERR_RATE_LIMIT | 2003 | 频率超限 | 速率限制/配额用尽 | 降低请求频率 |
| DIRX_ERR_KEY_NOT_FOUND | 3001 | 密钥未找到 | 未为域名配置 Key | dirx keys set <domain> |
| DIRX_ERR_KEY_EXPIRED | 3002 | 密钥过期 | Key 已过期/被轮换 | dirx keys set --overwrite |
| DIRX_ERR_UPSTREAM_TIMEOUT | 4001 | 上游超时 | 目标 API 响应慢 | 检查上游服务状态 |
| DIRX_ERR_UPSTREAM_ERROR | 4002 | 上游错误 | API 返回 4xx/5xx | 查看错误响应体 |
| DIRX_ERR_INVALID_SPEC | 5001 | 无效规范 | OpenAPI 格式错误 | dirx generate --spec 重新生成 |
| DIRX_ERR_CLAIM_FAILED | 5002 | 认领失败 | DNS 记录未配置 | 确认 TXT/CNAME 记录已生效 |