Get Transaction
The getTransaction function retrieves detailed information about a transaction by its signature.
Usage
const transaction = await connection.getTransaction(signature);Parameters
signature:string- The transaction signature to look upcommitment:Commitment(optional) - Confirmation level to use (default: “finalized”)"processed"- Processed by current node"confirmed"- Confirmed by supermajority of the cluster"finalized"- Confirmed by supermajority and unlikely to revert
maxSupportedTransactionVersion:number(optional) - Maximum transaction version to return (default: 0)
Returns
Returns a Promise<Object | null>:
Objectcontaining transaction details if foundnullif the transaction doesn’t exist
The transaction object includes:
slot: The slot in which the transaction was processedtransaction: The transaction data including signatures and messagemeta: Transaction metadata including fee, logs, and account changesblockTime: Estimated production time as Unix timestamp
Example
// Get transaction details
const signature = "5j7s6NiJS3JAkvgkoc18WVAsiSaci2pxB2A6ueCJP4tprA2TFg9wSyTLeYouxPBJEMzJinENTkpA52YStRW5Dia7";
const transaction = await connection.getTransaction(signature);
if (transaction) {
console.log(`Transaction found in slot: ${transaction.slot}`);
console.log(`Block time: ${new Date(transaction.blockTime! * 1000).toISOString()}`);
console.log(`Fee: ${transaction.meta?.fee} lamports`);
// Access transaction logs
if (transaction.meta?.logMessages) {
console.log("Transaction logs:");
transaction.meta.logMessages.forEach(log => console.log(log));
}
// Check if transaction succeeded
if (transaction.meta?.err) {
console.log("Transaction failed with error:", transaction.meta.err);
} else {
console.log("Transaction succeeded");
}
} else {
console.log("Transaction not found");
}
// Get with specific commitment
const recentTx = await connection.getTransaction(signature, "confirmed");
// Support versioned transactions
const versionedTx = await connection.getTransaction(
signature,
"finalized",
1 // Support version 0 and version 1 transactions
);Use Cases
- Debugging failed transactions
- Analyzing transaction logs and account changes
- Building transaction explorers
- Auditing transaction history
- Verifying transaction execution details
Notes
- Returns
nullif the transaction is not found - Transactions may not be available if they are too old (RPC nodes typically keep ~2-3 days)
- Use “finalized” commitment for confirmed transactions that won’t be rolled back
- The
maxSupportedTransactionVersionparameter allows fetching versioned transactions - Transaction logs are available in
meta.logMessages - Account balance changes are in
meta.preBalancesandmeta.postBalances
Related Functions
- Get Logs - Get logs for a transaction (simpler alternative)
- Get Recent Signature Confirmation - Check if a transaction is confirmed
- Send and Confirm Transaction - Send and wait for confirmation
Last updated on