Skip to content

helius-labs/helius-sdk

Repository files navigation

Helius Node.js SDK

Version Downloads

The Helius Node.js library provides access to the Helius API from JavaScript/TypeScript.

Documentation

API reference documentation is available at docs.helius.dev.

Installation

Using npm:

npm install helius-sdk

Using yarn:

yarn add helius-sdk

Usage

The package needs to be configured with your account's API key, which is available in the Helius Dashboard.

import { Helius } from "helius-sdk";

// Replace YOUR_API_KEY with the API key from your Helius dashboard
const helius = new Helius("YOUR_API_KEY"); 

const getAssetsByOwner = async () => {
  const response = await helius.rpc.getAssetsByOwner({
    ownerAddress: "86xCnPeV69n6t3DnyGvkKobf9FdN2H9oiVDdaMpo2MMY",
    page: 1,
  });
  console.log(response.items);
}

getAssetsByOwner();

Try it out

Handling errors

When the API returns a non-success status code (4xx or 5xx response), an error message will be thrown:

try {
  const response = await helius.rpc.getAssetsByOwner({
    ownerAddress: "86xCnPeV69n6t3DnyGvkKobf9FdN2H9oiVDdaMpo2MMY",
    page: 1,
  });
  console.log(response.items);
} catch (error) {
  console.log(error);
}

Common Error Codes

When working with the Helius SDK, you may encounter several error codes. Below is a table detailing some of the common error codes along with additional information to help you troubleshoot:

Error Code Error Message More Information
401 Unauthorized This occurs when an invalid API key is provided or access is restricted due to RPC rules.
429 Too Many Requests This indicates that the user has exceeded the request limit in a given timeframe or is out of credits.
5XX Internal Server Error This is a generic error message for server-side issues. Please contact Helius support for assistance.

If you encounter any of these errors, refer to the Helius documentation for further guidance, or reach out to the Helius support team for more detailed assistance.

Using the Helius SDK

Our SDK is designed to give you a seamless experience when building on Solana. We've separated the core functionality into various segments.

DAS API

Comprehensive and performant API for tokens, NFTs, and compressed NFTs on Solana.

  • getAsset(): Get an asset by its ID.
  • getAssetBatch(): Get multiple assets by ID (up to 1k).
  • getSignaturesForAsset(): Get a list of transaction signatures related to a compressed asset.
  • searchAssets(): Search for assets by a variety of parameters. Very useful for token-gating!
  • getAssetProof(): Get a Merkle proof for a compressed asset by its ID.
  • getAssetsByOwner(): Get a list of assets owned by an address. This is the fastest way to get all the NFTs and fungible tokens that are owned by a wallet on Solana.
  • getAssetsByGroup(): Get a list of assets by a group key and value. This endpoint is very useful for getting the mint list for NFT Collections.
  • getAssetsByCreator(): Get a list of assets created by an address.
  • getAssetsByAuthority(): Get a list of assets with a specific authority.
  • getTokenAccounts(): Get information about all token accounts for a specific mint or a specific owner.
  • getNftEditions(): Get information about all the edition NFTs for a specific master NFT

Mint API

The easiest way to mint compressed NFTs at scale.

Webhooks

Provides methods for setting up, editing, and managing webhooks, crucial for listening to on-chain Solana events (e.g., sales, listings, swaps) and triggering actions when these events happen.

Smart Transactions

Jito Smart Transactions and Helper Methods

Helper methods

Offers additional tools for various Solana-related tasks like analyzing blockchain throughput and tracking stake accounts and SPL token holders.

  • getCurrentTPS(): Returns the current transactions per second (TPS) rate — including voting transactions.
  • airdrop(): Request an allocation of lamports to the specified address
  • getStakeAccounts(): Returns all the stake accounts for a given public key.
  • getTokenHolders(): Returns all the token accounts for a given mint address (ONLY FOR SPL TOKENS).
  • getPriorityFeeEstimate(): Returns an estimated priority fee based on a set of predefined priority levels (percentiles).
  • sendTransaction(): Wrapper for sendTransaction RPC call that includes support for validatorAcls parameter.

DAS API (Digital Asset Standard)

Read more about the DAS API from our docs, here.

Namespace: helius.rpc

