Logo
RPC Assistant

Fantom API: Methods, Endpoints, and How to Start Building

摘要

The Fantom API provides JSON-RPC methods for interacting with the Fantom Opera blockchain, covering balances, transactions, event logs, gas estimation, and debugging. This reference covers public endpoints, supported methods, and how to connect reliably for production dApps.

Fantom API Decision Checklist

Before integrating the Fantom API, evaluate these key factors:

CriterionWhat to checkWhy it matters
Endpoint typePublic vs. private vs. dedicatedPublic endpoints are rate-limited and unreliable for production; private or dedicated nodes provide consistent performance.
API coverageEthereum JSON-RPC, Fantom-specific methods, Debug/TraceEnsure the provider supports the methods your dApp needs (e.g., eth_call, ftm_getBalance, debug_traceTransaction).
WebSocket supportReal-time event subscriptionsRequired for order books, transaction monitoring, and live updates.
Archive dataAccess to historical stateNeeded for analytics, historical queries, and certain dApps like explorers.
Rate limitsRequests per second (RPS) allowedLow limits can break your app during traffic spikes.
Fallback & redundancyMultiple endpoints, failoverPrevents downtime if one endpoint goes offline.

What Is the Fantom API?

The Fantom API refers to the set of RPC (Remote Procedure Call) interfaces used to interact with the Fantom Opera blockchain. Because Fantom is Ethereum Virtual Machine (EVM) compatible, its API is largely identical to the standard Ethereum JSON-RPC API, with a few Fantom-specific extensions. Developers use these methods to read blockchain state, send transactions, deploy smart contracts, and subscribe to events.

Fantom also provides a GraphQL API (see fantom-api-graphql on GitHub) for aggregated chain data, but the JSON-RPC API remains the primary interface for direct node interaction.

Fantom API Endpoints

Public Endpoints

Fantom offers free public endpoints for testing and light usage:

  • HTTPS: https://rpc.fantom.network
  • WebSocket: wss://ws.fantom.network

These are shared and rate-limited (typically around 10–100 requests per second). They often fail under heavy load or during network congestion. For production apps, you should use a private endpoint from an RPC provider or run your own node.

Private / Dedicated Endpoints

RPC providers like OnFinality offer dedicated Fantom API endpoints with:

  • Higher or clear rate limits
  • Archive data support
  • WebSocket connections
  • Global load balancing

You can find Fantom among the supported networks on OnFinality's network page.

Fantom API Methods

Fantom supports standard Ethereum JSON-RPC methods plus Fantom-specific ones. Below are the most commonly used categories.

Account & Balance

  • eth_getBalance – Returns the FTM balance of an address.
  • eth_getTransactionCount – Returns the number of transactions sent from an address.
  • ftm_getAccount (Fantom-specific) – Returns full account state including delegations.

Block & Chain Information

  • eth_blockNumber – Latest block number.
  • eth_getBlockByNumber / eth_getBlockByHash – Block details.
  • eth_chainId – Returns chain ID (250 for Fantom mainnet).

Transactions & Execution

  • eth_sendRawTransaction – Submits a signed transaction.
  • eth_getTransactionReceipt – Gets receipt for a transaction.
  • eth_call – Executes a read-only contract call.
  • eth_estimateGas – Estimates gas for a transaction.

Events & Logs

  • eth_getLogs – Retrieves event logs matching a filter.
  • eth_newFilter / eth_getFilterChanges – Polling-based event subscriptions.

Debug & Trace

  • debug_traceTransaction – Full EVM execution trace (if provider supports it).
  • debug_traceBlockByNumber – Traces all transactions in a block.
  • trace_call / trace_replayTransaction (OpenEthereum-style trace) – Available on some providers.

Getting Started with the Fantom API

1. Choose Your Connection Method

You can call the API via HTTPS or WebSocket. Below is a Node.js example using axios.

const axios = require('axios');

const endpoint = 'https://rpc.fantom.network'; // Replace with your private endpoint

async function getBalance(address) {
  const payload = {
    jsonrpc: '2.0',
    method: 'eth_getBalance',
    params: [address, 'latest'],
    id: 1
  };

  try {
    const response = await axios.post(endpoint, payload);
    const balanceWei = response.data.result;
    const balanceFtm = parseInt(balanceWei, 16) / 1e18;
    console.log(`Balance: ${balanceFtm} FTM`);
  } catch (error) {
    console.error('Error:', error);
  }
}

getBalance('0x...');

2. Connect via WebSocket (for real-time events)

const WebSocket = require('ws');

const ws = new WebSocket('wss://ws.fantom.network'); // Replace with your WebSocket endpoint

ws.on('open', function open() {
  ws.send(JSON.stringify({
    jsonrpc: '2.0',
    method: 'eth_subscribe',
    params: ['newHeads'],
    id: 1
  }));
});

ws.on('message', function incoming(data) {
  console.log('New block:', JSON.parse(data).params.result);
});

Fantom API vs. Ethereum API

