@aptos-labs/aptos-client

中文文档
4.1.0发布于 2026/05/21Apache-2.0TypeScriptES Module构建来源已验证近 30 天下载 75.2万

Client package for accessing the Aptos network API.

原始使用文档
文档目录14 节
  1. Features
  2. Installation
  3. Usage
  4. Named exports
  5. Runtime Resolution
  6. Types
  7. HTTP/2
  8. Migrating from v2
  9. What works the same as v2
  10. Behavior changes (observable to callers)
  11. New capabilities since v2
  12. Removed since v2
  13. Migration checklist
  14. Releasing a new version

License NPM Package Version Node Version NPM bundle size NPM Package Downloads codecov

@aptos-labs/aptos-client

HTTP client for the Aptos network API. Works standalone or as the transport layer for the Aptos TypeScript SDK.

Features

  • HTTP/2 — enabled by default on all platforms
  • Multi-runtime — Node.js, Deno, Bun, browsers, and React Native
  • Cookie jar — automatic cookie handling in Node, Deno, and Bun
  • BCS support — bcsRequest() returns raw ArrayBuffer for binary-encoded responses

Installation

npm install @aptos-labs/aptos-client
# or
pnpm add @aptos-labs/aptos-client

Usage

import aptosClient from "@aptos-labs/aptos-client";

const { status, data } = await aptosClient<{ chain_id: number }>({
  url: "https://fullnode.mainnet.aptoslabs.com/v1",
  method: "GET",
});

Named exports

import { jsonRequest, bcsRequest } from "@aptos-labs/aptos-client";

// JSON (same as default export)
const json = await jsonRequest<MyType>({ url, method: "GET" });

// BCS (returns ArrayBuffer)
const bcs = await bcsRequest({ url, method: "GET" });

Runtime Resolution

The package uses conditional exports to select the right implementation for each runtime:

ConditionEntry pointHTTP/2Notes
nodeindex.node.tsConfigurable via http2 option (default true)Uses got (decodes br/gzip/deflate transparently on H1 and H2)
browserindex.browser.tsAutomatic (browser engine)Delegates cookies to the browser
react-nativeindex.fetch.tsAutomatic (OkHttp / NSURLSession)Platform negotiates HTTP/2 via ALPN
denoindex.fetch.tsAutomatic—
bunindex.fetch.tsAutomatic—
workerdindex.fetch.tsAutomaticCloudflare Workers
edge-lightindex.fetch.tsAutomaticVercel Edge Functions
defaultindex.fetch.tsDepends on runtimeFallback for unknown runtimes

Types

type AptosClientRequest = {
  url: string;
  method: "GET" | "POST";
  body?: unknown;
  params?: Record<string, string | number | bigint | boolean | undefined>;
  headers?: Record<string, string | undefined>;
  overrides?: { WITH_CREDENTIALS?: boolean };
  http2?: boolean;    // Node only — ignored elsewhere
  cookieJar?: CookieJarLike; // Per-request cookie isolation (Node & fetch only)
};

type AptosClientResponse<Res> = {
  status: number;
  statusText: string;
  data: Res;
  config?: any;
  request?: any;
  response?: any;
  headers?: Record<string, string | string[]>;
};

See src/types.ts for the full type definitions with documentation.

HTTP/2

RuntimeHow it works
Node.jsgot negotiates HTTP/2 via ALPN (powered by http2-wrapper) when http2: true (the default). Set http2: false to force HTTP/1.1.
BrowserThe browser engine negotiates HTTP/2 with the server automatically. The http2 option is ignored.
React NativeOkHttp (Android) and NSURLSession (iOS) negotiate HTTP/2 via ALPN automatically. The http2 option is ignored.
Deno / BunThe runtime negotiates HTTP/2 automatically. The http2 option is ignored.

Migrating from v2

v4.1.0 returns to the same HTTP library v2 used (got) while keeping the v4 architecture (ESM, conditional exports, multi-runtime). For most callers the migration is one or two small edits.

If you are on v3.0.0 through v4.0.0, upgrade. Those versions silently return raw compressed bytes instead of parsed JSON whenever the origin sends content-encoding: br/gzip/deflate — which is what the Aptos fullnode and indexer do. 4.1.0 restores v2-era decompression behavior.

What works the same as v2

  • http2: true default; falls back to H1.1 when the server doesn't support H2.
  • Brotli / gzip / deflate decompression handled transparently.
  • Cookie jar round-trips set-cookie and re-sends cookies on same-origin requests.
  • bigint query params are stringified.
  • 4xx / 5xx responses do not throw — inspect res.status.
  • AptosClientResponse shape: { status, statusText, data, config, request, response, headers } with headers as a plain Record<string, string | string[]>.

