Your first transaction

A couple fundamentals to remember are that in Cardano's eUTxO model, a transaction can consume one or more UTxOs as inputs. Create one or more UTxOs as outputs and that a transaction must be balanced (sum of inputs = sum of outputs + fees).

Build

Let's create a simple transaction where we send 5 ADA to two recipients each:

const tx = await lucid
  .newTx()
  .pay.ToAddress("addr_testa...", { lovelace: 5000000n })
  .pay.ToAddress("addr_testb...", { lovelace: 5000000n })
  .complete();
💡

To balance the transaction and initiate coin selection, transactions always need to end with .complete()

Sign

const signedTx = await tx.sign.withWallet().complete();

You could also choose to sign the transaction with a private key:

const signedTx = await tx.sign.withPrivateKey(privateKey).complete();

Submit

Lastly we submit the transaction:

const txHash = await signedTx.submit();
console.log(txHash);
💡

Remember to select a wallet before attempting to build and submit transactions. The wallet selection methods we discussed in previous section Core Concept 2: Choose Wallet should be implemented before building the transaction.

Putting everything together

import { Lucid, Blockfrost } from "@lucid-evolution/lucid";
 
// Initialize Lucid with a provider
const lucid = await Lucid(
  new Blockfrost("https://cardano-preprod.blockfrost.io/api/v0", "<projectId>"),
  "Preprod"
);
 
// Select a wallet for signing
lucid.selectWallet.fromPrivateKey(privateKey);
 
// Build, sign and submit transaction
const tx = await lucid
  .newTx()
  .pay.ToAddress("addr_testa...", { lovelace: 5000000n }) // Pay 5 ADA to addr_testa
  .complete(); // Balances the transaction and initiates coin selection
 
const signedTx = await tx.sign.withWallet().complete();
const txHash = await signedTx.submit();
console.log(txHash);