@blockfrost/blockfrost-js

中文文档
6.2.0发布于 2026/08/21Apache-2.0TypeScriptCommonJS近 30 天下载 9.5万

A JavaScript/TypeScript SDK for interacting with the https://blockfrost.io API

原始使用文档
文档目录13 节
  1. Getting started
  2. Installation
  3. Usage
  4. Options
  5. Error handling
  6. BlockfrostServerError
  7. BlockfrostClientError
  8. Examples
  9. Cardano
  10. IPFS
  11. Utility functions
  12. Development
  13. Adding new method

master build ci npm version downloads

blockfrost-js


Blockfrost.io SDK for JavaScript for Node.js.

Getting started • Installation • Usage • Examples • SDK API Reference


Getting started

To use this SDK, you first need to log in to blockfrost.io, create your project and retrieve the API token.


Installation

To run the SDK you need Node.js version 16 and higher. While you may find a way to run it directly in a browser, we don't actively support or provide troubleshooting assistance with this scenario. We recommend setting up your own Node.js backend. Exposing your API keys in a frontend application is almost always a bad idea.

The SDK is hosted on npmjs.com, so you can directly import it using your favorite package manager.

npm i @blockfrost/blockfrost-js
yarn add @blockfrost/blockfrost-js

Usage

Using the SDK is pretty straight-forward as you can see from the following examples. For more examples take a look in blockfrost-js-examples repository.

For a list of all SDK methods check out our wiki.

const Blockfrost = require('@blockfrost/blockfrost-js');
// import { BlockFrostAPI } from '@blockfrost/blockfrost-js'; // using import syntax

const API = new Blockfrost.BlockFrostAPI({
  projectId: 'YOUR API KEY HERE', // see: https://blockfrost.io
  // For a list of all options see section below
});

Options

  • projectId - string, Blockfrost project ID (required)
  • network - string, Cardano network for given project ID. (optional, default value is derived from the projectId itself if possible). Possible values: mainnet, testnet, preview, preprod.
  • rateLimiter - boolean or RateLimiterConfig, whether to enable rate limiter that matches Blockfrost API limits (optional, default true). If you have your IP addresses white-listed you may want to disable it. You may also customize rate limiter by passing your own config object.
  • requestTimeout - number, How long to wait for a request to complete, in milliseconds (optional, default 20000)
  • retrySettings - RequiredRetryOptions, customizations for retrying failed request (optional, for defaults click here)
  • debug - boolean, whether to enable debug logging. It is also possible to enable it by setting environment variable DEBUG to true (optional, default false).
  • customBackend - string, option to set URL to a non-official backend (optional)
  • version - number, version of the Blockfrost API (optional, default 0)
  • gotOptions - Additional options to be passed to Got instance. For more details, refer to the Got Options documentation.

Error handling

Blockfrost Node.js SDK throws 2 types of errors, BlockfrostServerError and BlockfrostClientError. Each of these errors is extended from the built-in Error class, allowing you to properly catch it and handle it in your code.

BlockfrostServerError

BlockfrostServerError is an error returned directly by Blockfrost API. The error object's properties are matching the same format as defined by Blockfrost API, with additional url field set to the URL the request was sent to and optional body field set to the original body of the error response in cases where the error was not returned by Blockfrost Backend (eg. nginx).

Example

Blockfrost API returns 404 Not Found for any resource that does not exist on chain at the moment, even when in theory, it could exist. For more detailed explanation check Blockfrost developer portal.

// Example demonstrating catching BlockfrostServerError
try {
  const address = await API.addresses('totallyValidAddress');
} catch (error) {
  if (
    error instanceof Blockfrost.BlockfrostServerError &&
    error.status_code === 404
  ) {
    // address was not used before, but most likely we don't want to throw an error
    console.log("Address is totally empty! But that's ok!");
  } else {
    // rethrow other errors
    throw error;
  }
}

BlockfrostClientError

BlockfrostClientError is an error that was NOT returned by a Blockfrost API server. In this case the request has never reached our backends. Most common causes are network-related.

Shape of BlockfrostClientError object is slightly different from BlockfrostServerError. The error has code and message and optional url property to help you investigate the issue.

