Summary
The Astar API refers to the JSON-RPC interface for interacting with the Astar network, plus the official token and dApp staking data APIs. This guide explains the different Astar API types, how to connect using RPC endpoints, and how to choose the right infrastructure for production apps.
Quick Recommendation: Which Astar API Should You Use?
Before diving into the details, here is a quick guide to help you decide which Astar API fits your use case:
- For standard blockchain interactions (reading balances, sending transactions, querying blocks): use the Astar JSON-RPC API via an RPC provider. This is the core API for dApps and wallets.
- For token price and supply data: use the Astar Token API (api.astar.network) for ASTR price, market cap, and supply information.
- For dApp staking data: use the dApp Staking API or the GraphQL indexer to query staking statistics, rewards, and staker information.
- For high-demand production workloads: consider a dedicated node or a managed RPC service that offers reliability and scalability, as the public token API has usage restrictions.
If you are building a production application, you need an RPC provider that offers consistent performance, WebSocket support, and archive data. OnFinality provides Astar RPC endpoints and dedicated nodes that can handle your workload.
What Is the Astar API?
The Astar API is a set of interfaces that allow developers to interact with the Astar network. It includes:
- JSON-RPC API: Ethereum-compatible methods for reading and writing blockchain data, such as
eth_blockNumber,eth_getBalance, andeth_sendRawTransaction. - Token API: RESTful endpoints for token statistics, such as ASTR price, market cap, and supply.
- dApp Staking API: RESTful endpoints for querying dApp staking data, such as TVL, stakers, and rewards.
- GraphQL Indexer: A GraphQL endpoint for advanced queries on dApp staking data.
Astar is a multi-chain smart contract platform that supports both EVM and WebAssembly (Wasm) smart contracts. As a Polkadot parachain, it offers cross-chain interoperability. The JSON-RPC API is familiar to Ethereum developers, making it easy to migrate existing dApps.
Astar JSON-RPC API: Connecting to the Network
To interact with the Astar network, you need an RPC endpoint. You can use a public endpoint, a managed provider, or your own node. Here is an example using the OnFinality public endpoint:
curl https://astar.api.onfinality.io/public \
-X POST \
-H "Content-Type: application/json" \
--data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}'
This request returns the latest block number. You can use the same endpoint with libraries like ethers.js or viem:
import { createPublicClient, http } from 'viem';
const client = createPublicClient({
chain: astar,
transport: http('https://astar.api.onfinality.io/public')
});
const blockNumber = await client.getBlockNumber();
console.log('Current block number:', blockNumber);
For WebSocket support, you can use wss://astar.api.onfinality.io/public/ws for real-time subscriptions.
Astar Token API: Getting Price and Supply Data
The Astar Token API provides token statistics for ASTR. It is available at https://api.astar.network/api/v3/astar/token. The API is documented with Swagger UI at https://api.astar.network/.
Example request to get ASTR token stats:
curl https://api.astar.network/api/v3/astar/token
This returns JSON with fields like price, market_cap, circulating_supply, and total_supply. Note that this API is a complimentary service with restricted usage and may not be suitable for high-demand scenarios. For production, consider hosting your own instance or using a dedicated data service.
dApp Staking API and Indexer
The dApp Staking API allows you to query staking data on Astar. Endpoints include:
/api/v3/{network}/dapps-staking/chaindapps- list of registered dApps/api/v3/{network}/dapps-staking/tvl/{period}- TVL for a given period/api/v3/{network}/dapps-staking/stakerslist/{contractAddress}- list of stakers for a dApp/api/v3/{network}/dapps-staking/rewards/{period}- reward events
For more complex queries, the GraphQL indexer is available at https://squid.subsquid.io/dapps-staking-indexer-astar/graphql. This allows you to query staking data with GraphQL.
Choosing an RPC Provider for Astar
When selecting an RPC provider for Astar, consider the following factors:
| Factor | What to Check | Why It Matters |
|---|---|---|
| Reliability | Uptime history, redundancy, failover | Downtime can break your dApp |
| Performance | Latency, throughput, rate limits | Slow responses degrade user experience |
| Data Availability | Archive data, trace support | Needed for indexing and analytics |
| WebSocket Support | Real-time subscriptions | Essential for live updates |
| Pricing | Free tier, pay-as-you-go, dedicated plans | Must fit your budget and scale |
| Support | Documentation, SLAs, community | Helps resolve issues quickly |
OnFinality offers Astar RPC endpoints with public and private options, as well as dedicated nodes for high-throughput workloads. You can compare RPC pricing to find a plan that suits your needs.
Common Pitfalls and Troubleshooting
- Rate limiting: Public endpoints often have rate limits. If you hit limits, consider a paid plan or dedicated node.
- WebSocket disconnections: Ensure your client handles reconnections gracefully.
- Incorrect chain ID: Astar mainnet chain ID is 592. Make sure your wallet or dApp uses the correct ID.
- Data consistency: When using archive data, verify that the provider supports the required block range.
Key Takeaways
- The Astar API includes JSON-RPC, token, and dApp staking endpoints.
- For production apps, choose a reliable RPC provider that offers performance and support.
- Public APIs have usage restrictions; consider dedicated infrastructure for high-demand scenarios.
- OnFinality provides Astar RPC endpoints and dedicated nodes to support your development.
Frequently Asked Questions
What is the Astar API?
The Astar API is a set of interfaces for interacting with the Astar network, including JSON-RPC methods for blockchain data, token statistics, and dApp staking data.
How do I get an Astar RPC endpoint?
You can use a public endpoint like https://astar.api.onfinality.io/public or sign up for a managed provider like OnFinality to get a private endpoint.
Is the Astar Token API free?
Yes, the Astar Token API is free but has usage restrictions. For high-demand applications, consider hosting your own instance or using a dedicated data service.
Can I use WebSocket with Astar?
Yes, many RPC providers offer WebSocket endpoints for real-time subscriptions. OnFinality provides WebSocket support for Astar.
What is the Astar chain ID?
The Astar mainnet chain ID is 592. For testnets, use the appropriate ID.
How do I choose between public and dedicated RPC?
Public endpoints are fine for development and light usage. For production apps with high traffic, dedicated nodes offer better performance and reliability. Compare RPC pricing to decide.
For more information, visit the Astar network page and explore the supported networks.