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
- Compute unit limit (
SetComputeUnitLimit): the most compute units the transaction may use. Up to 1,400,000. Default 200,000 per instruction. - Compute unit price (
SetComputeUnitPrice): micro-lamports per compute unit. 1,000,000 micro-lamports = 1 lamport.
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
| Instruction | Data |
|---|---|
| SetComputeUnitLimit | 02 + u32 little-endian (5 bytes) |
| SetComputeUnitPrice | 03 + 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
- Scoping to a program id. Programs are read-only in your transaction, so they add nothing to the fee sample. Use the pool or curve account your transaction writes.
- Calling it without accounts. The unscoped answer is each block's overall minimum, which on mainnet is usually 0. On 2026-11-17 all 150 recent blocks were 0.
- Leaving the limit at the default. A swap that uses 120,000 CU but requests 1,400,000 pays the price on 1,400,000.
- Using price in lamports. The unit is micro-lamports per compute unit; 10,000 means 0.01 lamports per unit.
- Two price instructions. A transaction may hold at most one of each Compute Budget instruction; a duplicate fails the whole transaction.
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.