# Finalization

When the work with the transaction is ready, you now need to decide what you want to do with it next.

# Conversion to api form

The simple toApi method returns the transaction in the Hive API-JSON form:

import { createHiveChain } from '@hiveio/wax';

// Initialize hive chain interface
const chain = await createHiveChain();

// Initialize a transaction object
const tx = await chain.createTransaction();

// Declare example operation
const operation = {
  vote_operation: {
    voter: "voter",
    author: "test-author",
    permlink: "test-permlink",
    weight: 2200
  }
};

// Push operation into the transction
tx.pushOperation(operation);

// Log to the console the transaction which is **not signed yet** in the api form
console.log(tx.toApi());

// broadcast the transaction
// Uncomment the following line to broadcast the transaction to the mainnet
// (this will most likely fail due to transaction not being signed):
// await chain.broadcast(tx);
{
  "ref_block_num": 1960,
  "ref_block_prefix": 3915120327,
  "expiration": "2023-11-09T21:51:27",
  "operations": [
    {
      "type": "vote_operation",
      "value": {
        "voter": "voter",
        "author": "test-author",
        "permlink": "test-permlink",
        "weight": 2200
      }
    }
  ]
}

The simple toApi method returns the transaction in the Hive API-JSON form:

Test it yourself on github codespace: src/python/transaction/finalization/simple_build.py
import asyncio

from wax import create_hive_chain
from wax.proto.operations import vote


# Initialize hive chain interface
chain = create_hive_chain()


async def main():
    # Initialize a transaction object
    tx = await chain.create_transaction()

    # Declare example operation
    operation = vote(voter="voter", author="test-author", permlink="test-permlink", weight=2200)

    # Push operation into the transaction
    tx.push_operation(operation)

    # Log to the console the transaction which is **not signed yet** in the api form
    print(tx.to_api())

    # broadcast the transaction
    # Uncomment the following line to broadcast the transaction to the mainnet (this will most likely fail due to transaction not being signed):
    # await chain.broadcast(transaction=tx)


asyncio.run(main())
{
  "ref_block_num": 48094,
  "ref_block_prefix": 1834167881,
  "expiration": "2025-09-02T12:29:02",
  "operations": [
    {
      "type": "vote_operation",
      "value": {
        "voter": "voter",
        "author": "test-author",
        "permlink": "test-permlink",
        "weight": 2200
      }
    }
  ],
  "extensions": [],
  "signatures": []
}

You can also represent your transaction in the API form, with your signature added to the internal signatures array (it will also apply the transaction expiration time):

import { createHiveChain } from '@hiveio/wax';

// Initialize hive chain interface
const chain = await createHiveChain();

// Initialize a transaction object
const tx = await chain.createTransaction();

// Declare example operation
const operation = {
  vote_operation: {
    voter: "voter",
    author: "author",
    permlink: "test-permlink",
    weight: 2200
  }
};

// Push operation into the transction
tx.pushOperation(operation);

// Supplement a transaction with an externally generated signature.
tx.addSignature('deadc0de');

console.log(tx.toApi());
{
  "ref_block_num": 1960,
  "ref_block_prefix": 3915120327,
  "expiration": "2023-11-09T21:51:27",
  "operations": [
    {
      "type": "vote_operation",
      "value": {
        "voter": "voter",
        "author": "author",
        "permlink": "test-permlink",
        "weight": 2200
      }
    }
  ],
  "signatures": [
    "signature..."
  ]
}
from __future__ import annotations

import asyncio

from wax import create_hive_chain
from wax.proto.operations import vote

# Initialize hive chain interface
chain = create_hive_chain()

WALLET_NAME = "wallet_name"
WALLET_PASSWORD = "wallet_password"
key = chain.get_private_key_from_password("account_name", role="active", password="password")
public_key = key.associated_public_key
private_key = key.wif_private_key


async def main():
    # Initialize a transaction object
    tx = await chain.create_transaction()

    # Declare example operation
    operation = vote(voter="voter", author="test-author", permlink="test-permlink", weight=2200)

    # Push operation into the transaction
    tx.push_operation(operation)

    # Supplement a transaction with an externally generated signature.
    tx.add_signature("deadc0de")
    print(tx.to_api())


asyncio.run(main())
{
  "ref_block_num": 48094,
  "ref_block_prefix": 1834167881,
  "expiration": "2025-09-02T12:29:02",
  "operations": [
    {
      "type": "vote_operation",
      "value": {
        "voter": "voter",
        "author": "test-author",
        "permlink": "test-permlink",
        "weight": 2200
      }
    }
  ],
  "extensions": [],
  "signatures": ['deadc0de']
}

If you want to sign your transaction in traditional way and return it in the API form, you can use this sample (it will also apply the transaction expiration time):