getAsset()

Get an asset by its ID.

import { Helius } from "helius-sdk";

const helius = new Helius("YOUR_API_KEY");
const response = await helius.rpc.getAsset({
  id: "F9Lw3ki3hJ7PF9HQXsBzoY8GyE6sPoEZZdXJBsTTD2rk",
  displayOptions: {
    showCollectionMetadata: true,
  },
});
console.log(response.grouping?.map((g) => g.collection_metadata?.name));

getAssetBatch()

Get multiple assets by ID (up to 1k).

import { Helius } from "helius-sdk";

const ids = [
  "F9Lw3ki3hJ7PF9HQXsBzoY8GyE6sPoEZZdXJBsTTD2rk",
  "F9Lw3ki3hJ7PF9HQXsBzoY8GyE6sPoEZZdXJBsTTD2rk",
];
const helius = new Helius("your-api-key");
const response = await helius.rpc.getAssetBatch({
  ids: ids,
});
console.log(response.map((x) => x.id));

getSignaturesForAsset()

Get a list of transaction signatures related to a compressed asset.

import { Helius } from "helius-sdk";

const helius = new Helius("YOUR_API_KEY");
const response = await helius.rpc.getSignaturesForAsset({
  id: "Bu1DEKeawy7txbnCEJE4BU3BKLXaNAKCYcHR4XhndGss",
  page: 1,
});
console.log(response.items);

searchAssets()

Search for assets by a variety of parameters. Very useful for token-gating!

import { Helius } from "helius-sdk";

const helius = new Helius("YOUR_API_KEY");
const response = await helius.rpc.searchAssets({
  ownerAddress: "2k5AXX4guW9XwRQ1AKCpAuUqgWDpQpwFfpVFh3hnm2Ha",
  compressed: true,
  page: 1,
});
console.log(response.items);

getAssetProof()

Get a merkle proof for a compressed asset by its ID.

import { Helius } from "helius-sdk";

const helius = new Helius("YOUR_API_KEY");
const response = await helius.rpc.getAssetProof({
  id: "Bu1DEKeawy7txbnCEJE4BU3BKLXaNAKCYcHR4XhndGss",
});
console.log(response);

getAssetsByOwner()

Get a list of assets owned by an address. This is the fastest way to get all the NFTs owned by a wallet on Solana.

import { Helius } from "helius-sdk";

const helius = new Helius("YOUR_API_KEY");
const response = await helius.rpc.getAssetsByOwner({
  ownerAddress: "86xCnPeV69n6t3DnyGvkKobf9FdN2H9oiVDdaMpo2MMY",
  page: 1,
});
console.log(response.items);

getAssetsByGroup()

Get a list of assets by a group key and value. This endpoint is very useful for getting the mint list for NFT Collections.

import { Helius } from "helius-sdk";

const helius = new Helius("YOUR_API_KEY");
const response = await helius.rpc.getAssetsByGroup({
  groupKey: "collection",
  groupValue: "J1S9H3QjnRtBbbuD4HjPV6RpRhwuk4zKbxsnCHuTgh9w",
  page: 1,
});
console.log(response.items);

getAssetsByCreator()

Get a list of assets created by an address.

import { Helius } from "helius-sdk";

const helius = new Helius("YOUR_API_KEY");
const response = await helius.rpc.getAssetsByCreator({
  creatorAddress: "D3XrkNZz6wx6cofot7Zohsf2KSsu2ArngNk8VqU9cTY3",
  onlyVerified: true,
  page: 1,
});
console.log(response.items);

getAssetsByAuthority()

Get a list of assets with a specific authority.

import { Helius } from "helius-sdk";

const helius = new Helius("YOUR_API_KEY");
const response = await helius.rpc.getAssetsByAuthority({
  authorityAddress: "2RtGg6fsFiiF1EQzHqbd66AhW7R5bWeQGpTbv2UMkCdW",
  page: 1,
});
console.log(response.items);

getTokenAccounts()

Get information about all token accounts for a specific mint or a specific owner.

const response = await helius.rpc.getTokenAccounts({
  page: 1,
  limit: 100,
  options: {
    showZeroBalance: false,
  },
  owner: "CckxW6C1CjsxYcXSiDbk7NYfPLhfqAm3kSB5LEZunnSE"
});

