Mesh LogoMesh

Ogmios Provider

Connect to your cardano-node using Ogmios WebSocket API for transaction evaluation and submission.

Overview

Ogmios is a lightweight bridge interface for cardano-node that provides a WebSocket API enabling local clients to speak Ouroboros mini-protocols via JSON/RPC.

When to use Ogmios:

  • You run your own cardano-node and want direct access
  • You need low-latency transaction submission
  • You require self-hosted infrastructure without third-party dependencies
  • You want to evaluate Plutus script execution costs locally

Ogmios is fast, lightweight, and deploys alongside relay nodes to create entry points on the Cardano network for various types of applications.

Quick Start

Install the MeshJS package:

npm install @meshsdk/core

Initialize the provider:

import { OgmiosProvider } from "@meshsdk/core";

const provider = new OgmiosProvider("http://localhost:1337");

Evaluate and submit transactions:

import { OgmiosProvider } from "@meshsdk/core";

const provider = new OgmiosProvider("http://localhost:1337");

// Evaluate a smart contract transaction
const evaluation = await provider.evaluateTx(unsignedTx);

// Submit a signed transaction
const txHash = await provider.submitTx(signedTx);

Configuration Options

OptionTypeRequiredDescription
urlstringYesThe Ogmios server URL (WebSocket endpoint)

Running Ogmios

Ogmios requires a running cardano-node. Start Ogmios with:

ogmios --node-socket /path/to/node.socket --node-config /path/to/config.json

Or use Docker:

docker run -it \
  -v /path/to/node:/node \
  -p 1337:1337 \
  cardanosolutions/ogmios:latest \
  --node-socket /node/node.socket \
  --node-config /node/config.json

API Reference

evaluateTx

Evaluate a transaction to determine script execution costs. Use this to calculate the exact memory and CPU budget required for Plutus scripts.

await provider.evaluateTx(tx: string): Promise<Omit<Action, "data">[]>
ParameterTypeRequiredDescription
txstringYesThe unsigned transaction CBOR hex

Returns: Array of execution cost estimates for each script in the transaction.

[
  {
    index: 0,       // The redeemer index
    tag: "SPEND",   // The redeemer tag (SPEND, MINT, CERT, REWARD)
    budget: {
      mem: 1700,    // Memory units required
      steps: 368100 // CPU steps required
    }
  }
]

Example:

import { OgmiosProvider, MeshTxBuilder } from "@meshsdk/core";

const provider = new OgmiosProvider("http://localhost:1337");
const txBuilder = new MeshTxBuilder({ fetcher: provider });

// Build a transaction that interacts with a smart contract
const unsignedTx = await txBuilder
  // ... transaction building code
  .complete();

// Evaluate to get execution costs
const evaluation = await provider.evaluateTx(unsignedTx);

console.log(evaluation);
// [
//   {
//     index: 0,
//     tag: "SPEND",
//     budget: { mem: 1700, steps: 368100 }
//   }
// ]

Use the evaluation results to set precise redeemer budgets:

const redeemer = {
  data: { alternative: 0, fields: [] },
  budget: {
    mem: evaluation[0].budget.mem,
    steps: evaluation[0].budget.steps,
  },
};

submitTx

Submit a signed transaction to the Cardano network.

await provider.submitTx(tx: string): Promise<string>
ParameterTypeRequiredDescription
txstringYesThe signed transaction CBOR hex

Returns: The transaction hash.

Example:

import { OgmiosProvider } from "@meshsdk/core";

const provider = new OgmiosProvider("http://localhost:1337");

const txHash = await provider.submitTx(signedTx);
console.log(`Transaction submitted: ${txHash}`);

Complete Example

This example shows how to build, evaluate, and submit a smart contract transaction using Ogmios for evaluation and submission, combined with another provider for data fetching:

import { OgmiosProvider, MeshTxBuilder, BlockfrostProvider } from "@meshsdk/core";
import { MeshCardanoHeadlessWallet, AddressType } from "@meshsdk/wallet";

async function interactWithSmartContract() {
  // Use Ogmios for evaluation and submission
  const ogmios = new OgmiosProvider("http://localhost:1337");

  // Use another provider for data fetching (Ogmios doesn't support queries)
  const fetcher = new BlockfrostProvider("<YOUR_API_KEY>");

  // Initialize wallet
  const wallet = await MeshCardanoHeadlessWallet.fromMnemonic({
    networkId: 0,
    walletAddressType: AddressType.Base,
    fetcher: fetcher,
    submitter: ogmios,
    mnemonic: ["your", "mnemonic", "phrase", "..."],
  });

  // Get wallet data
  const utxos = await wallet.getUtxosMesh();
  const changeAddress = await wallet.getChangeAddressBech32();

  // Build transaction
  const txBuilder = new MeshTxBuilder({
    fetcher: fetcher,
    evaluator: ogmios,
  });

  const unsignedTx = await txBuilder
    .spendingPlutusScript("V3")
    .txIn(scriptUtxo.input.txHash, scriptUtxo.input.outputIndex)
    .txInInlineDatumPresent()
    .txInRedeemerValue({ alternative: 0, fields: [] })
    .txInScript(scriptCbor)
    .txOut(changeAddress, [{ unit: "lovelace", quantity: "5000000" }])
    .changeAddress(changeAddress)
    .selectUtxosFrom(utxos)
    .complete();

  // Evaluate to get precise script costs
  const evaluation = await ogmios.evaluateTx(unsignedTx);
  console.log("Script execution costs:", evaluation);

  // Sign and submit
  const signedTx = await wallet.signTx(unsignedTx, false);
  const txHash = await ogmios.submitTx(signedTx);

  console.log(`Transaction submitted: ${txHash}`);
}

Troubleshooting

Connection refused

Problem: Cannot connect to Ogmios server.

Solution:

  1. Verify Ogmios is running: curl http://localhost:1337/health
  2. Check the URL and port are correct
  3. Ensure your firewall allows connections to the Ogmios port
  4. Verify Ogmios has a working connection to the cardano-node socket

Node not synced

Problem: Transaction evaluation fails with "node not synced" error.

Solution: Wait for your cardano-node to fully sync with the network. Check sync progress:

cardano-cli query tip --mainnet

Invalid transaction

Problem: submitTx fails with validation errors.

Solution:

  1. Ensure the transaction is properly signed
  2. Verify all inputs exist and are unspent
  3. Check that fees are sufficient
  4. For script transactions, ensure redeemer budgets are adequate

Evaluation failures

Problem: evaluateTx returns errors for smart contract transactions.

Solution:

  1. Ensure all referenced UTxOs exist on-chain
  2. Verify the script CBOR is correct
  3. Check that datum and redeemer are properly formatted
  4. Ensure the transaction structure matches what the script expects

WebSocket timeout

Problem: Requests timeout without response.

Solution:

  1. Check if cardano-node is responsive
  2. Verify Ogmios has a working connection to the node socket
  3. Increase client timeout settings if evaluating complex scripts

On this page