Building a Trading Bot with a Pump.fun API

PumpPortal
4 min readJul 6, 2024

--

With millions of SOL flowing through pump.fun every day, it’s no wonder that successful bots and snipers can earn a killing on the platform. In this post we’ll walk through building an automated trading bot that snipes tokens launched on pump.fun. (Note this bot is a proof of concept, and you should develop your own strategy to maximize profitability.)

We’ll build our bot using the PumpPortal API, which enables trading on pump.fun through HTTP methods. For simplicity, the bot will use the Managed Trading API, which handles signing and sending transactions to the blockchain for us.

Before getting started, we’ll need to create a PumpPortal wallet and API key, and fund the wallet with some SOL. (To use a self-custody wallet, use the Local Trading API — the only downside is you have to manage signing and sending transactions yourself.)

Make sure to safely store your keys — they are not recoverable if lost!

Now that our wallet is set up, we can get started coding our bot. First let’s create a new folder and add a file inside called bot.js. In bot.js we’ll add the code from the API examples to stream new token creations in real time (modified slightly as we only need token creation events for now):

// bot.js
const WebSocket = require("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));
});

ws.on("message", function message(data) {
console.log(JSON.parse(data));
});

To run bot.js, we’ll need to install the WebSocket library for Node.js by running the install command in the terminal:

npm install ws

Now we should be able to test the bot by running bot.js with Node:

node bot.js

If everything is correct, our bot should log info about new coin creations to the console as they come in:

Now that we’re receiving the token creation events, let’s add a function to buy some of the token.

const pumpPortalAPIKey = "****************************";
async function sendPumpTransaction(action, mint, amount){
const response = await fetch(`https://pumpportal.fun/api/trade?api-key=${pumpPortalAPIKey}`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
"action": action, // "buy" or "sell"
"mint": mint, // contract address of the token you want to trade
"denominatedInSol": "true", // "true" if amount is amount of SOL, "false" if amount is number of tokens
"amount": amount, // amount of SOL or tokens or percent
"slippage": 50, // percent slippage allowed
"priorityFee": 0.0005,
"pool": "pump"
})
});
const data = await response.json();
if(data.errors.length > 0){
console.log(data.errors);
} else {
console.log("Transaction: https://solscan.io/tx/" + data.signature)
}
}

Now we can call sendPumpTransaction() in the Websocket message callback to buy 0.01 SOL of a token right after it’s created.

ws.on("message", function message(data) {
console.log(JSON.parse(data));
const tokenCreationData = JSON.parse(data);
if (tokenCreationData.mint) {
console.log("Buying: " + tokenCreationData.mint);
sendPumpTransaction("buy", tokenCreationData.mint, 0.01);
}
});

Finally, we want our bot to take profits, so we’ll make it sell 100% the tokens 20 seconds after buying. Here’s what our final bot looks like:

const WebSocket = require("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));
});

ws.on("message", function message(data) {
console.log(JSON.parse(data));
const tokenCreationData = JSON.parse(data);
if (tokenCreationData.mint) {
console.log("Buying: " + tokenCreationData.mint);
sendPumpTransaction("buy", tokenCreationData.mint, 0.01).then(() => {
// sell after 20000 milliseconds
setTimeout(() => {
console.log("Selling: " + tokenCreationData.mint);
sendPumpTransaction("sell", tokenCreationData.mint, "100%");
}, 20000);
});
}
});

const pumpPortalAPIKey = "*****************************";
async function sendPumpTransaction(action, mint, amount) {
const response = await fetch(
`https://pumpportal.fun/api/trade?api-key=${pumpPortalAPIKey}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
action: action, // "buy" or "sell"
mint: mint, // contract address of the token you want to trade
denominatedInSol: "true", // "true" if amount is amount of SOL, "false" if amount is number of tokens
amount: amount, // amount of SOL or tokens or percent
slippage: 50, // percent slippage allowed
priorityFee: 0.0005,
pool: "pump",
}),
}
);
const data = await response.json();
console.log(data);
if (data.errors.length > 0) {
console.log(data.errors);
} else {
console.log("Transaction: https://solscan.io/tx/" + data.signature);
}
}

Congratulations on creating your first Solana trading bot! Even though it uses a fairly simple trading strategy, you can continue using these tools to build all kinds of bots and apps to interact with pump.fun. To keep learning, review the PumpPortal documentation to see all the data the API provides, or join our community Telegram to ask questions and collaborate with other builders!

Hope to see you around :)

--

--

PumpPortal

PumpPortal is a 3rd-Party API for defi apps on the Solana blockchain.