# @cosmjs/tendermint-rpc Agent Guide

> **Package**: `@cosmjs/tendermint-rpc` v0.38.0 **Purpose**: Tendermint/CometBFT
> RPC client **Layer**: Transport Layer

## Package Overview

Low-level RPC client for Tendermint/CometBFT nodes. Supports HTTP and WebSocket
connections.

### Key Features

- **HTTP Client**: Request/response RPC
- **WebSocket Client**: Streaming subscriptions
- **Event Subscriptions**: Real-time block and transaction events
- **Multiple Versions**: Supports Tendermint 0.37, CometBFT 0.38, CometBFT 1.x

## Key Exports

| Export               | Purpose                               |
| -------------------- | ------------------------------------- |
| `Tendermint37Client` | Tendermint 0.37 client                |
| `Tendermint38Client` | CometBFT 0.38 client                  |
| `CometClient`        | Generic client (auto-detects version) |
| `connectComet()`     | Connect to any compatible version     |
| `HttpEndpoint`       | HTTP endpoint configuration           |
| `WebsocketClient`    | WebSocket client for subscriptions    |

## Common Usage

### Basic Connection

```typescript
import { connectComet } from "@cosmjs/tendermint-rpc";

const client = await connectComet("https://rpc.cosmos.network");

// Query status
const status = await client.status();

// Get block
const block = await client.block(height);

// Get latest block
const latestBlock = await client.block();

// Query transaction
const tx = await client.tx({ hash: txHash });

// Search transactions
const txs = await client.txSearch({ query: "tx.height=1000" });
```

### WebSocket Subscriptions

```typescript
import { WebsocketClient } from "@cosmjs/tendermint-rpc";

const client = new WebsocketClient("wss://rpc.cosmos.network/websocket");

// Subscribe to new blocks
const stream = client.listen({
  jsonrpc: "2.0",
  method: "subscribe",
  params: { query: "tm.event='NewBlock'" },
});

stream.addListener({
  next: (event) => {
    console.log("New block:", event.data.value.block.header.height);
  },
  error: (err) => console.error(err),
  complete: () => console.log("Stream ended"),
});

// Cleanup
client.disconnect();
```

### HTTP Endpoint Configuration

```typescript
import { HttpEndpoint } from "@cosmjs/tendermint-rpc";

const endpoint: HttpEndpoint = {
  url: "https://rpc.cosmos.network",
  headers: {
    Authorization: "Bearer token",
  },
};

const client = await connectComet(endpoint);
```

## Common Patterns

### Pattern: Block Polling

```typescript
async function pollBlocks(client, callback) {
  let lastHeight = 0;

  setInterval(async () => {
    const status = await client.status();
    const currentHeight = status.syncInfo.latestBlockHeight;

    if (currentHeight > lastHeight) {
      const block = await client.block(currentHeight);
      callback(block);
      lastHeight = currentHeight;
    }
  }, 1000);
}
```

### Pattern: Transaction Monitoring

```typescript
const stream = client.listen({
  jsonrpc: "2.0",
  method: "subscribe",
  params: { query: `tm.event='Tx' AND transfer.recipient='${address}'` },
});

stream.addListener({
  next: (event) => {
    const tx = event.data.value.TxResult;
    console.log("Received transaction:", tx.hash);
  },
});
```

---

**Last Updated**: January 6, 2026 **Package Version**: 0.38.0


