@blockscout/api-types

中文文档
12.0.4发布于 2026/11/02LicenseRef-BlockscoutTypeScriptES Module零依赖近 30 天下载 2311

TypeScript types generated from Blockscout OpenAPI specs (public, private, and chain-specific)

原始使用文档
文档目录8 节
  1. Prerequisites
  2. Generate
  3. Usage
  4. Shorthands (recommended)
  5. Merged schemas
  6. Per-spec namespaces
  7. Chain types
  8. Schemas merging

@blockscout/api-types

TypeScript types generated from Blockscout OpenAPI specs via openapi-typescript:

  • shorthands (schemas, operations, paths) — the recommended entry point; flat lookups over the merged spec, spanning every chain type. See Usage
  • merged — every schema, path, and operation from all specs below combined into one namespace (for universal frontends that handle any chain type at runtime); see Merged schemas
  • publicApi — default public API (BlockScoutWeb.Specs.Public, no CHAIN_TYPE)
  • privateApi — account API (BlockScoutWeb.Specs.Private)
  • chain namespaces — chain-specific public API (CHAIN_TYPE set; matches generate-swagger.yml)

Build artifacts (openapi/, dist/) are gitignored; run npm run build after clone.

Prerequisites

  • Elixir / Mix at the Blockscout repo root (compiled block_scout_web app)
  • Node.js 18+ (for openapi-typescript)

Generate

From this directory:

npm install
npm run build

build runs:

  1. generate:spec — mix openapi.spec.yaml for public, private, and each chain type
  2. generate:types — openapi-typescript writes matching files under dist/

Usage

Three top-level exports flatten the most common lookups against the merged spec, so you never index through components["schemas"] or ["responses"][200]["content"]["application/json"] by hand. Prefer these for everyday use:

import type { schemas, operations, paths } from "@blockscout/api-types";

// `schemas` — a model by name (merged across all chains; chain-specific props optional).
type Transaction = schemas["Transaction"];

// `operations` — keyed by short operationId, with `json` / `params` / `requestBody`.
type TxJson = operations["TransactionController.transaction"]["json"];
type TxParams = operations["TransactionController.transaction"]["params"];
type EthCallBody = operations["EthController.eth_call"]["requestBody"];

// `paths` — the 200 `application/json` body keyed by path, then HTTP method.
type Blocks = paths["/api/v2/blocks"]["get"];

Notes:

  • operationIds are shortened — the controller module prefix (BlockScoutWeb.API.V2., BlockScoutWeb.Account.API.V2., BlockScoutWeb.API.Legacy.) is stripped, so BlockScoutWeb.API.V2.TransactionController.transaction becomes TransactionController.transaction.
  • json is the 200 application/json response body; params is the request query/path parameters; requestBody is the application/json request body (never for endpoints without one).
  • paths only exposes the HTTP methods an endpoint actually defines; missing methods are absent, not never.
  • All three are derived from merged, so they already span every chain type. Reach for the merged/per-chain namespaces below only when you need something these don't cover (non-200 responses, response headers, raw path items).

Merged schemas

The shorthands above are built on the merged namespace — every schema, path, and operation from all specs combined, so a universal frontend can handle any chain type at runtime from a single type universe. Import merged directly when you need lookups the shorthands don't expose (non-200 responses, response headers, raw path items):

import type { merged } from "@blockscout/api-types";

// Includes the public base plus `arbitrum?`, `zksync?`, blob fields, etc. — all optional.
type Transaction = merged.components["schemas"]["Transaction"];

// Paths/operations are merged too, referencing the merged models:
type Tx = merged.paths["/api/v2/transactions/{transaction_hash_param}"]["get"]["responses"][200];

Per-spec namespaces

Types are also grouped by spec name in the package entry:

import type { publicApi, privateApi, arbitrum, optimismCelo } from "@blockscout/api-types";

type AddressResponse = publicApi.components["schemas"]["AddressResponse"];

type GetAddressParams =
  publicApi.paths["/api/v2/addresses/{address_hash_param}"]["get"]["parameters"];

type ArbitrumPath = arbitrum.paths[keyof arbitrum.paths];

publicApi / privateApi avoid TypeScript reserved words (public, private). optimism-celo is exported as optimismCelo (hyphen is invalid in identifiers)

Chain types

Synced with .github/workflows/generate-swagger.yml via scripts/chain-types.sh:

arbitrum, arc, blackfort, ethereum, filecoin, neon, optimism, optimism-celo, rsk, scroll, shibarium, stability, suave, zetachain, zilliqa, zksync

When CI adds or removes a chain type, update scripts/chain-types.sh and index.ts.

Schemas merging

scripts/merge-specs.mjs merges all generated specs (public → private → chains alphabetically) into openapi/merged.yaml, which compiles to dist/merged.schema.ts (exported as merged).

Merge semantics:

  • Schemas — properties are unioned; required is the intersection across specs defining a schema, so chain-specific properties are optional. Sub-objects defined in a single spec keep their required (e.g. arbitrum.gas_used_for_l1 stays required when arbitrum is present). enum values are unioned (e.g. transaction_types gains chain-specific entries).
  • Paths & operations — unioned across specs; chain-only endpoints (e.g. /v2/arbitrum/batches) are included. Shared operations are deep-merged, with parameters matched by (in, name) and their schemas unioned (e.g. enum query params gain chain-specific values). All operation $refs point at components.schemas, so they reference the merged models automatically.
  • Irreconcilable schema shapes (e.g. ethereum redefines Status as a beacon-deposit enum) become anyOf unions and are reported as warnings on stderr.

版本与发布记录

@blockscout/api-types 共发布 31 个版本,首次发布于 2026/06/05;过去 12 个月发布了 31 个版本。

版本发布日期距今
12.0.4latest2026/11/025 天前
0.0.1-beta.e1f332fbeta2026/10/318 天前
12.0.32026/10/2118 天前
12.0.22026/10/1622 天前
12.0.12026/10/1227 天前
12.0.02026/10/061 个月前

关于 @blockscout/api-types 的常见问题

@blockscout/api-types 是什么?

TypeScript types generated from Blockscout OpenAPI specs (public, private, and chain-specific)

如何安装 @blockscout/api-types?

在项目目录运行 npm install @blockscout/api-types,或使用 pnpm add / yarn add / bun add。

@blockscout/api-types 支持 TypeScript 吗?

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

@blockscout/api-types 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@blockscout/api-types 可以商用吗?

@blockscout/api-types 使用 LicenseRef-Blockscout 许可。声明的许可为 LicenseRef-Blockscout,请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

@blockscout/api-types 的下载量有多少?

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

@blockscout/api-types 还在维护吗?

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