<p>The TX fee is the difference between the totals of all the input values and all the output values. If you are looking at a single TX you can see all the output values, but how do you get the input values? They are not included in the TX itself. What you must do is look up the UTXO being spent by each TX input and total their values. (The data structure used to track and process all the input coins is called a "view").</p>

<p>Note that there is no "applying a fee to the output". A TX has no extra fee field or property, and the fee itself does not occupy an entire output.</p>

<p>If you are constructing a TX where the input values equal exactly the output values, you "include" a fee by reducing the value of one of the outputs.</p>

CC BY-SA 4.0
2020-05-05T14:27:02.570Z
2020-05-05T14:27:02.570Z
pinhead
<p>I have a transaction with 3 inputs and 2 outputs. 1output is the recipient and the other output is change address. How should I calculate the fee and apply it to output?</p>

CC BY-SA 4.0
2020-05-05T15:01:28.873Z
Does this answer your question? [Specifying fee amount using bitcoinjs](https://bitcoin.stackexchange.com/questions/57782/specifying-fee-amount-using-bitcoinjs)
MCCCS
CC BY-SA 4.0
2020-05-29T01:01:57.387Z
Your question shows a bit of a misunderstanding.  If you read some of the hits after searching for "transaction fee," I think you'll ask a better question.  You may have meant, "How do I SET the fee in a BitcoinJS raw transaction?" but even this would show that you don't realize the fee is simply the difference between the sum of the inputs and the sum of the outputs.  Or perhaps you meant to ask for advice in choosing a fee (https://bitcoin.stackexchange.com/questions/64443/how-to-find-what-is-suitable-transaction-fee-to-send-10-mbtc/96214#96214).
Dave Scotese
<transactions><transaction-fees><raw-transaction><bitcoinjs>
How to calculate fee in BitcoinJS raw transaction?
Amirhosein Rajabi
# How to calculate fee in BitcoinJS raw transaction?

(Asked by: username_0 on 2020-05-05)

I have a transaction with 3 inputs and 2 outputs. 1output is the recipient and the other output is change address. How should I calculate the fee and apply it to output?

**Tags:** transactions, transaction-fees, raw-transaction, bitcoinjs

> **Comments:**
> **username_1**: Does this answer your question? [Specifying fee amount using bitcoinjs](https://bitcoin.stackexchange.com/questions/57782/specifying-fee-amount-using-bitcoinjs)  
> **username_2**: Your question shows a bit of a misunderstanding.  If you read some of the hits after searching for "transaction fee," I think you'll ask a better question.  You may have meant, "How do I SET the fee in a BitcoinJS raw transaction?" but even this would show that you don't realize the fee is simply the difference between the sum of the inputs and the sum of the outputs.  Or perhaps you meant to ask for advice in choosing a fee (https://bitcoin.stackexchange.com/questions/64443/how-to-find-what-is-suitable-transaction-fee-to-send-10-mbtc/96214#96214).  

---

## 1 answers

### Answer by username_3 on 2020-05-05. Score: 2

The TX fee is the difference between the totals of all the input values and all the output values. If you are looking at a single TX you can see all the output values, but how do you get the input values? They are not included in the TX itself. What you must do is look up the UTXO being spent by each TX input and total their values. (The data structure used to track and process all the input coins is called a "view").

Note that there is no "applying a fee to the output". A TX has no extra fee field or property, and the fee itself does not occupy an entire output.

If you are constructing a TX where the input values equal exactly the output values, you "include" a fee by reducing the value of one of the outputs.


---

