SDKs overview

Blockfrost Cardano provides a suite of Software Development Kits (SDKs) designed to simplify interaction with its API endpoints for the Cardano blockchain, IPFS, and Arweave. These SDKs abstract the underlying HTTP requests and response parsing, allowing developers to focus on application logic rather than low-level API communication. The official SDKs support several popular programming languages, offering language-specific conventions and data structures for a streamlined development experience. They are maintained by the Blockfrost team and are consistent with the Blockfrost API reference documentation.

The primary benefit of using an SDK is reduced development time and complexity. Instead of manually constructing API requests and handling JSON responses, developers can utilize pre-built functions and objects provided by the SDK. This approach also helps in managing API key authentication and error handling more effectively. For instance, an SDK might automatically include the API key in request headers and provide structured error objects, simplifying debugging. The availability of multiple language SDKs enables developers to integrate Blockfrost services into existing projects without requiring new language adoptions.

In addition to official SDKs, the Blockfrost ecosystem also includes various community-contributed libraries and tools. These community resources often extend functionality, provide alternative implementations, or offer specialized features not present in the official SDKs. Developers should evaluate both official and community options based on their project requirements, language preferences, and the level of support and maintenance available for each library.

Official SDKs by language

Blockfrost provides official SDKs for several programming languages, each designed to offer idiomatic access to the Blockfrost API. These SDKs are maintained by the Blockfrost team and aim to keep pace with API updates and new features. The following table outlines the key details for each official SDK:

Language Package Name Install Command Example Maturity Documentation Link
TypeScript @blockfrost/blockfrost-js npm install @blockfrost/blockfrost-js Stable Blockfrost JavaScript/TypeScript SDK documentation
Python blockfrost-python pip install blockfrost-python Stable Blockfrost Python SDK documentation
Go blockfrost-go go get github.com/blockfrost/blockfrost-go Stable Blockfrost Go SDK documentation
Rust blockfrost-client cargo add blockfrost-client Stable Blockfrost Rust SDK documentation
C# Blockfrost.Api dotnet add package Blockfrost.Api Stable Blockfrost C# SDK documentation
Java blockfrost-java Add dependency to pom.xml or build.gradle Stable Blockfrost Java SDK documentation

Each SDK is designed to provide a native experience for its respective language, adhering to common coding patterns and best practices. For example, the TypeScript SDK leverages Promises for asynchronous operations, while the Python SDK utilizes standard library features for HTTP requests. Developers are encouraged to consult the specific SDK documentation for detailed usage instructions, authentication methods, and example code pertinent to their chosen language.

Installation

Installation of the official Blockfrost SDKs typically follows the standard package management practices for each language. Below are detailed installation steps for the primary languages supported by Blockfrost, along with considerations for setting up your development environment.

TypeScript/JavaScript

For Node.js environments or front-end projects using a bundler, the TypeScript SDK can be installed via npm or yarn:

npm install @blockfrost/blockfrost-js
# or
yarn add @blockfrost/blockfrost-js

After installation, you can import the BlockFrostAPI class into your project. Ensure you have Node.js and npm/yarn installed. The TypeScript SDK is compatible with both CommonJS and ES modules, providing flexibility for different project setups.

Python

The Python SDK is distributed via PyPI and can be installed using pip:

pip install blockfrost-python

It is recommended to use a virtual environment (e.g., venv or conda) to manage project dependencies and avoid conflicts with global Python packages. After installation, you can import the BlockFrostApi class from the blockfrost package.

Go

The Go SDK can be added to your project using the Go module system:

go get github.com/blockfrost/blockfrost-go/blockfrost

Ensure your Go environment is correctly configured, and go mod init has been run in your project directory if it's a new module. The SDK will be available for import as github.com/blockfrost/blockfrost-go/blockfrost.

Rust

To include the Rust SDK in your project, add it as a dependency in your Cargo.toml file:

[dependencies]
blockfrost-client = "0.x.x" # Replace with the latest version

Alternatively, you can use the cargo add command:

cargo add blockfrost-client

Cargo will automatically fetch and build the dependency. You can then use it by importing the necessary modules from blockfrost_client.

C#

The C# SDK is available as a NuGet package:

dotnet add package Blockfrost.Api

This command adds the package reference to your .csproj file. Ensure you have the .NET SDK installed. You can then use the SDK by adding using Blockfrost.Api; statements in your C# code.

