This guide demonstrates how to integrate with Pump.fun's API using Solana Web3.js 2.0 and Metis to automate token trading. Learn to fetch quotes, execute swaps, and manage transactions programmatically through a detailed TypeScript implementation.
Key Features
- Seamless API Integration: Connect directly with Pump.fun's trading platform
- Web3.js 2.0 Implementation: Leverage the latest Solana SDK features
- End-to-End Automation: From quote fetching to transaction confirmation
- TypeScript Best Practices: Type-safe code with clear interfaces
Prerequisites
- QuickNode account with Metis Jupiter Swap API enabled
- Node.js v21+ and TypeScript experience
- Basic Solana development knowledge
- Solana wallet with test funds
Implementation Steps
1. Project Setup
npm init -y
npm install @solana/web3.js@2 dotenv
npm install --save-dev typescript ts-node @types/node2. Core Functionality
API Configuration
const createPumpFunTransport = (metisEndpoint: string): RpcTransport => {
return async <TResponse>(...args) => {
const { method, params } = args[0].payload;
const url = new URL(`${metisEndpoint}/${METHOD_MAP[method].path}`);
return METHOD_MAP[method].method === 'GET'
? handleGET<TResponse>(url, params)
: handlePOST<TResponse>(url, params);
};
};Transaction Handling
async function signAndSendTransaction(
txBase64: string,
signerKey: number[]
): Promise<string> {
const keypair = await createKeyPairFromBytes(new Uint8Array(signerKey));
const txBytes = getBase64Encoder().encode(txBase64);
const signedTx = await signTransaction([keypair], getTransactionDecoder().decode(txBytes));
const { lastValidBlockHeight, blockhash } = await rpc.getLatestBlockhash().send();
const txWithLifetime = { ...signedTx, lifetimeConstraint: { blockhash, lastValidBlockHeight } };
return sendAndConfirmTransactionFactory({ rpc, rpcSubscriptions })(txWithLifetime);
}3. Trading Workflow
Fetching Quotes
const quote = await pumpFunApi.pumpfun_quote({
type: 'BUY',
mint: targetAddress,
amount: 1_000_000 // 0.001 SOL
}).send();Executing Swaps
const swap = await pumpFunApi.pumpfun_swap({
wallet: signerAddress,
type: 'BUY',
mint: targetAddress,
inAmount: 1_000_000,
priorityFeeLevel: 'high'
}).send();
const txSignature = await signAndSendTransaction(swap.tx, walletSecret);Best Practices
- Error Handling: Implement robust try-catch blocks for API calls
- Rate Limiting: Respect API rate limits (3-5 requests/second)
- Security: Never expose private keys in client-side code
- Testing: Always test with small amounts first
👉 Explore more Solana trading strategies
FAQ
What is Pump.fun?
A permissionless token creation and trading platform on Solana using fair launch principles.
Why use Web3.js 2.0?
The new version offers improved type safety, modular architecture, and better performance.
Can I use this on mainnet?
Yes, but start with small test transactions and ensure proper error handling.
How do I get a Metis API key?
Sign up at QuickNode and enable the Metis add-on.
👉 Learn advanced Solana trading techniques
Conclusion
This implementation provides a foundation for building sophisticated trading tools on Pump.fun. Key takeaways:
- Web3.js 2.0 offers significant improvements for Solana development
- Metis API simplifies integration with DeFi protocols
- Proper architecture ensures maintainability and scalability
For production deployment:
- Add monitoring and alerts
- Implement proper secret management
- Consider multi-signature solutions for larger trades