December 25, 2024

Avail Node API Guide

Avail Node API allows you to interact with the Avail blockchain. It provides various endpoints for querying blockchain data, sending transactions, and interacting with nodes on the Avail network.


Avail Node API Guide

Avail Node API allows you to interact with the Avail blockchain. It provides various endpoints for querying blockchain data, sending transactions, and interacting with nodes on the Avail network.

This guide will provide step-by-step instructions and code examples on how to use the Avail Node API.

1. Getting Started

1.1 Install Prerequisites

Before interacting with the API, make sure you have the following installed:

  • Node.js for JavaScript-based interactions (optional if you prefer Python or other languages).
  • curl (for testing the API via command-line).
  • Postman or another API testing tool (optional for easier interaction).

1.2 Set Up Connection

The base URL for the Avail Node API is:

https://avail-node-api-url

Replace avail-node-api-url with the actual node URL you're connecting to. This could be a local node or a public one provided by the Avail team.

Example base URL:

https://mainnet.avail.network

2. Making Requests to the API

2.1 Check Node Status

You can check the status of the node to ensure it's running properly. You can send a simple GET request:

curl https://mainnet.avail.network/status

Response:

{
  "status": "ok",
  "node_id": "abcdef123456",
  "syncing": false,
  "blocks_behind": 0
}

This will confirm that the node is healthy, and you’re connected.

2.2 Get Blockchain Information

To retrieve basic blockchain info (e.g., latest block, height), send a GET request to the /blockchain endpoint:

curl https://mainnet.avail.network/blockchain

Response:

{
  "block_height": 1234567,
  "block_hash": "0x123abc...",
  "chain_id": "avail-mainnet",
  "latest_block_time": "2024-12-25T00:00:00Z"
}

This shows you the current height of the blockchain and the latest block.

2.3 Get Transaction Details

To fetch a transaction's details, you can query by the transaction hash:

curl https://mainnet.avail.network/transactions/{transaction_hash}

Replace {transaction_hash} with the actual hash of the transaction you're interested in.

Example:

curl https://mainnet.avail.network/transactions/0x123abc456def...

Response:

{
  "transaction_hash": "0x123abc456def...",
  "block_number": 1234567,
  "status": "successful",
  "from": "0xabcdef...",
  "to": "0x789xyz...",
  "value": "1000000000000000000",
  "gas_used": 21000,
  "timestamp": "2024-12-25T00:00:00Z"
}

This returns details about the transaction, such as the block number, sender, receiver, value transferred, and gas used.

3. Sending Transactions

To send transactions, you'll use the /send_transaction endpoint.

3.1 Prepare Your Transaction

The transaction requires the following fields:

  • from: The address sending the transaction.
  • to: The receiving address.
  • value: The value to send (in Wei).
  • gas_limit: Maximum gas to use.
  • gas_price: The price per unit of gas.
  • data: Optional data field for contract interactions.

3.2 Sending a Transaction

Here’s an example using Node.js with the axios library to send a transaction:

  1. Install axios: npm install axios
  2. Create a script (e.g., sendTransaction.js):
const axios = require('axios');

const sendTransaction = async () => {
  const tx = {
    from: '0xYourSenderAddress',
    to: '0xReceiverAddress',
    value: '1000000000000000000', // 1 AVAIL in Wei
    gas_limit: 21000,
    gas_price: '1000000000', // 1 Gwei
    data: '' // Optional field for smart contract data
  };

  try {
    const response = await axios.post('https://mainnet.avail.network/send_transaction', tx);
    console.log('Transaction sent:', response.data);
  } catch (error) {
    console.error('Error sending transaction:', error.response?.data || error.message);
  }
};

sendTransaction();

This sends a simple value transfer transaction from 0xYourSenderAddress to 0xReceiverAddress. Make sure to replace the placeholder values with actual ones.

3.3 Handling the Response

The response from a successful transaction will typically look like this:

{
  "transaction_hash": "0xabcdef123456...",
  "status": "pending",
  "gas_used": 21000,
  "gas_price": "1000000000",
  "block_number": null,
  "timestamp": "2024-12-25T00:00:00Z"
}

The transaction_hash can be used to track the status of the transaction.

4. Interacting with Smart Contracts

To interact with smart contracts, you would send a transaction with data in the data field. The data is typically encoded based on the contract’s ABI (Application Binary Interface).

Example Request for Contract Interaction

const contractInteractionTx = {
  from: '0xYourSenderAddress',
  to: '0xContractAddress',
  value: '0', // No AVAIL sent
  gas_limit: 50000,
  gas_price: '1000000000',
  data: '0xYourEncodedFunctionCall' // Encoded function call to smart contract
};

You will need to encode the function calls using tools like ethers.js or web3.js.

5. Handling Errors

The API can return errors, such as invalid parameters or insufficient gas. Here's how to handle errors in the response:

Example Error Response:

{
  "error": "Insufficient funds",
  "code": -32000
}

Handling Errors in Code:

try {
  const response = await axios.post('https://mainnet.avail.network/send_transaction', tx);
  console.log('Transaction sent:', response.data);
} catch (error) {
  console.error('Error:', error.response?.data || error.message);
  if (error.response?.data?.error === "Insufficient funds") {
    console.log('Ensure you have enough funds in your wallet');
  }
}

6. Additional Endpoints

6.1 Account Balance

To get the balance of an account:

curl https://mainnet.avail.network/accounts/{account_address}/balance

6.2 Block Information

To get information about a specific block by its height or hash:

curl https://mainnet.avail.network/blocks/{block_hash_or_height}