How to set a Solana priority fee in JavaScript

A Solana priority fee is two small instructions at the front of your transaction: one sets how many compute units it may use, the other sets the price per unit. Here is how to add them with @solana/web3.js, with @solana/kit, or with no SDK at all, and how to pick a price that is not a guess.

The two numbers

The priority fee in lamports is ceil(price × limit / 1,000,000). It is charged on the limit you request, not on what the transaction uses, so a tight limit matters as much as the price. On top of it comes the base fee of 5,000 lamports per signature.

Pick the price from recent blocks

Every RPC has getRecentPrioritizationFees. Pass the writable accounts your transaction will lock (the pool, the bonding curve) and it returns, per recent block, the lowest price that landed next to writes to them. solana-priority-fee turns those ~150 numbers into percentiles and levels:

npm install github:blixvip/solana-priority-fee

fee.recommended.high is the 75th percentile: in three of four recent blocks, the cheapest transaction that landed on that account paid at most this much. Why that is a reasonable default is in What is a good Solana priority fee right now.

With @solana/web3.js (v1)

import { Connection, ComputeBudgetProgram, Keypair, SystemProgram, Transaction } from "@solana/web3.js";
import { estimatePriorityFee } from "solana-priority-fee";

const connection = new Connection(process.env.SOLANA_RPC_URL ?? "https://api.mainnet-beta.solana.com");
const POOL = "58oQChx4yWmvKdwLLZzBi4ChoCc2fqCUWBkwMihLYQo2"; // a writable account your transaction locks

const fee = await estimatePriorityFee({ accounts: [POOL] });
const microLamports = fee.recommended.high; // p75 of recent blocks

const payer = Keypair.generate(); // your wallet in real code
const tx = new Transaction().add(
  ComputeBudgetProgram.setComputeUnitLimit({ units: 200_000 }),
  ComputeBudgetProgram.setComputeUnitPrice({ microLamports }),
  SystemProgram.transfer({ fromPubkey: payer.publicKey, toPubkey: payer.publicKey, lamports: 1 }),
);
tx.feePayer = payer.publicKey;
tx.recentBlockhash = (await connection.getLatestBlockhash()).blockhash;

const { value: lamports } = await connection.getFeeForMessage(tx.compileMessage());
console.log(`price ${microLamports} micro-lamports/CU, total fee ${lamports} lamports`);

Output on 2026-11-17 against the public RPC:

price 10000 micro-lamports/CU, total fee 7000 lamports

7,000 = 5,000 base fee for one signature + 2,000 priority fee (10,000 × 200,000 / 1,000,000). getFeeForMessage asks the RPC what the transaction will cost before you sign or send it, which is a good check that your instructions are in place. Then sign and send as usual.

With @solana/kit

Kit keeps program instructions in separate packages; the Compute Budget ones are in @solana-program/compute-budget:

npm install @solana/kit @solana-program/compute-budget github:blixvip/solana-priority-fee
import {
  appendTransactionMessageInstructions, createSolanaRpc, createTransactionMessage, generateKeyPairSigner, pipe,
  setTransactionMessageFeePayerSigner, setTransactionMessageLifetimeUsingBlockhash,
} from "@solana/kit";
import { getSetComputeUnitLimitInstruction, getSetComputeUnitPriceInstruction } from "@solana-program/compute-budget";
import { estimatePriorityFee } from "solana-priority-fee";

const rpc = createSolanaRpc(process.env.SOLANA_RPC_URL ?? "https://api.mainnet-beta.solana.com");
const fee = await estimatePriorityFee({ accounts: ["58oQChx4yWmvKdwLLZzBi4ChoCc2fqCUWBkwMihLYQo2"] });

const payer = await generateKeyPairSigner(); // your wallet in real code
const { value: blockhash } = await rpc.getLatestBlockhash().send();

const message = pipe(
  createTransactionMessage({ version: 0 }),
  (m) => setTransactionMessageFeePayerSigner(payer, m),
  (m) => setTransactionMessageLifetimeUsingBlockhash(blockhash, m),
  (m) => appendTransactionMessageInstructions([
    getSetComputeUnitLimitInstruction({ units: 200_000 }),
    getSetComputeUnitPriceInstruction({ microLamports: BigInt(fee.recommended.high) }),
    // ...your own instructions
  ], m),
);
console.log(message.instructions.length, "instructions; price", fee.recommended.high, "micro-lamports/CU");
2 instructions; price 10000 micro-lamports/CU

Kit takes the price as a bigint; the estimator returns plain numbers, hence the BigInt().

Without an SDK: the raw bytes

If you build transactions yourself (or in a language binding), the two instructions have no accounts and tiny data: a discriminator byte and a little-endian number.

import { toInstructionsData } from "solana-priority-fee";

const ix = toInstructionsData(10_000, 200_000);
console.log(ix.programId);
console.log(Buffer.from(ix.setComputeUnitLimit).toString("hex")); // 02400d0300
console.log(Buffer.from(ix.setComputeUnitPrice).toString("hex")); // 031027000000000000
InstructionData
SetComputeUnitLimit02 + u32 little-endian (5 bytes)
SetComputeUnitPrice03 + u64 little-endian (9 bytes)

The program id is ComputeBudget111111111111111111111111111111. These bytes are identical to what ComputeBudgetProgram in web3.js produces; we checked both against each other and against the official interface's test vectors.

Common mistakes

Keeping it current

Fees move with activity. Re-estimate right before you build each transaction, or keep a cached value fresh in the background with watch(5000, callback). For a shared service, the repo has an HTTP fee endpoint you can run in front of your RPC.

Want it done for you?

This is the small, open version. Blix is the AI-powered Solana memecoin terminal it comes from: every pump.fun launch live, AI coin ratings, narratives, wallet and X tracking, and one-click trading with 0% fees. There is a public API and an MCP server too.

Start trading at blixvip.comJoin the Discord

Help build it

solana-priority-fee is open source and takes pull requests. There are good first issues with step-by-step instructions, and setup is git clone and npm test. If it helped, a star on GitHub helps other developers find it.

More guides

What is a good Solana priority fee right now (and how to measure it)solana-priority-feeHow to give Claude access to Solana data with MCPsolana-readonly-mcpHow to stop Solana RPC 429 errors (Too Many Requests)solana-rpc-poolHow to get a pump.fun token's market cap and bonding curve progress without an APIpump-curve