Use when querying the Midnight indexer for blockchain data, fetching account balances, listing transactions, reading contract state, or building data-driven DApp backends.
Connect to and query the Midnight Network indexer for blockchain data including balances, transactions, and contract state.
The Midnight indexer provides a GraphQL API for querying blockchain data. It indexes all on-chain activity and exposes it through structured queries.
| Component | Purpose |
|---|---|
| GraphQL API | Query interface for blockchain data |
| WebSocket | Real-time subscriptions (see event-subscriptions skill) |
| REST Health | Service health checks |
The indexer API has evolved through several versions. Ensure your queries are compatible with your target version.
| Version | Key Features | Breaking Changes |
|---|---|---|
| 2.0.0 | Base GraphQL schema | Initial release |
| 2.1.0 | Enhanced filters, pagination | Query parameter changes |
| 2.1.4 | Performance optimizations | None from 2.1.0 |
Midnight uses a UTXO (Unspent Transaction Output) model rather than an account model. Balance queries return the set of unspent outputs owned by an address.
Midnight supports both transparent and shielded transactions. The indexer provides different access patterns:
| Document | Description |
|---|---|
| api-versions.md | Version differences and migration guide |
| query-patterns.md | Common query patterns and optimization |
| Example | Description |
|---|---|
| balance-query/ | Query account balance and UTXOs |
| transaction-list/ | List transaction history with pagination |
| contract-state/ | Read deployed contract state |
import { createIndexerClient } from '@midnight-ntwrk/midnight-js-indexer';
const indexer = createIndexerClient({
uri: 'https://indexer.testnet.midnight.network/api/v1/graphql',
wsUri: 'wss://indexer.testnet.midnight.network/api/v1/graphql',
});
const balance = await indexer.query({
query: `
query GetBalance($address: String!) {
balance(address: $address) {
total
utxos {
txHash
outputIndex
amount
}
}
}
`,
variables: { address: 'addr_test1...' },
});
console.log('Total balance:', balance.data.balance.total);
const transactions = await indexer.query({
query: `
query GetTransactions($address: String!, $limit: Int!) {
transactions(address: $address, first: $limit) {
edges {
node {
hash
blockNumber
timestamp
inputs { address amount }
outputs { address amount }
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
`,
variables: { address: 'addr_test1...', limit: 20 },
});
interface IndexerConfig {
uri: string;
wsUri: string;
}
function getIndexerConfig(): IndexerConfig {
const network = process.env.MIDNIGHT_NETWORK || 'testnet';
const configs: Record<string, IndexerConfig> = {
testnet: {
uri: 'https://indexer.testnet.midnight.network/api/v1/graphql',
wsUri: 'wss://indexer.testnet.midnight.network/api/v1/graphql',
},
mainnet: {
uri: 'https://indexer.midnight.network/api/v1/graphql',
wsUri: 'wss://indexer.midnight.network/api/v1/graphql',
},
};
return configs[network] ?? configs.testnet;
}
async function fetchAllTransactions(
indexer: IndexerClient,
address: string
): Promise<Transaction[]> {
const allTransactions: Transaction[] = [];
let cursor: string | null = null;
let hasMore = true;
while (hasMore) {
const result = await indexer.query({
query: TRANSACTIONS_QUERY,
variables: {
address,
first: 100,
after: cursor,
},
});
const { edges, pageInfo } = result.data.transactions;
allTransactions.push(...edges.map(e => e.node));
hasMore = pageInfo.hasNextPage;
cursor = pageInfo.endCursor;
}
return allTransactions;
}
async function checkIndexerHealth(baseUrl: string): Promise<boolean> {
try {
const response = await fetch(`${baseUrl}/health`);
return response.ok;
} catch {
return false;
}
}
async function queryWithRetry<T>(
indexer: IndexerClient,
query: string,
variables: Record<string, unknown>,
maxRetries = 3
): Promise<T> {
let lastError: Error | null = null;
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
return await indexer.query({ query, variables });
} catch (error) {
lastError = error as Error;
if (attempt < maxRetries) {
await new Promise(r => setTimeout(r, 1000 * attempt));
}
}
}
throw lastError;
}
event-subscriptions - Real-time WebSocket event streamsmidnight-tooling:contract-calling - Invoking contracts that change statemidnight-dapp:state-management - Frontend state synchronization with indexer/midnight-tooling:check - Verify indexer connectivity