In this guide, you’ll learn how to retrieve the total lifetime fees earned by a Solana token using the Bags TypeScript SDK with Node.js. This is useful for tracking token performance and earnings over time.

Prerequisites

Before starting, make sure you have:

1. The Token Lifetime Fees Script

Here is a simple script to fetch token lifetime fees. Save this as get-token-lifetime-fees.ts. This script uses the updated SDK API to retrieve lifetime fees for any token and displays them in a user-friendly format.
import dotenv from "dotenv";
dotenv.config({ quiet: true });

import { BagsSDK } from "@bagsfm/bags-sdk";
import { LAMPORTS_PER_SOL, PublicKey, Connection } from "@solana/web3.js";

// Initialize SDK
const BAGS_API_KEY = process.env.BAGS_API_KEY;
const SOLANA_RPC_URL = process.env.SOLANA_RPC_URL;

if (!BAGS_API_KEY || !SOLANA_RPC_URL) {
    throw new Error("BAGS_API_KEY and SOLANA_RPC_URL are required");
}

const connection = new Connection(SOLANA_RPC_URL);
const sdk = new BagsSDK(BAGS_API_KEY, connection, "processed");

async function getTokenLifetimeFees(tokenMint: string) {
    const feesLamports = await sdk.state.getTokenLifetimeFees(new PublicKey(tokenMint));

    console.log("💰 Token lifetime fees:", (feesLamports / LAMPORTS_PER_SOL).toLocaleString(), "SOL");
}

getTokenLifetimeFees("CyXBDcVQuHyEDbG661Jf3iHqxyd9wNHhE2SiQdNrBAGS");

2. Run Your Script

To analyze a token’s lifetime fees, edit the getTokenLifetimeFees function call at the bottom of the script with the actual mint address you want to analyze. Then, run the script from your terminal:
npx ts-node get-token-lifetime-fees.ts

What You’ll See

The script will output the total lifetime fees earned by the token in SOL, formatted with proper number localization for easy reading. Example output:
💰 Token lifetime fees: 1,234.567890 SOL

Use Cases

This information is valuable for:
  • Token Performance Analysis: Track how much revenue a token has generated
  • Investment Research: Evaluate token success based on fee generation
  • Portfolio Management: Monitor earnings from tokens you’ve created or invested in
  • Market Analysis: Compare performance across different tokens
For more advanced token analytics, check out our Get Token Creators guide.