console.log(response);

getNftEditions()

Get information about all the edition NFTs for a specific master NFT.

import { Helius } from "helius-sdk";

const helius = new Helius("YOUR_API_KEY");
const response = await helius.rpc.getNftEditions({
  mint: "Ey2Qb8kLctbchQsMnhZs5DjY32To2QtPuXNwWvk4NosL",
  page: 1,
  limit: 2,
});

console.log(response);

Mint

To read more about the easiest way to mint cNFTs on Solana, see our docs.

mintCompressedNft()

To mint a compressed NFT, simply call the mintCompressedNft method and pass in your NFT data. This is what the mint will look like in the explorer.

import { Helius } from "helius-sdk";

const helius = new Helius("YOUR_API_KEY");
const response = await helius.mintCompressedNft({
  name: "Exodia the Forbidden One",
  symbol: "ETFO",
  owner: "OWNER_WALLET_ADDRESS",
  description:
    "Exodia the Forbidden One is a powerful, legendary creature composed of five parts: the Right Leg, Left Leg, Right Arm, Left Arm, and the Head. When all five parts are assembled, Exodia becomes an unstoppable force.",
  attributes: [
    {
      trait_type: "Type",
      value: "Legendary",
    },
    {
      trait_type: "Power",
      value: "Infinite",
    },
    {
      trait_type: "Element",
      value: "Dark",
    },
    {
      trait_type: "Rarity",
      value: "Mythical",
    },
  ],
  imageUrl:
    "https://cdna.artstation.com/p/assets/images/images/052/118/830/large/julie-almoneda-03.jpg?1658992401",
  externalUrl: "https://www.yugioh-card.com/en/",
  sellerFeeBasisPoints: 6900,
});
console.log(response.result);

If you want the SDK to handle the image upload, you can do so by passing in the imagePath and walletPrivatekey fields. imagePath is the relative path to the image, and walletPrivatekey is the private key of the wallet that'll pay for the image upload to Arweave.

import { Helius } from "helius-sdk";

const helius = new Helius("YOUR_API_KEY");
const response = await helius.mintCompressedNft({
  name: "Aggron",
  symbol: "AGNFT",
  owner: "OWNER_WALLET_ADDRESS",
  description:
    "Aggron is a powerful Steel/Rock-type Pokémon known for its iron defense.",
  attributes: [
    {
      trait_type: "Type",
      value: "Steel/Rock",
    },
    {
      trait_type: "Power",
      value: "High",
    },
  ],
  externalUrl: "https://www.pokemon.com/us/pokedex/aggron",
  imagePath: "../images/aagron.png",
  walletPrivatekey: "YOUR_WALLET_PRIVATE_KEY",
});

delegateCollectionAuthority() and revokeCollectionAuthority()

If you want to mint your cNFT to a collection:

  • Delegate Helius as a collection authority (using DelegateCollectionAuthority method), so that Helius can mint to that collection on your behalf.
  • Pass in the collection mint address in the collection field.
  • (Optional) Revoke collection authority (using RevokeCollectionAuthority method).
import { Helius } from "helius-sdk";

const helius = new Helius("YOUR_API_KEY");

// 1. Delegate Helius as a collection authority
await helius.delegateCollectionAuthority({
  collectionMint: "COLLECTION_MINT_ADDRESS",
  updateAuthorityKeypair: updateAuthorityKeypair,
});

// 2. Mint your cNFT to the collection
const response = await helius.mintCompressedNft({
  name: "Feitan Portor",
  symbol: "FEITAN",
  owner: "OWNER_WALLET_ADDRESS",
  collection: "COLLECTION_MINT_ADDRESS",
  description: "Feitan Portor is a member of the notorious Phantom Troupe.",
  attributes: [
    {
      trait_type: "Affiliation",
      value: "Phantom Troupe",
    },
    {
      trait_type: "Nen Ability",
      value: "Pain Packer",
    },
  ],
  externalUrl: "https://hunterxhunter.fandom.com/wiki/Feitan_Portor",
  imagePath: "../images/feitan.png",
  walletPrivatekey: "YOUR_WALLET_PRIVATE_KEY",
});

