Lesson 1: Hello World
Install Mesh SDK, create a wallet, and send your first Cardano transaction.
Learning Objectives
By the end of this lesson, you will be able to:
- Set up a Node.js project with the Mesh SDK
- Generate a new wallet with mnemonic phrases
- Retrieve your wallet address
- Send lovelace (ADA) to another address
- Understand UTXOs and transaction building basics
Prerequisites
Before starting this lesson, ensure you have:
- Node.js v20+ installed (nvm recommended for version management)
- A code editor (VS Code recommended)
- Basic familiarity with TypeScript/JavaScript
Step 1: Create Your Project
Create a new directory and initialize your project with a package.json file.
mkdir mesh-hello-world
cd mesh-hello-worldCreate a package.json file with ES module support:
{
"type": "module",
"dependencies": {},
"scripts": {}
}Step 2: Install the Mesh SDK
Install the Mesh SDK core package:
npm install @meshsdk/coreYour package.json now looks like this:
{
"type": "module",
"dependencies": {
"@meshsdk/core": "^1.9.0"
},
"scripts": {}
}Step 3: Generate a Wallet
The MeshCardanoHeadlessWallet class provides methods to create wallets, generate mnemonic phrases, and retrieve addresses.
Key Concepts
- Mnemonic phrase: A set of 24 words that can recover your wallet. Keep this secure.
- Network ID: Use
0for preprod testnet,1for mainnet. - Change address: The address where remaining funds return after a transaction.
Create the Mnemonic Script
Create a new file called mnemonic.ts:
import { MeshCardanoHeadlessWallet, AddressType } from "@meshsdk/wallet";
// Generate new mnemonic phrases for your wallet
const mnemonic = MeshCardanoHeadlessWallet.brew();
console.log("Your mnemonic phrases are:", mnemonic);
// Initialize the wallet with the mnemonic key
const wallet = await MeshCardanoHeadlessWallet.fromMnemonic({
networkId: 0, // preprod testnet
walletAddressType: AddressType.Base,
mnemonic: mnemonic as string[],
});
// Get the wallet address
const address = await wallet.getChangeAddressBech32();
console.log("Your wallet address is:", address);Run the Script
Add a script to your package.json:
{
"type": "module",
"dependencies": {
"@meshsdk/core": "^1.9.0"
},
"scripts": {
"mnemonic": "node mnemonic.ts"
}
}Run the script:
npm run mnemonicYou see output similar to:
Your mnemonic phrases are: [
'access', 'spawn', 'taxi',
'prefer', 'fortune', 'sword',
'nerve', 'price', 'valid',
'panther', 'sure', 'hello',
'layer', 'try', 'grace',
'seven', 'fossil', 'voice',
'tobacco', 'circle', 'measure',
'solar', 'pride', 'together'
]
Your wallet address is: addr_test1qptwuv6dl863u3k93mjrg0hgs0ahl08lfhsudxrwshcsx59cjxatme29s6cl7drjceknunry049shu9eudnsjvwqq9qsuem66dImportant: Save your mnemonic phrase securely. You need it for the next steps.
Step 4: Fund Your Wallet
Before sending transactions, you need test ADA (lovelace) in your wallet.
- Go to the Cardano Preprod Testnet Faucet
- Paste your wallet address from the previous step
- Click "Request funds"
- Wait a few minutes for the funds to arrive
Step 5: Get a Blockfrost API Key
Mesh uses Blockfrost to interact with the Cardano blockchain for fetching UTXOs and submitting transactions.
- Sign up at Blockfrost
- Create a new project for the Preprod network
- Copy your API key (starts with
preprod)
Step 6: Send Lovelace
Now you can send lovelace to another address using MeshTxBuilder.
Key Concepts
- UTXO (Unspent Transaction Output): Cardano uses a UTXO model where each transaction consumes existing outputs and creates new ones.
- Lovelace: The smallest unit of ADA. 1 ADA = 1,000,000 lovelace.
- Change address: Where leftover funds go after covering the transaction amount and fees.
Create the Send Script
Create a new file called send-lovelace.ts:
import { BlockfrostProvider, MeshTxBuilder } from "@meshsdk/core";
import { MeshCardanoHeadlessWallet, AddressType } from "@meshsdk/wallet";
// Set up the blockchain provider with your API key
const provider = new BlockfrostProvider("YOUR_BLOCKFROST_API_KEY");
// Initialize the wallet with your mnemonic
const wallet = await MeshCardanoHeadlessWallet.fromMnemonic({
networkId: 0,
walletAddressType: AddressType.Base,
fetcher: provider,
submitter: provider,
mnemonic: [
"your", "mnemonic", "phrases", "here",
// ... all 24 words
],
});
// Get wallet data needed for the transaction
const utxos = await wallet.getUtxosMesh();
const changeAddress = await wallet.getChangeAddressBech32();
// Create the transaction
const txBuilder = new MeshTxBuilder({
fetcher: provider,
verbose: true, // Prints the transaction body for debugging
});
const unsignedTx = await txBuilder
.txOut(
"addr_test1qpvx0sacufuypa2k4sngk7q40zc5c4npl337uusdh64kv0uafhxhu32dys6pvn6wlw8dav6cmp4pmtv7cc3yel9uu0nq93swx9",
[{ unit: "lovelace", quantity: "1500000" }]
)
.changeAddress(changeAddress)
.selectUtxosFrom(utxos)
.complete();
// Sign and submit the transaction
const signedTx = await wallet.signTx(unsignedTx, false);
const txHash = await wallet.submitTx(signedTx);
console.log("Transaction hash:", txHash);Configuration
Replace these values in the script:
| Placeholder | Description |
|---|---|
YOUR_BLOCKFROST_API_KEY | Your Blockfrost preprod API key |
your, mnemonic, ... | Your 24-word mnemonic phrase |
| Recipient address | The address to send lovelace to |
Run the Transaction
Add the script to your package.json:
{
"type": "module",
"dependencies": {
"@meshsdk/core": "^1.9.0"
},
"scripts": {
"mnemonic": "node mnemonic.ts",
"send-lovelace": "node send-lovelace.ts"
}
}Run it:
npm run send-lovelaceYou see output showing the transaction body before and after coin selection, followed by the transaction hash:
Transaction hash: 62a825c607e4ca5766325c2fccd7ee98313ff81b7e8a4af67eac421b0f0866ffComplete Working Example
Here is the full project structure:
mesh-hello-world/
package.json
mnemonic.ts
send-lovelace.tspackage.json
{
"type": "module",
"dependencies": {
"@meshsdk/core": "^1.9.0"
},
"scripts": {
"mnemonic": "node mnemonic.ts",
"send-lovelace": "node send-lovelace.ts"
}
}mnemonic.ts
import { MeshCardanoHeadlessWallet, AddressType } from "@meshsdk/wallet";
const mnemonic = MeshCardanoHeadlessWallet.brew();
console.log("Your mnemonic phrases are:", mnemonic);
const wallet = await MeshCardanoHeadlessWallet.fromMnemonic({
networkId: 0,
walletAddressType: AddressType.Base,
mnemonic: mnemonic as string[],
});
const address = await wallet.getChangeAddressBech32();
console.log("Your wallet address is:", address);send-lovelace.ts
import { BlockfrostProvider, MeshTxBuilder } from "@meshsdk/core";
import { MeshCardanoHeadlessWallet, AddressType } from "@meshsdk/wallet";
const provider = new BlockfrostProvider("YOUR_BLOCKFROST_API_KEY");
const wallet = await MeshCardanoHeadlessWallet.fromMnemonic({
networkId: 0,
walletAddressType: AddressType.Base,
fetcher: provider,
submitter: provider,
mnemonic: ["your", "24", "word", "mnemonic", "phrase", "here"],
});
const utxos = await wallet.getUtxosMesh();
const changeAddress = await wallet.getChangeAddressBech32();
const txBuilder = new MeshTxBuilder({
fetcher: provider,
verbose: true,
});
const unsignedTx = await txBuilder
.txOut(
"addr_test1qpvx0sacufuypa2k4sngk7q40zc5c4npl337uusdh64kv0uafhxhu32dys6pvn6wlw8dav6cmp4pmtv7cc3yel9uu0nq93swx9",
[{ unit: "lovelace", quantity: "1500000" }]
)
.changeAddress(changeAddress)
.selectUtxosFrom(utxos)
.complete();
const signedTx = await wallet.signTx(unsignedTx, false);
const txHash = await wallet.submitTx(signedTx);
console.log("Transaction hash:", txHash);Key Concepts Explained
MeshCardanoHeadlessWallet
The MeshCardanoHeadlessWallet class manages your wallet:
| Method | Description |
|---|---|
MeshCardanoHeadlessWallet.brew() | Generates a new 24-word mnemonic |
getChangeAddressBech32() | Returns the wallet's receiving address |
getUtxosMesh() | Fetches all UTXOs owned by the wallet |
signTx(tx, partialSign) | Signs a transaction with the wallet's private key |
submitTx(tx) | Submits a signed transaction to the network |
MeshTxBuilder
The MeshTxBuilder class constructs transactions:
| Method | Description |
|---|---|
txOut(address, assets) | Adds an output to send assets to an address |
changeAddress(address) | Sets where remaining funds go |
selectUtxosFrom(utxos) | Provides UTXOs for coin selection |
complete() | Finalizes and balances the transaction |
Exercises
-
Send to multiple addresses: Modify
send-lovelace.tsto send lovelace to two different addresses in a single transaction. Use multipletxOut()calls. -
Check your balance: Before and after sending, use
wallet.getUtxos()to calculate your total balance. -
Explore the transaction: Copy your transaction hash and view it on CardanoScan Preprod.
Next Steps
You have successfully:
- Created a Cardano wallet
- Funded it with test ADA
- Sent your first transaction
In the next lesson, you learn how to build multi-signature transactions that require approval from multiple parties.