Fantom is EVM-compatible, so the vast majority of Ethereum methods work unchanged. However, there are a few differences:

  • Gas model: Fantom uses a fixed gas price (usually 1 gwei) and lower gas limits.
  • Finality: Fantom achieves finality in ~1 second, so transaction receipts are available almost immediately.
  • Staking: Fantom-specific methods like ftm_getDelegations and ftm_getRewards are not part of the Ethereum API.
  • Chain ID: 250 (mainnet), 4002 (testnet).

Choosing a Fantom API Provider

Public vs. Private vs. Dedicated

  • Public endpoints – Free but unreliable for production. Use for quick prototyping only.
  • Private shared endpoints – Provided by services like OnFinality as part of an RPC plan. Higher rate limits and better uptime.
  • Dedicated nodes – A full Fantom node provisioned just for your project. Maximum performance and customization.

To evaluate providers, compare the criteria from the checklist above. For production, look for providers that support WebSocket, archive data, and trace methods if needed. OnFinality offers both shared and dedicated Fantom endpoints. Check current pricing and network availability.

Common Issues and Troubleshooting

Rate Limiting

Public endpoints return 429 Too Many Requests when exceeded. Solution: upgrade to a private endpoint or implement retry logic.

WebSocket Disconnections

Public WebSocket endpoints may drop idle connections. Use keep-alive pings or your own WebSocket management.

Missing Methods

Not all providers enable Debug/Trace methods. Verify your provider's supported methods list.

Chain ID Mismatch

Ensure your wallet or dApp is configured to chain ID 250 (Fantom mainnet) and not 4002 (testnet) or another chain.

Key Takeaways

  • The Fantom API is EVM-compatible but includes unique methods for staking and fast finality.
  • Public endpoints are only suitable for testing; use private or dedicated endpoints for production.
  • WebSocket support is essential for real-time applications.
  • Evaluate providers on rate limits, archive data, WebSocket, and Debug/Trace support.

Frequently Asked Questions

What is the Fantom API? The Fantom API is a JSON-RPC interface for interacting with the Fantom Opera blockchain. It includes standard Ethereum methods and Fantom-specific extensions.

Is the Fantom API free? Public endpoints are free but rate-limited. For production, you typically pay for a private endpoint or dedicated node.

Does Fantom support WebSocket? Yes. WebSocket endpoints are available for real-time subscriptions (e.g., eth_subscribe).

What is the chain ID for Fantom mainnet? 250.

Can I use Ethereum libraries (ethers.js, web3.js) with Fantom? Yes. Because Fantom is EVM-compatible, you can use the same libraries after changing the RPC endpoint and chain ID.

How do I get Fantom testnet tokens? The Fantom testnet faucet is available at faucet.fantom.network.

What providers offer Fantom API access? Many RPC providers support Fantom, including OnFinality. Visit OnFinality's network list to see current support.

RPC 知识库

相关 RPC 内容

Network Rpc

什么是Kusama Asset Hub以及如何连接?

# 什么是Kusama Asset Hub以及如何连接? Kusama Asset Hub(原名Statemine)是Kusama网络上的一个公益平行链,支持同质化和非同质化资产的发行与转移。它也是持有和转移KSM代币的低成本链。经过最近的运行时升级,Asset Hub现在支持智能合约,并承载了以前...

Testnet Rpc

Polygon Amoy 测试网:RPC 端点、水龙头和部署的完整开发者指南

# Polygon Amoy 测试网:RPC 端点、水龙头和部署的完整开发者指南 Polygon Amoy 是 Polygon PoS 的官方测试网,于 2024 年 1 月作为 Mumbai 的继任者启动。Amoy 锚定到 Ethereum Sepolia,为开发者提供了一个可持续、面向未来的环境...

Network Rpc

What is an Abstract RPC and how do I start using it?

Abstract is an Ethereum Layer 2 chain that uses zero-knowledge proofs. To connect, you need an Abstract RPC endpoint, chain ID, and the native token a...

Blockchain Infrastructure

什么是TON节点,何时应该运行一个?

TON节点是存储The Open Network区块链完整状态、验证交易并为应用程序提供数据的软件客户端。运行自己的节点可以让你直接、不受限制地访问网络,但也需要大量的运维开销。对于大多数开发者来说,使用托管RPC端点或来自OnFinality等提供商的专用节点,是一种更快、更可靠且无需维护负担的替...

Network Rpc

什么是 Stellar RPC,如何使用它?

# 什么是 Stellar RPC,如何使用它? Stellar RPC(远程过程调用)是与 Stellar 区块链网络交互的标准接口。它允许开发者以编程方式查询账本数据、提交交易和管理账户。Stellar 的 RPC API 与兼容以太坊的 JSON-RPC 不同;它使用基于 RESTful 原则...

Rpc Provider Selection

面向生产工作负载的领先以太坊RPC解决方案有哪些?

# 领先的以太坊RPC解决方案:开发者指南 以太坊仍然是主导的智能合约平台,每天处理超过一百万笔交易,涵盖DeFi、NFT和二层Rollup。对于生产级应用,RPC提供商的选择直接影响交易速度、可靠性和安全性。本指南根据实际工作负载的关键标准评估领先的以太坊RPC解决方案:延迟、正常运行时间、归档访...

永远不用担心基础设施

OnFinality 消除了 DevOps 的繁重工作,让您能够更聪明、更快地构建。

开始