Skip to main content

Introduction

In this tutorial, you’ll learn how to retrieve swap (trade) data and price information for Pump.fun tokens using the Moralis Solana API. This includes getting recent swaps, price history, and OHLC data for any token launched on Pump.fun. We’ll use the following Moralis API endpoint:

Prerequisites

Step 1: Set Up Your Project

Create a new directory for your project and initialize it:
mkdir pump-fun-swaps && cd pump-fun-swaps
npm init -y

Step 2: Create the Script

Create a file called index.js and add the following code:
// index.js
const API_KEY = 'YOUR_API_KEY';
const TOKEN_ADDRESS = 'YOUR_PUMP_FUN_TOKEN_ADDRESS'; // Solana token mint address

async function getTokenSwaps() {
  const response = await fetch(
    `https://solana-gateway.moralis.io/token/mainnet/${TOKEN_ADDRESS}/swaps`,
    {
      headers: {
        'X-API-Key': API_KEY,
      },
    }
  );

  const data = await response.json();
  console.log(JSON.stringify(data, null, 2));
}

getTokenSwaps();
Replace YOUR_API_KEY and YOUR_PUMP_FUN_TOKEN_ADDRESS with your actual values.

Step 3: Run the Script

node index.js

Example Response

{
  "result": [
    {
      "transactionHash": "5abc123...",
      "blockTime": "2024-01-15T10:30:00.000Z",
      "tokenIn": {
        "address": "So11111111111111111111111111111111111111112",
        "symbol": "SOL",
        "amount": "1.5",
        "amountUsd": 150.00
      },
      "tokenOut": {
        "address": "ABC123...",
        "symbol": "PUMP",
        "amount": "1000000",
        "amountUsd": 150.00
      },
      "walletAddress": "7abc...",
      "exchange": "pumpfun",
      "swapType": "buy"
    }
  ]
}

Understanding the Response

FieldDescription
transactionHashSolana transaction signature
blockTimeWhen the swap occurred
tokenInToken being sold (with amount and USD value)
tokenOutToken being bought (with amount and USD value)
walletAddressWallet that made the swap
exchangeDEX where swap occurred
swapTypeType of trade (buy or sell)

Next Steps