import { createHiveChain } from '@hiveio/wax';

// Initialize hive chain interface
const chain = await createHiveChain();

const { signer1 } = globalThis.snippetsBeekeeperData;

// Initialize a transaction object
const tx = await chain.createTransaction();

// Declare example operation
const operation = {
  vote_operation: {
    voter: "voter",
    author: "author",
    permlink: "test-permlink",
    weight: 2200
  }
};

// Push operation into the transction
tx.pushOperation(operation);

// Build transaction with signature provided.
await signer1.signTransaction(tx);

console.log(tx.toApi());
{
  "ref_block_num": 1960,
  "ref_block_prefix": 3915120327,
  "expiration": "2023-11-09T21:51:27",
  "operations": [
    {
      "type": "vote_operation",
      "value": {
        "voter": "voter",
        "author": "author",
        "permlink": "test-permlink",
        "weight": 2200
      }
    }
  ],
  "signatures": [
    "1fe8647a82f131671997ce26250bf5a1cb7a18609cbc69b3b2fd7fcaefc848c7fc308abfb0992c1ce9a805715f102416d85c4313a8a00527fa1500ac93898b418a"
  ]
}
Test it yourself on github codespace: src/python/transaction/finalization/sign_and_build.py
from __future__ import annotations

import asyncio

from beekeepy import AsyncBeekeeper
from wax import create_hive_chain
from wax.proto.operations import vote


# Initialize hive chain interface
chain = create_hive_chain()

WALLET_NAME = "wallet_name"
WALLET_PASSWORD = "wallet_password"
key = chain.get_private_key_from_password("account_name", role="active", password="password")
public_key = key.associated_public_key
private_key = key.wif_private_key


async def main():
    # Initialize a transaction object
    tx = await chain.create_transaction()

    # Declare example operation
    operation = vote(voter="voter", author="test-author", permlink="test-permlink", weight=2200)

    # Push operation into the transaction
    tx.push_operation(operation)

    async with await AsyncBeekeeper.factory() as beekeeper:
        async with await beekeeper.create_session() as session:
            existing_wallets = [w.name for w in await session.wallets_created]

            if WALLET_NAME in existing_wallets:
                wallet = await session.open_wallet(name=WALLET_NAME)
                wallet = await wallet.unlock(WALLET_PASSWORD)
            else:
                wallet = await session.create_wallet(name=WALLET_NAME, password=WALLET_PASSWORD)

            async with wallet:
                await wallet.import_key(private_key=private_key)
                # Build transaction with signature provided.
                await tx.sign(wallet, public_key)
                print(tx.to_api())


asyncio.run(main())
{
  "ref_block_num": 50325,
  "ref_block_prefix": 551408451,
  "expiration": "2025-09-02T14:20:48",
  "operations": [
    {
      "type": "vote_operation",
      "value": {
        "voter": "voter",
        "author": "test-author",
        "permlink": "test-permlink",
        "weight": 2200
      }
    }
  ],
  "extensions": [],
  "signatures": [
    "1fb3c5117c8a01459ce106f949dae1cf5ad9609b1eaa3fc49be5d418acabd0ad69549306d5df8667cc8487c2e5c20ad21380d42c1c5abe6918e94bbe5f5f4fb66e"
  ]
}

You can also sign the transaction without converting it to the API form (which will return the signatures):

import { createHiveChain } from '@hiveio/wax';

// Initialize hive chain interface
const chain = await createHiveChain();

const { signer1 } = globalThis.snippetsBeekeeperData;

// Initialize a transaction object
const tx = await chain.createTransaction();

// Declare example operation
const operation = {
  vote_operation: {
    voter: "voter",
    author: "author",
    permlink: "test-permlink",
    weight: 2200
  }
};

// Push operation into the transction
tx.pushOperation(operation);

// Sign transaction.
await signer1.signTransaction(tx);

console.log(tx.transaction.signatures[0]);
1fe8647a82f131671997ce26250bf5a1cb7a18609cbc69b3b2fd7fcaefc848c7fc308abfb0992c1ce9a805715f102416d85c4313a8a00527fa1500ac93898b418a
Test it yourself on github codespace: src/python/transaction/finalization/sign.py
from __future__ import annotations

import asyncio

from beekeepy import AsyncBeekeeper
from wax import create_hive_chain
from wax.proto.operations import vote


# Initialize hive chain interface
chain = create_hive_chain()

WALLET_NAME = "wallet_name"
WALLET_PASSWORD = "wallet_password"
key = chain.get_private_key_from_password("account_name", role="active", password="password")
public_key = key.associated_public_key
private_key = key.wif_private_key


