Migrate from Lucid to Mesh
Step-by-step migration guide from Lucid to Mesh SDK with API mapping, code examples, and common patterns.
Overview
In this guide, you migrate an existing Cardano application from Lucid to Mesh SDK. Mesh separates transaction building, wallet interaction, and blockchain queries into distinct components, giving you more flexibility in how you structure your application.
What you will learn
- How Lucid concepts map to Mesh equivalents
- Key differences in API design and architecture
- Step-by-step migration patterns for common operations
- How to update tests and smart contract code
Prerequisites
- An existing application using Lucid
- Node.js 18+ installed
- Basic TypeScript knowledge
Time to complete
2 hours (varies based on application complexity)
Quick Start
Install Mesh packages alongside your existing Lucid installation:
npm install @meshsdk/core @meshsdk/wallet @meshsdk/reactYou can migrate incrementally, using both libraries during the transition.
Step-by-Step Guide
Step 1: Understand the architectural differences
Lucid combines provider and wallet in a single configured instance:
// Lucid pattern
const lucid = await Lucid.new(
new Blockfrost("https://cardano-preprod.blockfrost.io/api", "key"),
"Preprod"
);
await lucid.selectWallet(walletApi);
const tx = await lucid.newTx().payToAddress(...).complete();Mesh separates these concerns:
// Mesh pattern
import { BlockfrostProvider, TxBuilder } from "@meshsdk/core";
import { BrowserWallet } from "@meshsdk/wallet";
const provider = new BlockfrostProvider("key");
const wallet = await BrowserWallet.enable("eternl");
const builder = new TxBuilder({ network: "preprod", provider }); // holds no wallet: each build names its payerWhy this matters: You can swap providers without changing wallet code, and test transactions without blockchain connectivity.
Step 2: Install Mesh packages
Add Mesh to your project:
npm install @meshsdk/core @meshsdk/wallet @meshsdk/reactFor React applications, @meshsdk/react provides wallet components and hooks.
What to expect: Both Lucid and Mesh can coexist during migration.
Step 3: Replace provider setup
Lucid:
import { Blockfrost, Lucid } from "lucid-cardano";
const lucid = await Lucid.new(
new Blockfrost("https://cardano-preprod.blockfrost.io/api", "project_key"),
"Preprod"
);Mesh:
import { BlockfrostProvider } from "@meshsdk/core";
// Preprod
const provider = new BlockfrostProvider("project_key_preprod");
// Mainnet
const providerMainnet = new BlockfrostProvider("project_key_mainnet");
// Preview
const providerPreview = new BlockfrostProvider("project_key_preview");Mesh auto-detects the network from your API key prefix. Alternative providers:
import { KoiosProvider, MaestroProvider, OgmiosProvider } from "@meshsdk/core";
const koios = new KoiosProvider("preprod");
const maestro = new MaestroProvider({ apiKey: "key", network: "Preprod" });
const ogmios = new OgmiosProvider("ws://localhost:1337");What to expect: Provider instances ready to pass to transaction builders.
Step 4: Update wallet connection
Lucid:
const api = await window.cardano.eternl.enable();
lucid.selectWallet(api);
const address = await lucid.wallet.address();
const utxos = await lucid.wallet.getUtxos();Mesh:
import { BrowserWallet } from "@meshsdk/wallet";
const wallet = await BrowserWallet.enable("eternl");
const address = await wallet.getChangeAddress();
const usedAddresses = await wallet.getUsedAddresses();
const utxos = await wallet.getUtxos();React hooks (Mesh):
import { CardanoWallet, useWallet } from "@meshsdk/react";
function WalletConnect() {
const { wallet, connected, connect } = useWallet();
if (!connected) {
return <CardanoWallet />;
}
return <div>Connected</div>;
}What to expect: Wallet instances with CIP-30 standard methods.
Step 5: Migrate transaction building
Lucid payment transaction:
const tx = await lucid
.newTx()
.payToAddress("addr_test1...", { lovelace: 5000000n })
.complete();
const signedTx = await tx.sign().complete();
const txHash = await signedTx.submit();Mesh payment transaction:
import { output, TxBuilder } from "@meshsdk/core";
const builder = new TxBuilder({ network: "preprod", provider });
const built = await builder.build(
output({ address: "addr_test1...", value: { lovelace: 5_000_000n } }),
{ payer: wallet }, // the wallet's UTxOs pay, and the change goes back to it
);
await built.sign();
await built.submit({ submitter: wallet });
const txHash = built.txHash;Key differences:
- Mesh builds a transaction from a list of parts (
output,mint.native,spend.plutus) - The builder holds no wallet: each
build()names itspayer, which pays and gets the change build()signs and sends nothing:built.sign()andbuilt.submit()work on the same objectbuilt.txHashis known before signing
What to expect: Transaction hashes on successful submission.
Step 6: Migrate native token minting
Lucid:
const { paymentCredential } = lucid.utils.getAddressDetails(address);
const mintingPolicy = lucid.utils.nativeScriptFromJson({
type: "sig",
keyHash: paymentCredential.hash,
});
const policyId = lucid.utils.mintingPolicyToId(mintingPolicy);
const tx = await lucid
.newTx()
.mintAssets({ [policyId + tokenName]: 1n })
.validTo(Date.now() + 100000)
.attachMintingPolicy(mintingPolicy)
.complete();Mesh:
import {
TxBuilder,
ForgeScript,
mint,
output,
resolveScriptHash,
stringToHex,
} from "@meshsdk/core";
const address = await wallet.getChangeAddress();
const forgingScript = ForgeScript.withOneSignature(address);
const policyId = resolveScriptHash(forgingScript);
const tokenNameHex = stringToHex("MyToken");
const builder = new TxBuilder({ network: "preprod", provider });
const built = await builder.build(
[
mint.native({ script: forgingScript, assets: { [tokenNameHex]: 1n } }),
output({ address, value: { [policyId + tokenNameHex]: 1n } }),
],
{ payer: wallet },
);
await built.sign(); // the wallet signs for its UTxOs and for the policy
await built.submit({ submitter: wallet });
const txHash = built.txHash;What to expect: NFTs minted with Mesh's ForgeScript utilities.
Step 7: Migrate smart contract interactions
Lucid locking funds:
const tx = await lucid
.newTx()
.payToContract(scriptAddress, { inline: datum }, { lovelace: 5000000n })
.complete();Mesh locking funds:
const built = await builder.build(
output({
address: scriptAddress,
value: { lovelace: 5_000_000n },
datum: { type: "inline", data: datum },
}),
{ payer: wallet },
);Lucid unlocking funds:
const tx = await lucid
.newTx()
.collectFrom([utxo], redeemer)
.attachSpendingValidator(validator)
.complete();Mesh unlocking funds:
import { guard, output, spend } from "@meshsdk/core";
const built = await builder.build(
[
spend.plutus({
utxo, // the datum is read from the output
script: { code: scriptCbor, version: "V2" },
redeemer: { data: redeemer },
}),
output({ address: recipientAddress, value: utxo.output.amount }),
guard.pubKey({ keyHash: pubKeyHash }),
],
{ payer: wallet }, // collateral comes from the wallet's UTxOs
);
await built.sign(); // partial signing is the defaultWhat to expect: Smart contract transactions with explicit script and datum handling.
Step 8: Migrate data serialization
Lucid:
import { Data } from "lucid-cardano";
const datum = Data.to({ owner: pubKeyHash });
const parsed = Data.from(onchainDatum);Mesh:
import { serializePlutusData, deserializePlutusData } from "@meshsdk/core";
import type { Data } from "@meshsdk/core";
// For simple datums, use the Data type directly
const datum: Data = {
alternative: 0,
fields: [pubKeyHash],
};
// For complex serialization
const cbor = serializePlutusData(datum);
const parsed = deserializePlutusData(onchainDatum);What to expect: CBOR-encoded datum and redeemer values.
Step 9: Migrate tests
Lucid Emulator:
import { Emulator, Lucid } from "lucid-cardano";
const emulator = new Emulator([{ address, assets: { lovelace: 100_000_000n } }]);
const lucid = await Lucid.new(emulator);Mesh testing:
import { TxBuilder } from "@meshsdk/core";
import type { UTxO } from "@meshsdk/core";
// Mock blockchain state: the UTxOs the payer holds
const utxos: UTxO[] = [
{
input: { txHash: "abc...", outputIndex: 0 },
output: { address, amount: [{ unit: "lovelace", quantity: "100000000" }] },
},
];
// Offline: nothing is read, so give the protocol parameters yourself
const builder = new TxBuilder({ network: "preprod", params, costModels });
const built = await builder.build(parts, { payer: { changeAddress: address, utxos } });For local development, use Yaci:
import { YaciProvider } from "@meshsdk/core";
const yaci = new YaciProvider("http://localhost:8080");What to expect: Test environments without live blockchain connectivity.
API Mapping Reference
| Lucid | Mesh | Notes |
|---|---|---|
Lucid.new() | new BlockfrostProvider() + new TxBuilder() | Separate provider and builder |
lucid.selectWallet() | BrowserWallet.enable() | Returns wallet instance |
lucid.newTx() | A list of parts | One builder for every transaction |
tx.payToAddress() | output() | Output construction |
tx.payToContract() | output() with datum | With datum |
tx.mintAssets() | mint.native() or mint.plutus() | Minting |
tx.collectFrom() | spend.plutus() | Script spending |
tx.attachSpendingValidator() | script of spend.plutus() | Attach validator |
tx.complete() | builder.build(parts, { payer }) | Build transaction |
tx.sign().complete() | built.sign() | built.signedTx is the signed CBOR |
signedTx.submit() | built.submit() | Submit to network |
Data.to() | serializePlutusData() | Serialize datum |
Data.from() | deserializePlutusData() | Parse datum |
Emulator | TxBuilder with params and your own UTxOs | Testing without a provider |
Complete Example
Here is a complete migration example for a payment transaction:
Before (Lucid):
import { Blockfrost, Lucid } from "lucid-cardano";
async function sendPayment() {
const lucid = await Lucid.new(
new Blockfrost("https://cardano-preprod.blockfrost.io/api", "key"),
"Preprod"
);
const api = await window.cardano.eternl.enable();
lucid.selectWallet(api);
const tx = await lucid
.newTx()
.payToAddress("addr_test1...", { lovelace: 5000000n })
.complete();
const signedTx = await tx.sign().complete();
const txHash = await signedTx.submit();
console.log("TX:", txHash);
}After (Mesh):
import { BlockfrostProvider, output, TxBuilder } from "@meshsdk/core";
import { BrowserWallet } from "@meshsdk/wallet";
async function sendPayment() {
const provider = new BlockfrostProvider("key_preprod");
const wallet = await BrowserWallet.enable("eternl");
const builder = new TxBuilder({ network: "preprod", provider });
const built = await builder.build(
output({ address: "addr_test1...", value: { lovelace: 5_000_000n } }),
{ payer: wallet },
);
await built.sign();
await built.submit({ submitter: wallet });
console.log("TX:", built.txHash);
}Troubleshooting
Type errors with amounts
Cause: Lucid uses BigInt for amounts, and so does TxBuilder: a string amount such as { lovelace: "5000000" } is INVALID_VALUE.
Solution: Keep amounts as bigint when migrating:
// Lucid
{ lovelace: 5000000n }
// Mesh
{ lovelace: 5_000_000n }Transaction signing fails
Cause: A key the transaction needs has not signed, so submit() throws MISSING_SIGNATURES and sends nothing.
Solution: built.sign() signs partially by default and takes no flag. Look at built.missing, and give the signer of each key it lists to built.sign({ signers }):
await built.sign(); // the payer signs the keys it holds
console.log(built.missing); // the keys still to sign, each with its rolesMissing change address error
Cause: The build has no payer, so it throws MISSING_PAYER. The payer gives the change address.
Solution: Always give the payer:
const built = await builder.build(parts, { payer: wallet }); // change goes to the wallet's change addressScript validation fails
Cause: Datum or redeemer structure does not match contract expectations.
Solution: Verify your Data structure matches the Aiken/Plutus types:
const datum: Data = {
alternative: 0, // Constructor index
fields: [value1, value2], // Field values
};UTxO not found errors
Cause: UTxOs fetched at different times may be spent.
Solution: Give the wallet as the payer, so that its UTxOs are read when the transaction is built:
// The wallet's UTxOs are read by build() itself
const built = await builder.build(parts, { payer: wallet });Related Links
- Transaction Builder API
- Smart Contract Transactions
- BrowserWallet API
- Providers Documentation
- Mesh Discord for migration support
Mint an NFT Collection on Cardano
Create native NFT assets on Cardano using Mesh SDK and Bun. Learn CIP-25 metadata, native scripts, and transaction building.
Mesh vs Cardano SDK Alternatives
Compare Mesh SDK with cardano-serialization-lib, Lucid, Pallas, and other Cardano development tools to choose the right one for your project.