Java

For Java projects, you'll typically add the SDK as a dependency in your build tool configuration. For Maven, add the following to your pom.xml:

<dependency>
    <groupId>io.blockfrost</groupId>
    <artifactId>blockfrost-java</artifactId>
    <version>0.x.x</version> <!-- Replace with the latest version -->
</dependency>

For Gradle, add to your build.gradle file:

implementation 'io.blockfrost:blockfrost-java:0.x.x' // Replace with the latest version

After adding the dependency, your build tool will download the necessary JAR files. You can then import classes from the io.blockfrost.sdk package.

Quickstart example

This quickstart example demonstrates how to fetch the latest Cardano block using the Blockfrost TypeScript SDK. This involves initializing the SDK with your API key and making a simple API call. Ensure you have installed the @blockfrost/blockfrost-js package as described in the installation section.

TypeScript Quickstart

First, obtain an API key from your Blockfrost project dashboard. You can use the free tier for initial development, which provides 50,000 requests per day across 5 projects.

import { BlockFrostAPI } from '@blockfrost/blockfrost-js';

const project_id = process.env.BLOCKFROST_API_KEY; // It's best practice to use environment variables

if (!project_id) {
  console.error('BLOCKFROST_API_KEY environment variable is not set.');
  process.exit(1);
}

const API = new BlockFrostAPI({
  projectId: project_id,
});

async function getLatestBlock() {
  try {
    const latestBlock = await API.blocksLatest();
    console.log('Latest Cardano Block:');
    console.log(`Hash: ${latestBlock.hash}`);
    console.log(`Height: ${latestBlock.height}`);
    console.log(`Epoch: ${latestBlock.epoch}`);
    console.log(`Slot: ${latestBlock.slot}`);
  } catch (error) {
    if (error instanceof Error) {
      console.error('Error fetching latest block:', error.message);
    } else {
      console.error('An unknown error occurred:', error);
    }
  }
}

getLatestBlock();

To run this example:

  1. Save the code as getLatestBlock.ts.
  2. Create a .env file in the same directory and add your API key: BLOCKFROST_API_KEY="your_blockfrost_project_id".
  3. Install dotenv for loading environment variables (npm install dotenv).
  4. Update the script to load dotenv at the top: require('dotenv').config();
  5. Compile and run: npx ts-node getLatestBlock.ts (if using ts-node) or compile with tsc and run with node.

This example initializes the BlockFrostAPI client with your project ID and then calls the blocksLatest() method to retrieve the most recent block data from the Cardano blockchain. The retrieved data includes the block hash, height, epoch number, and slot number, which are fundamental identifiers for any block on the chain.

Community libraries

Beyond the officially supported SDKs, the Blockfrost ecosystem benefits from various community-driven libraries and tools. These resources often provide specialized functionalities, alternative language bindings, or integrations with other blockchain tools. While not directly maintained by Blockfrost, they can offer valuable extensions for specific use cases.

  • Cardano serialization libraries: Projects like cardano-serialization-lib (Rust/WASM) are fundamental for building transactions and interacting with the Cardano blockchain at a lower level. While not a Blockfrost SDK directly, it's frequently used in conjunction with Blockfrost to construct and submit complex transactions. Developers might use Blockfrost to query blockchain state and then use serialization libraries to craft a transaction before submitting it back to the network via Blockfrost. More information on this can be found in the Cardano documentation on serialization libraries.
  • Wallet integration tools: Community libraries often emerge to simplify integration with specific Cardano wallets (e.g., Nami, Eternl, Lace). These libraries might provide wrappers around wallet APIs or facilitate the signing of transactions retrieved or constructed using Blockfrost data.
  • Higher-level abstraction layers: Some community projects aim to provide even higher-level abstractions than the official SDKs, simplifying common dApp development patterns. These might include frameworks for building NFT marketplaces or defi protocols that leverage Blockfrost for data access.
  • Language-specific wrappers: Although official SDKs cover many popular languages, community members might create wrappers for less common languages or offer alternative implementations with different design philosophies.

When considering community libraries, it is important to evaluate their maintenance status, community support, and security implications. Always check the project's repository for recent updates, open issues, and contributor activity. The Blockfrost community page and relevant developer forums are good starting points for discovering and assessing these resources.