# Constructing a transaction

We have already looked at the composition of a transaction. Now, let's dig a bit deeper. The following table shows the data structure of a transaction.

| Field                                                                              | Description                                                                                                 | Size                                               |
| ---------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------- | -------------------------------------------------- |
| Version no                                                                         | currently 2                                                                                                 | 4 bytes                                            |
| In-counter                                                                         | positive integer VI = [VarInt](https://wiki.bitcoinsv.io/index.php/VarInt)                                  | 1 - 9 bytes                                        |
| list of inputs                                                                     | [Input Structure](https://wiki.bitcoinsv.io/index.php/Bitcoin_Transactions#Format_of_a_Transaction_Input)   | \<in-counter> qty with variable length per input   |
| Out-counter                                                                        | positive integer VI = [VarInt](https://wiki.bitcoinsv.io/index.php/VarInt)                                  | 1 - 9 bytes                                        |
| list of outputs                                                                    | [Output Structure](https://wiki.bitcoinsv.io/index.php/Bitcoin_Transactions#Format_of_a_Transaction_Output) | \<out-counter> qty with variable length per output |
| [nLocktime](https://wiki.bitcoinsv.io/index.php/NLocktime_and_nSequence#nLockTime) | if non-zero and sequence numbers are < 0xFFFFFFFF: block height or timestamp when transaction is final      | 4 bytes                                            |

The following image provides an example of a typical raw transaction (P2PKH transaction with two inputs and two outputs):

<figure><img src="/files/amt4swi7AbT8UhP1tWlP" alt=""><figcaption></figcaption></figure>

The colours help to differentiate every field present in the transaction. Input script is denoted as scriptSig, and output script is denoted as scriptPubKey (due to the nature of the input script being a signature and output being a script locking the funds to a public key).

As shown in the attachment below, each field in the transaction is modularised i.e. you can build a raw transaction one by one by adding each of these fields in the right format and endianness. These transactions are usually built using well-defined libraries and functions available in various programming languages.&#x20;

{% file src="/files/EAqusL5ADRj1bSUwdYV0" %}

There will often be visualisation tools which will present the transaction details in a much more readable format; for example, below is the translated version of the input and output scripts for the raw transactions in discussion.

<figure><img src="/files/esWTGtdXrg7qZzNmlvz0" alt=""><figcaption></figcaption></figure>

Once the transaction is constructed, it is submitted to a mining node. This can be done using the RPC endpoint (called bitcoind), which is made available by the mining nodes to users. Some mining nodes also use a service component built on top of bitcoind called Merchant API or mAPI. mAPI provides a REST interface for broadcasting a transaction and returns the Merkle Proof of the transaction once the block is mined. Either way, the broadcasting of transaction is a responsibility of the user who uses either of the methods mentioned above.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hub.bsvblockchain.org/bitcoin-protocol-documentation/transaction-lifecycle/constructing-a-transaction.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
