@elastic/schemas

中文文档
0.11.0发布于 2026/10/30Apache-2.0TypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 22.8万

Request and response schemas for all Elastic APIs

原始使用文档
文档目录9 节
  1. Installation
  2. Optional dependencies
  3. Usage
  4. JSON schemas
  5. Zod schemas
  6. Tool registry
  7. Tool manifests
  8. Available exports
  9. Version support

Elastic Schemas

@elastic/schemas Build status

Schema definitions for all Elastic APIs, including Elasticsearch, Kibana, and Cloud. Available in multiple forms:

  • Zod schemas
  • plain JSON schemas
  • a tool registry optimized for LLM-based agents

Also includes basic utilities for building HTTP API requests from inputs that match one of the provided schemas.

Installation

Install from npm:

npm install @elastic/schemas

Optional dependencies

Zod is an optional dependency. If you only need JSON schemas or the tool registry, you don't need to install Zod. If you want to use Zod schemas, install it explicitly:

npm install zod

Usage

JSON schemas

Static JSON Schema files are available for each API's request and response. Namespaced APIs are named {namespace}.{name}.request.json; top-level (global) APIs are named {name}.request.json.

import searchRequest from '@elastic/schemas/es/json/search.request.json' with { type: 'json' }
import searchResponse from '@elastic/schemas/es/json/search.response.json' with { type: 'json' }
import findRulesRequest from '@elastic/schemas/kibana/json/find_rules.request.json' with { type: 'json' }
import createApiKeyRequest from '@elastic/schemas/cloud/json/create_api_key.request.json' with { type: 'json' }

The schemas can be used directly with any JSON Schema validator, e.g. Ajv:

import Ajv from 'ajv'
import searchRequest from '@elastic/schemas/es/json/search.request.json' with { type: 'json' }

const ajv = new Ajv()
const validate = ajv.compile(searchRequest)

const valid = validate({ index: 'my-index', query: { match_all: {} } })
if (!valid) console.error(validate.errors)

Zod schemas

If you have Zod installed, import individual schemas by their module path and use them like any Zod schema:

import { SearchRequest } from '@elastic/schemas/es/schemas/search.js'

const result = SearchRequest.safeParse({
  index: 'my-index',
  query: { match: { title: 'elasticsearch' } },
  size: 10,
})

if (!result.success) {
  console.error(result.error.issues)
}

Tool registry

Load a tool definition by API ID and build a ready-to-send HTTP request:

import { esRegistry } from '@elastic/schemas/es/tools/index.js'

const api = await esRegistry.loadApi('search')

const request = api.buildRequest({
  index: 'my-index',
  query: { match: { title: 'elasticsearch' } },
  size: 10,
})
// { method: 'GET', path: '/my-index/_search', body: { query: ..., size: 10 } }

Each loaded definition carries two classification flags. destructive marks operations that delete or mutate existing data. readOnly marks operations that only read: every GET and HEAD call except a few with side effects, plus search-style POST calls such as count or sql.query. A read-only operation is never destructive. The same flags appear on the JSON Schema request documents as x-destructive and x-read-only.

Browse available API IDs via esRegistry.manifest (an array of { id, name, namespace, description, destructive, readOnly } entries).

Tool manifests

Each product exposes a manifest: a lightweight array of ApiRegistryMeta objects describing every available API ({ id, name, namespace, description, namespaceFile, destructive, readOnly }). The destructive and readOnly flags match the loaded definition's, so you can filter APIs by classification without loading any definitions. You can import a manifest directly without loading any schema code:

import { kibanaManifest } from '@elastic/schemas/kibana/tools/manifest.js'
import { esManifest } from '@elastic/schemas/es/tools/manifest.js'
import { cloudManifest } from '@elastic/schemas/cloud/tools/manifest.js'
import { serverlessManifest } from '@elastic/schemas/serverless/tools/manifest.js'

The ApiRegistryMeta type is exported from the main entry point:

import type { ApiRegistryMeta } from '@elastic/schemas'

Available exports

:warning: Importing all the schemas as a single object is going to consume a lot of memory, so we encourage you to use the exact path to the schema you need:

import { SearchRequest } from '@elastic/schemas/es/schemas/search.js'

However, if you do need a full export as one object, the following exports are provided:

ExportDescription
ElasticsearchSchemasZod schemas for all ES APIs
ElasticsearchToolsES tool registry + esRegistry
KibanaSchemasZod schemas for all Kibana APIs
KibanaToolsKibana tool registry + kibanaRegistry
CloudSchemasZod schemas for all Cloud APIs
CloudToolsCloud tool registry + cloudRegistry
ServerlessSchemasZod schemas for all Serverless APIs
ServerlessToolsServerless tool registry + serverlessRegistry
ApiRegistryMetaShared type for manifest entries

Version support

The schemas in this package reflect the API specifications for the latest Elasticsearch Serverless release of Elasticsearch and Kibana only, as well as the latest stable release of the Elastic Cloud control plane API. Support for other versions of the Elastic stack is coming!

版本与发布记录

@elastic/schemas 共发布 22 个版本,首次发布于 2026/07/22;过去 12 个月发布了 22 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.11.0latest2026/10/3014 天前
0.10.02026/10/061 个月前
0.9.12026/10/011 个月前
0.9.02026/09/301 个月前
0.8.12026/09/291 个月前
0.8.02026/09/191 个月前

其他发布通道:alpha → 0.4.0-alpha.1。

关于 @elastic/schemas 的常见问题

@elastic/schemas 是什么?

Request and response schemas for all Elastic APIs

如何安装 @elastic/schemas?

在项目目录运行 npm install @elastic/schemas,或使用 pnpm add / yarn add / bun add。

@elastic/schemas 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@elastic/schemas 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@elastic/schemas 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@elastic/schemas 可以商用吗?

@elastic/schemas 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@elastic/schemas 的下载量有多少?

根据 npm 官方下载统计,@elastic/schemas 在 2026/10/14 – 2026/11/12 这 30 天内被下载 227,647 次,日均约 7,588 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@elastic/schemas 还在维护吗?

最新版本 0.11.0 发布于 2026/10/30(14 天前);至今共发布 22 个版本,其中过去 12 个月发布了 22 个;首次发布于 2026/07/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。