Tron API Quickstart

Introduction

The Tron API enables developers to interact with the Tron blockchain via a collection of HTTP JSON endpoints. With this API, you can query block and transaction data, interact with smart contracts, manage assets, participate in governance, and more.

Tron leverages a high-throughput, low-latency architecture optimized for dApp performance and decentralized finance. This quickstart guide will walk you through setting up your environment and making your first API call to the Tron network.


What is the Tron Chain API?

The Tron Chain API allows developers to perform queries and send transactions to the Tron blockchain through a HTTP JSON interface. The API supports comprehensive interactions with Tron’s account model, token standards (TRC-10, TRC-20), smart contracts, and node-level information.

Alchemy’s Tron API offers consistent and performant access to:

  • Fetching account and token balances
  • Querying blocks, transactions, contracts, and resources
  • Sending and simulating transactions
  • Managing tokens and interacting with smart contracts

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 tron-api-quickstart
cd tron-api-quickstart
npm init --yes

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

3. Make Your First Request

Install Axios, a popular HTTP client, to make API requests:

bash
$npm install axios
># Or with yarn
># yarn add axios

Create an index.js file in your project directory and paste the following code:

javascript
1const axios = require('axios');
2
3const url = `https://tron-mainnet.g.alchemy.com/v2/${yourAPIKey}/wallet/getnowblock`;
4
5axios.get(url)
6.then(response => {
7const blockNumber = response.data.block_header.raw_data.number;
8console.log('Latest block number:', blockNumber);
9})
10.catch(error => {
11console.error('Error fetching latest block:', error);
12});

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 Tron API, run:

bash
$node index.js

You should see the latest block number from the Tron blockchain printed in your console:

$Latest block number: 53762589

Next Steps

Well done! You’ve just made your first request to the Tron API. With this foundation, you can dive deeper into the array of HTTP JSON endpoints available on Tron API and start building your dApps on it!