// 3. Revoke collection authority (optional)
await helius.revokeCollectionAuthority({
  collectionMint: "COLLECTION_MINT_ADDRESS",
  revokeAuthorityKeypair: revokeAuthorityKeypair,
});

getMintlist()

To get all the tokens for an NFT collection:

import {
  Helius
  Collections,
} from "helius-sdk";

const helius = new Helius("YOUR_API_KEY");

const mints = helius.getMintlist({
  query: Collections.ABC,
});

Webhooks

Helius webhooks are the perfect way of building event-driven systems on Solana.

Simply select the transaction type(s) to detect, the accounts to watch, and the webhook handler.

We've parsed over 100 transaction types (including NFT Sales, NFT Listings, and more) from over 50 different sources so you can get building ASAP.

If you don't want Helius' unique abstractions and would rather work with Solana's native data types, set webhookType to "raw".

For a quick demo video, please see the Webhook docs.

createWebhook()

Note: You can use enum WebhookType to specify between raw, discord, or enhanced webhooks! The default type is "enhanced".

import {
  // enums
  Address,
  TransactionType,

  // lib
  Helius,
} from "helius-sdk";

const helius = new Helius("YOUR_API_KEY");

helius.createWebhook({
  accountAddresses: [Address.MAGIC_EDEN_V2],
  transactionTypes: [TransactionType.NFT_LISTING],
  webhookURL: "my-webhook-handler.com/handle",
});

If you'd like to work with the native Solana transaction format instead of Helius' abstraction, use the "raw" type instead (this will also have lower latency). Note we also add an auth-header for security purposes.

import {
  // enums
  TransactionType,
  WebhookType,
  Address,
  Helius,
} from "helius-sdk";

const helius = new Helius("YOUR_API_KEY");

helius.createWebhook({
  accountAddresses: [Address.MAGIC_EDEN_V2],
  authHeader: "some auth header",
  webhookURL: "my-webhook-handler.com/handle",
  webhookType: WebhookType.RAW,
  transactionTypes: [TransactionType.ANY],
});

For Discord webhooks, simply use enum WebhookType.DISCORD.

editWebhook()

You can also edit your webhooks. A common use case is dynamically adding/removing accounts to watch in a webhook:

import { Helius, Address } from "helius-sdk";

const helius = new Helius("YOUR_API_KEY");

helius.editWebhook(
  "your-webhook-id-here",
  { accountAddresses: [Address.SQUADS] } // This will ONLY update accountAddresses, not the other fields on the webhook object
);

Very important: The editWebhook method will completely overwrite the existing values for the field(s) that you inputted. Make sure to fetch the existing webhook and merge the values to avoid this.

appendAddressesToWebhook()

For convenience, we've added a method to let you simply append new addresses to an existing webhook:

import { Helius, Address } from "helius-sdk";

const helius = new Helius("YOUR_API_KEY");

helius.appendAddressesToWebhook("your-webhook-id-here", [
  Address.SQUADS,
  Address.JUPITER_V3,
]);

removeAddressesFromWebhook()

For convenience, we've added a method to let you simply remove addresses from an existing webhook:

import { Helius, Address } from "helius-sdk";

const helius = new Helius("YOUR_API_KEY");

helius.removeAddressesFromWebhook("your-webhook-id-here", [
  Address.SQUADS,
  Address.JUPITER_V3,
]);

getAllWebhooks()

import { Helius } from "helius-sdk";

const helius = new Helius("YOUR_API_KEY");

helius.getAllWebhooks();

getWebhookByID()

import { Helius } from "helius-sdk";

const helius = new Helius("YOUR_API_KEY");

helius.getWebhookByID("<webhook-id-here>");

deleteWebhook()

import { Helius } from "helius-sdk";

const helius = new Helius("YOUR_API_KEY");

helius.deleteWebhook("<webhook-id-here>"); // returns a boolean

createCollectionWebhook()

import {
  // collections dict
  Collections,
  Helius,
} from "helius-sdk";

const helius = new Helius("YOUR_API_KEY");

helius.createCollectionWebhook({
  collectionQuery: Collections.ABC,
  transactionTypes: [Types.TransactionType.ANY],
  webhookType: Types.WebhookType.DISCORD,
  webhookURL: "https://discord.com/api/webhooks/your-discord-token-here",
});

