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

# Signed reads

> Get a template's value threshold-signed by the Molpha nodes, paid with x402 or a subscription session, and verify it on Solana, EVM or Starknet.

`GET /v1/data/<template>?<params>` is a round. The gateway derives the template's source (or reuses one it derived
in the last 10 minutes for the same parameters), then hands the request to its execute route exactly as if you had
posted that source yourself:

| You send | The round runs on | Paid by |
| - | - | - |
| No `Authorization` header (the default) | `POST /v1/x402/execute` | One x402 USDC payment |
| `Authorization: Bearer <session token>` | `POST /v1/round/execute` | One round of your subscription's quota |

The execute route's answer is passed on unchanged, with one added header, `Molpha-Source-Id`, naming the derived
source. The gateway uses its current registry version.

## Choose the quorum

`signaturesRequired` is the one query parameter that is not a template parameter. It defaults to **3**.

```bash theme={null}
curl -i "https://gateway.molpha.io/v1/data/lst/rate?chain=ethereum&token=0x7f39c581f595b53c5cb19bd0b3f8da6c935e2ca0&signaturesRequired=3"
```

* It must be a whole number from 1 to 255 (`400 PARAM_INVALID` otherwise). The round is then refused with `400` for a
  quorum below the protocol minimum of 3 or above the registry's node count, and on a subscription with `403` above
  your plan's maximum.
* A tolerance-mode source needs at least 3.
* It is not part of the `sourceId`, but it is part of the round, the x402 price and the signed message. On Solana a
  feed account is keyed by `sourceId`, `signaturesRequired` and the submitter, so pick one quorum per feed.

## Pay with x402

Without a session the first answer is the x402 quote:

```bash theme={null}
curl -i "https://gateway.molpha.io/v1/data/token/supply?chain=ethereum&token=0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"
# HTTP/2 402
# molpha-source-id: 0x86a196b1…
# payment-required: eyJ4NDAy…
```

1. The `402` carries the x402 v2 requirements in the `PAYMENT-REQUIRED` header (base64) and in the body: Solana
   devnet USDC, the amount, `payTo` (the protocol treasury), `extra.feePayer` and `extra.memo`. The memo commits to
   the source, the quorum and the registry version.