async def main():
    # Initialize a transaction object
    tx = await chain.create_transaction()

    # Declare example operation
    operation = vote(voter="voter", author="test-author", permlink="test-permlink", weight=2200)

    # Push operation into the transaction
    tx.push_operation(operation)

    async with await AsyncBeekeeper.factory() as beekeeper:
        async with await beekeeper.create_session() as session:
            existing_wallets = [w.name for w in await session.wallets_created]

            if WALLET_NAME in existing_wallets:
                wallet = await session.open_wallet(name=WALLET_NAME)
                await wallet.unlock(WALLET_PASSWORD)
            else:
                wallet = await session.create_wallet(name=WALLET_NAME, password=WALLET_PASSWORD)

            async with wallet:
                await wallet.import_key(private_key=private_key)

                # Sign transaction.
                print(await tx.sign(wallet, public_key))


asyncio.run(main())
1fde4f09e326c69f4321e517e512770ef05986b1f33ad6fadcea75f54cf49beeb80fab11e45416adae97ed78a207b7d3950f24c78fb5ce1d0dfa1d5370f0f383ea

# Conversions

At the end you can also just convert your transaction into the Hive API-form JSON:

import { createHiveChain } from '@hiveio/wax';

// Initialize hive chain interface
const chain = await createHiveChain();

// Initialize a transaction object
const tx = await chain.createTransaction();

// Declare example operation
const operation = {
  vote_operation: {
    voter: "voter",
    author: "test-author",
    permlink: "test-permlink",
    weight: 2200
  }
};

// Push operation into the transction
tx.pushOperation(operation);

// Convert transaction into the Hive API-form JSON.
const apiTx = tx.toApi();

console.log(apiTx);
{
  "ref_block_num": 1960,
  "ref_block_prefix": 3915120327,
  "expiration": "2023-11-09T21:51:27",
  "operations": [
    {
      "type": "vote_operation",
      "value": {
        "voter": "voter",
        "author": "test-author",
        "permlink": "test-permlink",
        "weight": 2200
      }
    }
  ]
}
Test it yourself on github codespace: src/python/transaction/finalization/convert_to_api.py
from __future__ import annotations

import asyncio

from beekeepy import AsyncBeekeeper
from wax import create_hive_chain
from wax.proto.operations import vote


# Initialize hive chain interface
chain = create_hive_chain()

WALLET_NAME = "wallet_name"
WALLET_PASSWORD = "wallet_password"
key = chain.get_private_key_from_password("account_name", role="active", password="password")
public_key = key.associated_public_key
private_key = key.wif_private_key


async def main():
    # Initialize a transaction object
    tx = await chain.create_transaction()

    # Declare example operation
    operation = vote(voter="voter", author="test-author", permlink="test-permlink", weight=2200)

    # Push operation into the transaction
    tx.push_operation(operation)

    async with await AsyncBeekeeper.factory() as beekeeper:
        async with await beekeeper.create_session() as session:
            existing_wallets = [w.name for w in await session.wallets_created]

            if WALLET_NAME in existing_wallets:
                wallet = await session.open_wallet(name=WALLET_NAME)
                await wallet.unlock(WALLET_PASSWORD)
            else:
                wallet = await session.create_wallet(name=WALLET_NAME, password=WALLET_PASSWORD)

            async with wallet:
                await wallet.import_key(private_key=private_key)

                # Convert transaction into the Hive API-form JSON.
                tx_in_api_form = tx.to_api()
                print(tx_in_api_form)


asyncio.run(main())
{
  "ref_block_num": 50730,
  "ref_block_prefix": 1097976323,
  "expiration": "2025-09-02T14:41:07",
  "operations": [
    {
      "type": "vote_operation",
      "value": {
        "voter": "voter",
        "author": "test-author",
        "permlink": "test-permlink",
        "weight": 2200
      }
    }
  ],
  "extensions": [],
  "signatures": []
}

Or you can just convert transction to legacy API form:

import { createHiveChain } from '@hiveio/wax';

// Initialize hive chain interface
const chain = await createHiveChain();

// Initialize a transaction object
const tx = await chain.createTransaction();

// Declare example operation
const operation = {
  vote_operation: {
    voter: "voter",
    author: "test-author",
    permlink: "test-permlink",
    weight: 2200
  }
};

// Push operation into the transction
tx.pushOperation(operation);

// Convert transaction into the Hive API-legacy form JSON string
const legacyApiTx = tx.toLegacyApi();

console.log(legacyApiTx);
{
  "ref_block_num": 1960,
  "ref_block_prefix": 3915120327,
  "expiration": "2023-11-09T21:51:27",
  "operations": [
    [
      "vote",
      {
        "voter": "voter",
        "author": "test-author",
        "permlink": "test-permlink",
        "weight": 2200
      }
    ]
  ],
  "extensions": [],
  "signatures": []
}
The legacy transaction format is deprecated — wax-python no longer supports this conversion.