Here is a small example showcasing the error format:

{
  "code": "ENOTFOUND",
  "message": "getaddrinfo ENOTFOUND api.blockfrost.io",
  "url": "https://cardano-mainnet.blockfrost.io/api/v0/addresses/addr1qxqs59lph(truncated)"
}

Example

// Example demonstrating catching a network-related client error
try {
  const address = await API.addresses('totallyValidAddress');
} catch (error) {
  if (error instanceof Blockfrost.BlockfrostClientError) {
    console.log('Oops, error during sending the request');
  }
  // Depending on your use case you may want to rethrow the error
  throw error;
}

Examples

For more examples take a look in blockfrost-js-examples repository.

For a list of all SDK methods check out our wiki.

Cardano

const Blockfrost = require('@blockfrost/blockfrost-js');
// import { BlockFrostAPI } from '@blockfrost/blockfrost-js'; // using import syntax

const API = new Blockfrost.BlockFrostAPI({
  projectId: 'YOUR API KEY HERE', // see: https://blockfrost.io
});

async function runExample() {
  try {
    const latestBlock = await API.blocksLatest();
    const networkInfo = await API.network();
    const latestEpoch = await API.epochsLatest();
    const health = await API.health();
    const address = await API.addresses(
      'addr1qxqs59lphg8g6qndelq8xwqn60ag3aeyfcp33c2kdp46a09re5df3pzwwmyq946axfcejy5n4x0y99wqpgtp2gd0k09qsgy6pz',
    );
    const pools = await API.pools({ page: 1, count: 10, order: 'asc' });

    console.log('pools', pools);
    console.log('address', address);
    console.log('networkInfo', networkInfo);
    console.log('latestEpoch', latestEpoch);
    console.log('latestBlock', latestBlock);
    console.log('health', health);
  } catch (err) {
    console.log('error', err);
  }
}

runExample();

IPFS

const Blockfrost = require('@blockfrost/blockfrost-js');
// import { BlockFrostIPFS } from '@blockfrost/blockfrost-js'; // using import syntax

const IPFS = new Blockfrost.BlockFrostIPFS({
  projectId: 'YOUR IPFS KEY HERE', // see: https://blockfrost.io
});

async function runExample() {
  try {
    const added = await IPFS.add(`${__dirname}/img.svg`);
    console.log('added', added);

    const pinned = await IPFS.pin(added.ipfs_hash);
    console.log('pinned', pinned);
  } catch (err) {
    console.log('error', err);
  }
}

runExample();

Utility functions

Blockfrost SDK exports several utility functions to improve developer experience.

Development

Adding new method

  1. Add a code to a corresponding file. e.g. new account method belong to src/endpoints/api/account/ directory.
    • add <method>All method for endpoints with pagination
    • update TSDoc for added method
  2. Add class method to BlockfrostAPI object in src/BlockFrostAPI.ts.
  3. Add unit-test fixture for the added method to test/fixtures/endpoints.ts
  4. Regenerate wiki docs
    • yarn docs
    • push files to Wiki repository https://github.com/blockfrost/blockfrost-js.wiki.git

版本与发布记录

@blockfrost/blockfrost-js 共发布 111 个版本,首次发布于 2021/03/27;过去 12 个月发布了 4 个版本。

版本发布日期距今
6.2.0latest2026/08/212 个月前
6.1.12026/03/097 个月前
6.1.02025/12/0611 个月前
6.1.0-beta.0beta2025/12/0411 个月前
6.0.02025/02/071.7 年前
6.0.0-beta.22025/01/131.8 年前

关于 @blockfrost/blockfrost-js 的常见问题

@blockfrost/blockfrost-js 是什么?

A JavaScript/TypeScript SDK for interacting with the https://blockfrost.io API

如何安装 @blockfrost/blockfrost-js?

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

@blockfrost/blockfrost-js 支持 TypeScript 吗?

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

@blockfrost/blockfrost-js 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@blockfrost/blockfrost-js 需要什么版本的 Node.js?

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

@blockfrost/blockfrost-js 可以商用吗?

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

@blockfrost/blockfrost-js 的下载量有多少?

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

@blockfrost/blockfrost-js 还在维护吗?

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