Note that the Collections.ABC enum references the collection query for this collection. It is just a convenience enum so that developers don't have to figure out whether to use firstVerifiedCreator or the Metaplex Certified Collection address. If you already know it for your collection, please make a PR.

Helper methods

We provide a variety of helper methods to help make Solana RPCs easier to work with.

getCurrentTPS()

import { Helius } from "helius-sdk";

const helius = new Helius("YOUR_API_KEY");

const tps = await helius.rpc.getCurrentTPS();

airdrop()

import { Helius } from "helius-sdk";

const helius = new Helius("YOUR_API_KEY");

const response = await helius.rpc.airdrop(
  new PublicKey("<wallet address>"),
  1000000000
); // 1 sol

getStakeAccounts()

import { Helius } from "helius-sdk";

const helius = new Helius("YOUR_API_KEY");

const response = await helius.rpc.getStakeAccounts("<wallet address>");

getTokenHolders()

import { Helius } from "helius-sdk";

const helius = new Helius("YOUR_API_KEY");

const response = await helius.rpc.getTokenHolders("<token mint address>");

getPriorityFeeEstimate()

This method considers both global and local fee markets in the estimation. Users can also specify to receive all the priority levels and adjust the window with which these levels are calculated via lookbackSlots

import { Helius } from "helius-sdk";
const helius = new Helius("YOUR_API_KEY");

const response = await helius.rpc.getPriorityFeeEstimate({
  accountKeys: ["JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4"],
  options: {
    includeAllPriorityFeeLevels: true,
  }
});

console.log(response);

sendSmartTransaction()

This method builds and sends an optimized transaction, while handling its confirmation status. Whether the transaction skips preflight checks and how many times it is retried is configurable by the user. The following code snippet is an example of sending 0.5 SOL to a given public key, with an optimize transaction that skips preflight checks:

import { Helius } from "helius-sdk";
import {
  Keypair,
  SystemProgram,
  LAMPORTS_PER_SOL,
  TransactionInstruction,
} from "@solana/web3.js";

const helius = new Helius("YOUR_API_KEY");

const fromKeypair = /* Your keypair goes here */;
const fromPubkey = fromKeypair.publicKey;
const toPubkey = /* The person we're sending 0.5 SOL to */;

const instructions: TransactionInstruction[] = [
  SystemProgram.transfer({
    fromPubkey: fromPubkey,
    toPubkey: toPubkey,
    lamports: 0.5 * LAMPORTS_PER_SOL, 
  }),
];

const transactionSignature = await helius.rpc.sendSmartTransaction(instructions, [fromKeypair], [], { skipPreflight: true });
console.log(`Successful transfer: ${transactionSignature}`);

createSmartTransaction()

The smart transaction creation functionality has been abstracted out of sendSmartTransaction and is available with the createSmartTransaction() method. It takes in an array of instructions, signers, lookup tables, and an optional fee payer. It returns an object containing the smart transaction (i.e., Transaction | VersionedTransaction) as well as the lastValidBlockHeight:

const { smartTransaction: transaction, lastValidBlockHeight } = await helius.rpc.createSmartTransaction(instructions, signers, lookupTables, feePayer);

getComputeUnits()

This method simulates a transaction to get the total compute units consumed. It takes in an array of instructions, a fee payer, an array of lookup tables, and an array of signers. It returns the compute units consumed, or null if unsuccessful:

const units = helius.rpc.getComputeUnits(instructions, payerKey, [], []);

pollTransactionConfirmation()

This method polls a transaction to check whether it has been confirmed. It takes in a TransactionSignature and checks whether it has been confirmed within the timeout period. Currently, this method defaults to a 15 second timeout and a 5 second retry interval, so it polls 3 times over 15 seconds. However, with PollTransactionOptions, these values can be changed in addition to the confirmation status. It returns the confirmed transaction signature or an error if the confirmation times out:

let txSig = await helius.connection().sendRawTransaction(transaction.serialize(), {
  skipPreflight: true,
  ...sendOptions
});

return await helius.rpc.pollTransactionConfirmation(txSig);

sendSmartTransactionWithTip()