Behavior changes (observable to callers)

Changev2v4.1.0
BCS response typeBufferArrayBuffer — cross-runtime, no Buffer polyfill needed. The bytes are identical; use new Uint8Array(res.data) or Buffer.from(res.data) if you need either shape.
Retriesgot default (limit: 2, backoff)Off (limit: 0). Wrap in your own retry loop, or let the Aptos TS SDK manage retries at a higher layer.
Non-JSON bodiesJSON.parse would throwFalls back to returning the raw text in data — lets callers inspect the status code regardless of body content-type.
Empty / 204 / 205 bodiesWhatever got returned (often "" or undefined)Explicitly null.
statusText over HTTP/2Empty string (H2 has no reason phrase)Falls back to http.STATUS_CODES[code] — "OK", "Not Found", etc.
NODE_TLS_REJECT_UNAUTHORIZED=0 on H2Inherited transitively (worked by accident)Explicitly propagated to got's https.rejectUnauthorized — works on both H1 and H2.

New capabilities since v2

  • Per-request cookie jar isolation — pass cookieJar: new CookieJar() to keep multi-tenant requests from sharing cookie state.
  • Public CookieJar is now exported (with RFC 6265 validation, expiry eviction, per-origin caps, and SameSite=None+Secure enforcement).
  • CookieJarLike interface — bring your own jar (e.g. tough-cookie or a database-backed store).
  • Conditional exports auto-select the right entry for Node / browser / Deno / Bun / Cloudflare Workers / Vercel Edge / React Native. v2 only shipped Node + browser.
  • overrides.WITH_CREDENTIALS (browser entry) maps to fetch's credentials: "omit" | "include".

Removed since v2

  • CJS require() — the package is ESM-only since v4.0. Use import or await import(...).
  • Re-export of got error types — if you handled RequestError / HTTPError directly, import them from got (it's still a direct dependency).
  • Node < 22 — the minimum supported Node version is 22.

Migration checklist

  1. Using require()? → switch to import (or await import() from CJS).
  2. Calling Buffer methods on a BCS response? → use new Uint8Array(res.data), new DataView(res.data), or wrap with Buffer.from(res.data).
  3. Relying on automatic retries? → add an explicit retry wrapper. (Most consumers — including the Aptos TS SDK — don't need to.)
  4. Node version < 22? → upgrade.

That's the full migration. Everything else either works the same, is additive, or is a fixed bug.

Releasing a new version

Releases are published to npm automatically via GitHub Actions whenever a GitHub release is created. The workflow lives in .github/workflows/publish.yml.

To release a new version:

  1. Update the version in package.json (follows semver):

    npm version <major|minor|patch> --no-git-tag-version
    
  2. Update CHANGELOG.md — move any notes under # Unreleased into a new section for the version being released.

  3. Commit and push the version bump and changelog update to main:

    git add package.json CHANGELOG.md
    git commit -m "v<VERSION>"
    git push
    
  4. Create a GitHub release with a tag that matches vMAJOR.MINOR.PATCH (e.g. v2.3.0). The tag must match the version in package.json — the publish workflow will fail otherwise. Pre-release tags like v2.3.0-beta.1 are also supported.

The publish workflow will then automatically:

  • Validate that the tag matches the expected vMAJOR.MINOR.PATCH[-prerelease] pattern.
  • Verify that the tag matches the version field in package.json.
  • Install dependencies, build the package, and publish to npm with provenance.

版本与发布记录

@aptos-labs/aptos-client 共发布 15 个版本,首次发布于 2023/07/14;过去 12 个月发布了 7 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
4.1.0latest2026/05/215 个月前
4.0.02026/04/156 个月前
3.0.22026/04/086 个月前
3.0.12026/03/147 个月前
3.0.02026/03/137 个月前
2.2.02026/03/068 个月前

关于 @aptos-labs/aptos-client 的常见问题

@aptos-labs/aptos-client 是什么?

Client package for accessing the Aptos network API.

如何安装 @aptos-labs/aptos-client?

在项目目录运行 npm install @aptos-labs/aptos-client,或使用 pnpm add / yarn add / bun add。

@aptos-labs/aptos-client 支持 TypeScript 吗?

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

@aptos-labs/aptos-client 支持 ESM 还是 CommonJS?

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

@aptos-labs/aptos-client 需要什么版本的 Node.js?

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

@aptos-labs/aptos-client 可以商用吗?

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

@aptos-labs/aptos-client 的下载量有多少?

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

@aptos-labs/aptos-client 还在维护吗?

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