2. Sign a payment that matches the quote exactly, as [x402 pay-per-request](https://docs.molpha.io/access-models/x402.md) describes.
3. Send the same `GET` again with `PAYMENT-SIGNATURE: <base64 payment payload>`. The gateway verifies the payment
   before it dispatches the round, and answers `200` with the attestation and a `PAYMENT-RESPONSE` settlement receipt.

The price is `0.05 + 0.01 × (signaturesRequired + redundancy buffer)` USDC on this deployment: 0.10 USDC at the
default quorum of 3. Always pay what the `402` quotes.

## Pay with a subscription session

Sign in once with the wallet of the subscription owner or of one of its delegates
([`POST /v1/session/challenge`](https://docs.molpha.io/gateways/api/session-challenge), then [`POST /v1/session`](https://docs.molpha.io/gateways/api/session-create)),
and send the token as a bearer token:

```bash theme={null}
curl "https://gateway.molpha.io/v1/data/token/supply?chain=ethereum&token=0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48" \
  -H "Authorization: Bearer $MOLPHA_SESSION"
```

Each value uses one round of your plan's quota. An unknown, expired or revoked token is `401` at no cost. See
[Subscriptions](https://docs.molpha.io/access-models/subscriptions.md).

## Sign with RequestAuth

Template routes do not take RequestAuth, because a RequestAuth signature covers the `sourceId`, which you only learn
from `/source`. Take the source from `/source` and request the round like any other source. With the SDK:

```ts theme={null}
import { web3 } from "@anchor-lang/core";
import { MolphaSDK, parseSource } from "@molpha/sdk";
import { walletFromKeypairFile } from "@molpha/sdk/utils";

const sdk = new MolphaSDK({
  connection: new web3.Connection("https://api.devnet.solana.com", "confirmed"),
  wallet: walletFromKeypairFile("~/.config/solana/id.json"), // the subscription owner or a delegate
});

const url =
  "https://gateway.molpha.io/v1/data/token/supply/source" +
  "?chain=ethereum&token=0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48";
const derived = await (await fetch(url)).json();

// Re-hash the source yourself: never trust a sourceId you did not compute.
const source = parseSource(derived.canonical);
if (source.idHex !== derived.sourceId) throw new Error("sourceId mismatch");

const result = await sdk.gateway.requestSignedData({ source, signaturesRequired: 3 });
const { signature, feed } = await sdk.solana.submitAttestation(result);
```

Without the SDK, post the source with RequestAuth (`authSig`, `authTimestamp`, `consumerAuthority`,
`subscriptionOwner`, `signaturesRequired`, `registryVersion`) to [`POST /v1/round/execute`](https://docs.molpha.io/gateways/api/round-execute).

> [!WARNING]
>
> The `@molpha/sdk/data` entry (`MolphaDataClient`) in `@molpha/sdk` 0.3.0 was written for a standalone Molpha Data
> service at `data.molpha.io`, which is not deployed. Use the gateway routes on this page.

## The response

A `200` is the execute routes' `AttestationResponse`:

```json theme={null}
{
  "status": "completed",
  "data": {
    "attestation": {
      "payload": {
        "sourceId": "0x86a196b1…",
        "value": "…",
        "timestamp": 1796074598400,
        "registryVersion": 5,
        "signaturesRequired": 3
      },
      "signature": { "signature": "…", "commitment": "…", "signersBitmap": "…" }
    },
    "value": "…",
    "fresh": true,
    "roundId": "…"
  }
}
```

| Field | Signed | Meaning |
| - | - | - |
| `data.attestation.payload.sourceId` | Yes | The template's source: equal to `Molpha-Source-Id` and to the `sourceId` from `/source` |
| `data.attestation.payload.value` | Yes | The 32-byte value, encoded by the source's output (see the template's `returns`) |
| `data.attestation.payload.timestamp` | Yes | Unix **milliseconds**, the start of the round's 100 ms tick, set by the gateway. Use `timestamp / 1000` against chain clocks and `maxAge` |
| `data.attestation.payload.registryVersion`, `signaturesRequired` | Yes | The node set and the quorum the round was signed under |
| `data.attestation.signature` | | The aggregate Schnorr signature: `signature`, `commitment`, `signersBitmap` |
| `data.value` | No | The signed value rendered as a decimal. Display only |
| `data.roundId` | No | The round's ID, the key of its record in the [round archive](https://indexer.molpha.io) |
| `data.valuePreimage` | No | For a `keccak256` output (the snapshot templates): the bytes that hash to `payload.value`. This is the only copy |

For a snapshot, check `keccak256(valuePreimage) == payload.value`, then decode the preimage with the `layout` from
`/source`.

## Verify it

A template's attestation is an ordinary Molpha attestation: verify it on Solana, an EVM chain or Starknet as
[Verify and publish](https://docs.molpha.io/verifiers/overview.md) describes. The SDK's verifier helpers take its `Attestation` type, whose
signature fields are named differently from the wire. Map them like this:

```ts theme={null}
import { buildEvmVerifierArgs, type Attestation } from "@molpha/sdk";

const res = await fetch(templateUrl, { headers: { Authorization: `Bearer ${session}` } });
const { data } = await res.json();

const attestation: Attestation = {
  payload: data.attestation.payload,
  signature: {
    s: data.attestation.signature.signature,
    commitmentAddr: data.attestation.signature.commitment,
    signersBitmap: data.attestation.signature.signersBitmap,
  },
  value: data.value,
  fresh: data.fresh,
  ...(data.valuePreimage ? { valuePreimage: data.valuePreimage } : {}),
};

const { attestation: evmAttestation, maxAge } = buildEvmVerifierArgs(attestation, { maxAge: 300 });
// or: await sdk.solana.submitAttestation(attestation);
```

A valid signature does not replace your own policy. Check that `payload.sourceId` is the source you derived and
re-hashed, that `signaturesRequired` is the quorum you require, and that `timestamp / 1000` is fresh enough for
you. A gateway response proves neither settlement nor on-chain verification.

## Errors

The value route answers with the execute routes' `{status, error}` body once the round starts, and with the
[Molpha Data error body](https://docs.molpha.io/data/overview.md#errors) for a failure before it.

| HTTP | When | What to do |
| - | - | - |
| 400 | The round request was refused, for example a quorum below the minimum; or a malformed query | Fix the request |
| 401 | The session token is unknown, expired or revoked | Sign in again |
| 402 | No session and no payment: the x402 quote | Pay and resend with `PAYMENT-SIGNATURE` |
| 403 | The subscription is inactive, the delegate is not authorized, the quorum exceeds the plan's maximum, or the quota is spent | Check the subscription |
| 409 | You already have a round for this source and quorum in this 100 ms tick, or the payment was already used | Retry once after one tick (100 ms plus a small jitter). An unused payment stays usable |
| 422 | A template parameter is wrong, or the template does not run on that chain | Fix the parameters |
| 429 | Too many new derivations from this address, or too many rounds for this consumer | Retry after `Retry-After` |
| 503 | No quorum could form, the gateway is at capacity, a fact could not be read, or the nodes refused the data | Retry after `Retry-After`. For x402, read [Retries and failures](https://docs.molpha.io/access-models/x402.md#retries-and-failures) first: a dispatched round can spend the payment |

There is no idempotency key: every retry that reaches the nodes is a new round that spends another quota unit or
needs another payment.


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