Avalanche C-Chain API Quickstart

Get started building on Avalanche Chain and using the JSON-RPC API

To use the Avalanche C-Chain API you'll need to create a free Alchemy account first!

Introduction

Avalanche is an EVM-compatible blockchain known for its high throughput and low latency. Designed to support the needs of decentralized applications (dApps), Avalanche provides a scalable and efficient environment for deploying Ethereum-based applications with near-instant finality.

📘

NOTE

Avalanche C-Chain is only available on paid tiers (growth, scale, enterprise). If you're on free tier upgrade your account to use Avalanche. If you want us to support Avalanche on free tier, please request here.


What is the Avalanche C-Chain API?

The Avalanche C-Chain API facilitates interaction with the Avalanche network through a collection of JSON-RPC methods. Given its compatibility with the Ethereum ecosystem, developers familiar with Ethereum's JSON-RPC APIs will find working with Avalanche both intuitive and straightforward.

Getting Started Instructions

1. Choose a Package Manager (npm or yarn)

Your first step involves selecting a package manager, which will be crucial for managing your project's dependencies. The choice between npm and yarn depends on your personal preference or project requirements.

npmyarn
Begin with npm by following the npm documentation.For yarn, refer to yarn's installation guide.

2. Set Up Your Project

To kickstart your project, open your terminal and execute the following commands:

mkdir avalanche-api-quickstart
cd avalanche-api-quickstart
npm init --yes
mkdir avalanche-api-quickstart
cd avalanche-api-quickstart
yarn init --yes

This creates a new directory named avalanche-api-quickstart and initializes a Node.js project within it.

3. Make Your First Request

For making API requests, we'll use Axios, a widely-used HTTP client. Install Axios with the following command:

npm install axios
# Or with yarn
# yarn add axios

Next, create an index.js file in your project directory. Paste the following code to send a request to the Avalanche network:

const axios = require('axios');

const url = `https://avax-mainnet.g.alchemy.com/v2/${yourAPIKey}`;

const payload = {
  jsonrpc: '2.0',
  id: 1,
  method: 'eth_blockNumber',
  params: []
};

axios.post(url, payload)
  .then(response => {
    console.log('Block Number:', response.data.result);
  })
  .catch(error => {
    console.error(error);
  });

Remember to replace yourAPIKey with your actual Alchemy API key that you can get from your Alchemy dashboard.

4. Run Your Script

To execute your script and make a request to the Avalanche Chain, run:

node index.js

You should see the current block number on Avalanche Chain (in hexadecimal format) outputted to your console:

Block Number: 0x6d68e

Next Steps

Well done! You've just made your first request to the Avalanche C-Chain API. With this foundation, you can dive deeper into the array of JSON-RPC methods available on Avalanche Chain and start building your dApps on it!

ReadMe