### Connect and Subscribe to WebSocket Data Streams Source: https://context7.com/thetateman/trading-api/llms.txt Establishes a WebSocket connection and sends subscription messages for different event types. Ensure you have the 'ws' library installed. Multiple subscriptions can be active simultaneously. ```javascript import WebSocket from 'ws'; const ws = new WebSocket('wss://pumpportal.fun/api/data'); ws.on('open', function open() { console.log('Connected to PumpPortal WebSocket'); // Stream all new token creation events ws.send(JSON.stringify({ method: 'subscribeNewToken', })); // Stream all trades on one or more specific tokens (by contract address) ws.send(JSON.stringify({ method: 'subscribeTokenTrade', keys: [ '91WNez8D22NwBssQbkzjy4s2ipFrzpmn5hfvWVe2aY5p', 'AnotherTokenContractAddressHere', ], })); // Stream all trades made by one or more specific wallet addresses ws.send(JSON.stringify({ method: 'subscribeAccountTrade', keys: [ 'AArPXm8JatJiuyEffuC1un2Sc835SULa4uQqDcaGpAjV', ], })); }); ws.on('message', function message(data) { const event = JSON.parse(data); console.log('Event received:', event); // Example output for a trade event: // { // type: 'tokenTrade', // mint: '91WNez8D22NwBssQbkzjy4s2ipFrzpmn5hfvWVe2aY5p', // traderPublicKey: 'AArPXm8JatJiuyEffuC1un2Sc835SULa4uQqDcaGpAjV', // txType: 'buy', // tokenAmount: 1000000, // solAmount: 0.05, // ... // } }); ws.on('error', function error(err) { console.error('WebSocket error:', err.message); }); ws.on('close', function close() { console.log('Disconnected from PumpPortal WebSocket'); }); ``` -------------------------------- ### Local Trading API Source: https://github.com/thetateman/trading-api/blob/main/README.md Send a POST request to this endpoint to get a serialized transaction for local signing and sending with a custom RPC. This API is gated by an API key and charges a 0.5% fee per trade. ```APIDOC ## POST /api/trade-local ### Description Generates a serialized transaction for trading on Pump.fun. This transaction can then be signed and sent using a custom RPC. ### Method POST ### Endpoint `https://pumpportal.fun/api/trade-local` ### Parameters #### Request Body - **publicKey** (string) - Required - Your wallet public key. - **action** (string) - Required - The action to perform, either "buy" or "sell". - **mint** (string) - Required - The contract address of the token you want to trade. - **amount** (number or string) - Required - The amount of SOL or tokens to trade. If selling, can be a percentage (e.g., "100%"). - **denominatedInSol** (string) - Required - Set to "true" if the amount is in SOL, "false" if in tokens. - **slippage** (number) - Required - The percent slippage allowed. - **priorityFee** (number) - Required - The amount to use as a priority fee. - **pool** (string) - Optional - The exchange to trade on. Supported options are 'pump' and 'raydium'. Defaults to 'pump'. ### Response #### Success Response (200) - A serialized transaction (Buffer). ### Request Example ```js { "publicKey": "your-public-key", "action": "buy", "mint": "token-ca-here", "denominatedInSol": "false", "amount": 1000, "slippage": 1, "priorityFee": 0.00001, "pool": "pump" } ``` ``` -------------------------------- ### Local Trading API - Sell Token by Percentage Source: https://context7.com/thetateman/trading-api/llms.txt Generates a serialized, unsigned Solana transaction for selling a percentage of held tokens on Pump.fun or Raydium. Requires local signing and broadcasting. Use 'false' for 'denominatedInSol' and specify percentage for amount. ```javascript async function sellTokenByPercentage() { const response = await fetch('https://pumpportal.fun/api/trade-local', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ publicKey: 'YourWalletPublicKeyHere', action: 'sell', mint: '91WNez8D22NwBssQbkzjy4s2ipFrzpmn5hfvWVe2aY5p', denominatedInSol: 'false', amount: '100%', slippage: 10, priorityFee: 0.00001, pool: 'pump', }), }); if (response.status === 200) { const data = await response.arrayBuffer(); const tx = VersionedTransaction.deserialize(new Uint8Array(data)); const signerKeyPair = Keypair.fromSecretKey(bs58.decode('YourBase58PrivateKey')); tx.sign([signerKeyPair]); const signature = await web3Connection.sendTransaction(tx); console.log('Sell confirmed:', `https://solscan.io/tx/${signature}`); } else { console.error('Sell failed:', response.statusText); } } ``` -------------------------------- ### Subscribe to Real-time Pump.fun Data via WebSocket Source: https://github.com/thetateman/trading-api/blob/main/README.md Connect to the PumpPortal WebSocket to receive real-time updates on token creation, trades for specific tokens, or trades by specific accounts. You can subscribe to different data streams by sending JSON payloads with the desired method and keys. ```javascript import WebSocket from 'ws'; const ws = new WebSocket('wss://pumpportal.fun/api/data'); ws.on('open', function open() { // Subscribing to token creation events let payload = { method: "subscribeNewToken", } ws.send(JSON.stringify(payload)); // Subscribing to trades made by accounts payload = { method: "subscribeAccountTrade", keys: ["AArPXm8JatJiuyEffuC1un2Sc835SULa4uQqDcaGpAjV"] // array of accounts to watch } ws.send(JSON.stringify(payload)); // Subscribing to trades on tokens payload = { method: "subscribeTokenTrade", keys: ["91WNez8D22NwBssQbkzjy4s2ipFrzpmn5hfvWVe2aY5p"] // array of token CAs to watch } ws.send(JSON.stringify(payload)); }); ws.on('message', function message(data) { console.log(JSON.parse(data)); }); ``` -------------------------------- ### Real-Time Data WebSocket Connection Source: https://context7.com/thetateman/trading-api/llms.txt Connect to the WebSocket and subscribe to different data streams. You can subscribe to all new token launches, trades on specific token contract addresses, or trades made by specific wallet addresses. Multiple subscriptions can be active simultaneously. ```APIDOC ## Real-Time Data WebSocket — `wss://pumpportal.fun/api/data` ### Description Provides a persistent WebSocket connection for streaming live Pump.fun events. After connecting, send JSON subscription messages to opt into one or more data streams. Three subscription methods are available: `subscribeNewToken` (all new token launches), `subscribeTokenTrade` (all trades on specified token contract addresses), and `subscribeAccountTrade` (all trades made by specified wallet addresses). Multiple subscriptions can be active simultaneously on a single connection. The Data API is free with no charge, subject only to rate limits. ### Method WebSocket Connection ### Endpoint `wss://pumpportal.fun/api/data` ### Subscription Methods Send JSON messages to the WebSocket to subscribe to data streams: 1. **`subscribeNewToken`** * **Description**: Stream all new token creation events. * **Request Body Example**: ```json { "method": "subscribeNewToken" } ``` 2. **`subscribeTokenTrade`** * **Description**: Stream all trades on one or more specific tokens (by contract address). * **Parameters**: * **`keys`** (array of strings) - Required - An array of token contract addresses. * **Request Body Example**: ```json { "method": "subscribeTokenTrade", "keys": [ "91WNez8D22NwBssQbkzjy4s2ipFrzpmn5hfvWVe2aY5p", "AnotherTokenContractAddressHere" ] } ``` 3. **`subscribeAccountTrade`** * **Description**: Stream all trades made by one or more specific wallet addresses. * **Parameters**: * **`keys`** (array of strings) - Required - An array of wallet public addresses. * **Request Body Example**: ```json { "method": "subscribeAccountTrade", "keys": [ "AArPXm8JatJiuyEffuC1un2Sc835SULa4uQqDcaGpAjV" ] } ``` ### Event Messages Received messages will be in JSON format. Example for a trade event: ```json { "type": "tokenTrade", "mint": "91WNez8D22NwBssQbkzjy4s2ipFrzpmn5hfvWVe2aY5p", "traderPublicKey": "AArPXm8JatJiuyEffuC1un2Sc835SULa4uQqDcaGpAjV", "txType": "buy", "tokenAmount": 1000000, "solAmount": 0.05 // ... other fields } ``` ``` -------------------------------- ### Real-time Updates (WebSocket API) Source: https://github.com/thetateman/trading-api/blob/main/README.md Connect to this WebSocket endpoint to stream real-time trading and token creation data. Various subscription methods are available to filter the data streams. ```APIDOC ## WebSocket API ### Description Provides real-time data streams for token creation events, trades on specific tokens, and trades by specific accounts. ### Endpoint `wss://pumpportal.fun/api/data` ### Methods - **subscribeNewToken**: Subscribes to token creation events. - **subscribeTokenTrade**: Subscribes to all trades made on specific token(s). - **subscribeAccountTrade**: Subscribes to all trades made by specific account(s). ### Request Example (Subscribing to multiple streams) ```js { "method": "subscribeNewToken" } { "method": "subscribeAccountTrade", "keys": ["AArPXm8JatJiuyEffuC1un2Sc835SULa4uQqDcaGpAjV"] } { "method": "subscribeTokenTrade", "keys": ["91WNez8D22NwBssQbkzjy4s2ipFrzpmn5hfvWVe2aY5p"] } ``` ``` -------------------------------- ### Local Trading API - Buy Token Source: https://context7.com/thetateman/trading-api/llms.txt Generates a serialized, unsigned Solana transaction for buying tokens on Pump.fun or Raydium. Requires local signing and broadcasting. Ensure correct parameters for amount, slippage, and priority fee. ```javascript import { VersionedTransaction, Connection, Keypair } from '@solana/web3.js'; import bs58 from 'bs58'; const RPC_ENDPOINT = 'https://api.mainnet-beta.solana.com'; const web3Connection = new Connection(RPC_ENDPOINT, 'confirmed'); async function buyToken() { const response = await fetch('https://pumpportal.fun/api/trade-local', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ publicKey: 'YourWalletPublicKeyHere', action: 'buy', mint: '91WNez8D22NwBssQbkzjy4s2ipFrzpmn5hfvWVe2aY5p', denominatedInSol: 'true', amount: 0.1, slippage: 5, priorityFee: 0.00001, pool: 'pump', }), }); if (response.status === 200) { const data = await response.arrayBuffer(); const tx = VersionedTransaction.deserialize(new Uint8Array(data)); const signerKeyPair = Keypair.fromSecretKey(bs58.decode('YourBase58PrivateKey')); tx.sign([signerKeyPair]); const signature = await web3Connection.sendTransaction(tx); console.log('Transaction confirmed:', `https://solscan.io/tx/${signature}`); } else { console.error('Trade failed:', response.statusText); } } buyToken(); ``` -------------------------------- ### Local Trading API - POST /api/trade-local Source: https://context7.com/thetateman/trading-api/llms.txt Generates a serialized, unsigned Solana transaction for a buy or sell operation. The transaction is returned as a binary buffer for local signing and broadcasting. ```APIDOC ## POST /api/trade-local ### Description Generates a serialized, unsigned Solana transaction for a buy or sell on either the Pump.fun bonding curve or Raydium. The transaction is returned as a binary buffer, which the caller deserializes, signs locally with their keypair, and broadcasts through their own RPC node. Supported pools are `pump` (default) and `raydium`. A 0.5% fee is charged on each trade (calculated pre-slippage). ### Method POST ### Endpoint `/api/trade-local` ### Parameters #### Request Body - **publicKey** (string) - Required - The public key of the user's wallet. - **action** (string) - Required - The trade action, either 'buy' or 'sell'. - **mint** (string) - Required - The token contract address. - **denominatedInSol** (string) - Required - 'true' if the amount is in SOL, 'false' if the amount is in tokens. - **amount** (number | string) - Required - The amount to trade. If `denominatedInSol` is 'true', this is the SOL amount. If `denominatedInSol` is 'false' and `action` is 'sell', this can be a percentage string (e.g., '100%'). - **slippage** (number) - Required - Slippage tolerance in percentage. - **priorityFee** (number) - Required - Solana priority fee in SOL. - **pool** (string) - Optional - The trading pool, either 'pump' (default) or 'raydium'. ### Request Example ```json { "publicKey": "YourWalletPublicKeyHere", "action": "buy", "mint": "91WNez8D22NwBssQbkzjy4s2ipFrzpmn5hfvWVe2aY5p", "denominatedInSol": "true", "amount": 0.1, "slippage": 5, "priorityFee": 0.00001, "pool": "pump" } ``` ### Response #### Success Response (200) - **data** (binary buffer) - A serialized, unsigned Solana transaction. #### Response Example (Binary data representing a VersionedTransaction) ``` -------------------------------- ### Generate and Send Local Trading Transaction Source: https://github.com/thetateman/trading-api/blob/main/README.md Use this snippet to generate a serialized transaction for buying or selling tokens on Pump.fun. It requires your public key, desired action, token mint address, amount, slippage, and priority fee. The response is a serialized transaction that must be signed locally and sent using a Web3.js connection. ```javascript import { VersionedTransaction, Connection, Keypair } from '@solana/web3.js'; import bs58 from "bs58"; const RPC_ENDPOINT = "Your RPC Endpoint"; const web3Connection = new Connection( RPC_ENDPOINT, 'confirmed', ); async function sendPortalTransaction(){ const response = await fetch(`https://pumpportal.fun/api/trade-local`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ "publicKey": "your-public-key", // Your wallet public key "action": "buy", // "buy" or "sell" "mint": "token-ca-here", // contract address of the token you want to trade "denominatedInSol": "false", // "true" if amount is amount of SOL, "false" if amount is number of tokens "amount": 1000, // amount of SOL or tokens "slippage": 1, // percent slippage allowed "priorityFee": 0.00001, // priority fee "pool": "pump" // exchange to trade on. "pump" or "raydium" }) }); if(response.status === 200){ // successfully generated transaction const data = await response.arrayBuffer(); const tx = VersionedTransaction.deserialize(new Uint8Array(data)); const signerKeyPair = Keypair.fromSecretKey(bs58.decode("your-wallet-private-key")); tx.sign([signerKeyPair]); const signature = await web3Connection.sendTransaction(tx) console.log("Transaction: https://solscan.io/tx/" + signature); } else { console.log(response.statusText); // log error } } sendPortalTransaction(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.