Astar API Quickstart

How to get started building on Astar and use the JSON-RPC API

Don’t have an API key?

Sign up to start building on Astar. Get started for free

Getting Started Instructions

1. Choose a package manager (npm or yarn)

For this guide, we will be using npm or yarn as our package manager to install Viem or Ethers.js.

npm

To get started with npm, follow the documentation to install Node.js and npm for your operating system: https://docs.npmjs.com/downloading-and-installing-node-js-and-npm

yarn

To get started with yarn, follow these steps: https://classic.yarnpkg.com/lang/en/docs/install

2. Set up your project (npm or yarn)

mkdir alchemy-astar-api
cd alchemy-astar-api
npm init --yes

3. Install Web3 Library

Run the following command to install Viem or Ethers.js with npm or yarn.

npm install viem

4. Make your first request

You are all set now to use Astar API and make your first request. For instance, lets make a request to get latest block. Create an index.js file and paste the following code snippet into the file.

index.js
1import { createPublicClient, http } from 'viem'
2// Note: Astar chain config may need custom setup
3import { mainnet } from 'viem/chains'
4
5const client = createPublicClient({
6 chain: {
7 ...mainnet,
8 id: 592, // Astar network ID
9 name: 'Astar',
10 rpcUrls: {
11 default: { http: ['https://astar-mainnet.g.alchemy.com/v2/demo'] } // Replace 'demo' with your API Key
12 }
13 },
14 transport: http('https://astar-mainnet.g.alchemy.com/v2/demo')
15})
16
17async function main() {
18 const latestBlock = await client.getBlockNumber()
19 console.log('The latest block number is', latestBlock)
20}
21
22main()

5. Run script

To run the above node script, use cmd node index.js, and you should see the output.

shell
The latest block number is 2404244

Astar Tutorials

You must not stop here! Want to build your first Dapp on Astar and use Astar APIs?

Head towards Alchemy Tutorials.

View: /docs

For full documentation on modern Web3 libraries: