> ## Documentation Index
> Fetch the complete documentation index at: https://nexus.gerowallet.io/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Midnight Indexer (GraphQL)

> Point midnight-js at the Nexus Midnight indexer: one query URL, one subscription URL, your API key

Nexus serves the Midnight indexer's GraphQL API over HTTP and WebSocket. The official Midnight SDK
(midnight-js) and any other GraphQL client can use it with a Nexus API key.

Requests are passed to the indexer as sent and its answers come back unchanged, so the schema is the
indexer's own (API v4).

## The two URLs

Replace `<network>` with `midnight-mainnet`, `midnight-preprod` or `midnight-preview`, and use an
API key created for that same network. A key reads the indexer of its own network only.

| Use | URL |
| - | - |
| Queries (HTTP POST) | `https://nexus.gerowallet.io/api/midnight/<network>/indexer/graphql?api_key=nxs_your_api_key` |
| Subscriptions (WebSocket) | `wss://nexus.gerowallet.io/api/midnight/<network>/indexer/ws?api_key=nxs_your_api_key` |

The key goes in the URL because midnight-js takes two URLs and cannot set a request header. A client
that can set headers may send `X-Api-Key` instead, on either URL, and leave the parameter out.

> [!WARNING]
>
> A URL with `api_key` in it is a credential. Keep it in server-side configuration, do not commit it,
> and remove the query string before you log the URL. Do not ship it in a web page: a browser dApp
> should take its endpoints from the user's wallet. `api_key` is read on these two routes only.

## midnight-js

Tested with `@midnightntwrk/midnight-js-indexer-public-data-provider` 5.0.3.

```typescript theme={null}
import { indexerPublicDataProvider } from '@midnightntwrk/midnight-js-indexer-public-data-provider';

const key = process.env.NEXUS_API_KEY; // a key created for Midnight Preprod
const base = 'nexus.gerowallet.io/api/midnight/midnight-preprod/indexer';

const publicDataProvider = indexerPublicDataProvider({
  queryURL: `https://${base}/graphql?api_key=${key}`,
  subscriptionURL: `wss://${base}/ws?api_key=${key}`,
});

const tip = await publicDataProvider.queryBlock();
const protocolVersion = await publicDataProvider.queryLatestProtocolVersion();
```

If you build a `MidnightNetwork` object, these are its `indexer.queryURL` and
`indexer.subscriptionURL`. Nexus does not serve the Midnight node RPC, so `nodeURL` and `nodeWsURL`
keep pointing at your node provider.

## Plain GraphQL

```bash theme={null}
curl -X POST "https://nexus.gerowallet.io/api/midnight/midnight-preprod/indexer/graphql" \
  -H "X-Api-Key: nxs_your_api_key" \
  -H "Content-Type: application/json" \
  -d '{"query":"query { block { height hash } }"}'
```

The WebSocket speaks the `graphql-transport-ws` protocol (the one the `graphql-ws` library
implements). Offer `graphql-transport-ws` in `Sec-WebSocket-Protocol`; Nexus answers with it.
midnight-js also offers `graphql-transport-ws+deflate`; Nexus selects the plain protocol, so frames
arrive as text. A handshake that offers only other sub-protocols is refused with 400.

## What counts against your plan

| | Counted as |
| - | - |
| Each GraphQL query (HTTP POST) | One request: monthly quota and per-second rate limit, as for any endpoint |
| Opening a WebSocket | One request, the same way. A handshake that is then refused with 400 or 403 has already been counted |
| Subscription data on an open WebSocket | Not counted against the monthly quota. Shown in your usage as WebSocket messages, one per frame you receive, keep-alive replies included |

No add-on is needed: the indexer is part of every plan.

## WebSocket limits

These apply per API key, on top of your plan.

| Limit | Value | When you pass it |
| - | - | - |
| Open connections per key | 5 | The new connection is closed with code 1008, reason `connection_cap_exceeded` |
| Frames you send | 20 per second (burst 60) per connection | Closed with code 4429, reason `traffic_cap_exceeded` |
| Data you receive | 2 MiB per second (burst 16 MiB) per connection | Closed with code 4429, reason `traffic_cap_exceeded` |

midnight-js opens one connection per provider and sends a keep-alive every 10 seconds, far below
these, and a contract's state or events stream stays well under the data limit. A subscription that
replays a long history in one go, such as wallet transactions or ledger events from an early
index, can receive more than 2 MiB per second for longer than the burst covers, and is then closed.
Code 4429 is final for `graphql-ws`: the subscription reports an error instead of reconnecting, so
resume from the last position you received. If your workload needs higher limits, contact
[support](https://nexus.gerowallet.io/support).

An open connection is also closed when its key stops being valid, checked every five minutes:

| Close code | Reason | Meaning |
| - | - | - |
| 4401 | `api_key_revoked` | The key was deleted, disabled or has expired, or its account is disabled |
| 4403 | `subscription_inactive` | The account's subscription is no longer active |

## Refused requests

| Status | When |
| - | - |
| 400 | GraphQL URL: the key was created for another Midnight network than the one in the URL (`error` is `Network Mismatch`). WebSocket: the handshake offers sub-protocols and none of them is `graphql-transport-ws` |
| 401 | No key, or the key is not valid. On the GraphQL URL the body has `errorCode` `MISSING_API_KEY` or `INVALID_API_KEY` |
| 402 | The account has no active subscription |
| 403 | The key cannot be used here: it belongs to another chain. On the WebSocket also: it was created for another Midnight network than the one in the URL, or it is a public-partner key |
| 404 | The network in the URL is not one Nexus serves |
| 429 | Monthly quota or rate limit reached. See [Rate limits](https://nexus.gerowallet.io/docs/concepts/rate-limits.md) |

A refused WebSocket handshake answers with the HTTP status; `ws` and browsers report it as a failed
connection.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.