This method has the same functionality as sendSmartTransaction. However, it sends the optimized transaction as a bundle and includes a tip so it is processed by Jito's Block Engine. The following code snippet sends 0.05 SOL to a given public key with a Jito tip of 100k lamports using Jito's New York API URL:

import { Helius } from "helius-sdk";
import {
  Keypair,
  SystemProgram,
  LAMPORTS_PER_SOL,
  TransactionInstruction,
} from "@solana/web3.js";

const helius = new Helius("YOUR_API_KEY");

const fromKeypair = /* Your keypair goes here */;
const fromPubkey = fromKeypair.publicKey;
const toPubkey = /* The person we're sending 0.05 SOL to */;

const instructions: TransactionInstruction[] = [
  SystemProgram.transfer({
    fromPubkey: fromPubkey,
    toPubkey: toPubkey,
    lamports: 0.05 * LAMPORTS_PER_SOL, 
  }),
];

// Call the sendSmartTransactionWithTip function
const bundleId = await helius.rpc.sendSmartTransactionWithTip(instructions, [keypair], address_lut, 100000, "NY");
console.log(`Bundle sent successfully with ID: ${bundleId}`);

createSmartTransactionWithTip()

Similarly to createSmartTransaction, the smart transaction creation functionality for smart transactions with tips has been abstracted out of sendSmartTransactionWithTip and is available with the createSmartTransactionWithTip method. It takes in an array of instructions, signers, lookup tables, as well as a tip amount and an optional fee payer. The tip amount defaults to 1000 lamports — the minimum specified in Jito's documentation. It returns the serialized transaction as a string, and the lastValidBlockHeight. The reason we return the transaction as a string is because the sendJitoBundle method requires a serialzied transaction:

const { serializedTransaction, lastValidBlockHeight } = await this.createSmartTransactionWithTip(instructions, signers, lookupTables, tipAmount, feePayer);

addTipInstruction()

This method adds a tip instruction to the last instruction in the set of provided instructions. It is a transfer instruction that sends the specified amount of lamports from the fee payer to the designated tip account.

const randomTipAccount = JITO_TIP_ACCOUNTS[Math.floor(Math.random() * JITO_TIP_ACCOUNTS.length)];
const tipAmount = 10000;

helius.rpc.addTipInstruction(instructions, feePayer, randomTipAccount, tipAmount);

sendJitoBundle()

This method sends a bundle of transactions to the Jito Block Engine. It takes in an array of serialized transactions and a Jito Block Engine API URL. It returns the bundle ID as a string.

const jitoApiUrl = 'https://mainnet.block-engine.jito.wtf/api/v1/bundles';
const bundleId = helius.rpc.sendJitoBundle([serializedTransactions], jitoApiUrl);

getBundleStatuses()

This method gets the status of Jito bundles. It takes in an array of bundle IDs and a Jito Block Engine API URL. It returns the status of the bundles.

const bundleIds = [ /* Bundle IDs */];
const jitoApiUrl = 'https://mainnet.block-engine.jito.wtf/api/v1/bundles';
const statuses = helius.rpc.getBundleStatuses(bundleIds, jitoApiUrl);

sendTransaction()

This method behaves the same way as the standard sendTransaction, but adds support for validatorAcls JSON-based allow/denylists.

try {
    const response = await helius.rpc.sendTransaction(
        transaction,
        {
            validatorAcls: [SFDP_REJECTS_URL],
            skipPreflight: true,
        },
    );
} catch (error) {
    console.error(error);
}

helius.connection

Warning

This uses Solana-Web3.js version 1.73.2

Incorporates all commonly-used methods from Solana-Web3.js Provider using your Helius RPC. Ideal for managing connections and performing standard Solana blockchain operations. For a list of all the Methods see: https://docs.solana.com/api/http

import { Helius } from "helius-sdk";

// Replace YOUR_API_KEY with the API key from your Helius dashboard
const helius = new Helius("YOUR_API_KEY"); 

const getBlockHeight = async () => {
  const response = await helius.connection.getBlockHeight();
  console.log(response);
}

getBlockHeight();

FAQ

Q: I get an error stating Cannot use import statement outside a module
A: In the package.json file add "type": "module". Adding this enables ES6 modules.