> ## 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.

# Simulate a source

> Evaluates a source over inputs the caller supplies, with the nodes' own evaluator: the policy, transform, assertions and output encoding, exactly what a node does after its inputs were read. Each input is given as an http `body` (decoded with the source's decode block, as a node decodes what it fetched), as decoded `fields` (a chain input also gives its `block`), or as an `error` (`fetch` or `decode`). Nothing is fetched, signed, charged or recorded, so a source's logic, a policy's fallback included, can be checked against any data. A refusal at a stage is a 200 with `error`; `policy` says what the policy concluded.

Every input of the source must be given; an unknown input, a malformed field or a body over 1 MiB is 400. A private source with `{{secret.NAME}}` placeholders is 422. Rate-limited per client address together with previews (429 with `Retry-After`).

Evaluates a source over inputs you supply, with the nodes' own evaluator: the policy, transform, assertions and output encoding, exactly what a node does after it has read its inputs. Nothing is fetched, signed, charged or recorded. Use it to see what a source does with data you choose, for example a [policy's](https://docs.molpha.io/concepts/policies.md) outcome and fallback when one venue is down or out of band.

Give every input of the source exactly one way:

| Form | Meaning |
| - | - |
| `{"body": "<text>"}` (`"bodyHex": true` for hex) | An http response body, decoded with the source's own `decode` block. At most 1 MiB. A body the decode refuses is a failed input |
| `{"fields": {"<name>": {"type", "value"}}, "block": n}` | The decoded fields (`decimal`, `string`, `bool`, `bytes` as `0x` hex, `null`). A chain input is given this way, with the block or slot it was read at |
| `{"error": "fetch"}` or `{"error": "decode"}` | The input failed |

```json theme={null}
{
  "source": { "v": "1", "inputs": { "a": { "...": "..." }, "b": { "...": "..." } }, "policy": { "...": "..." } },
  "inputs": {
    "a": { "body": "{\"price\": \"65000.12\"}" },
    "b": { "error": "fetch" }
  },
  "timestampMs": 1795000000500
}
```

`timestampMs` is the round timestamp to evaluate at, unix milliseconds on the 100 ms grid; without it the gateway uses its clock. The answer has the shape of a [preview](https://docs.molpha.io/gateways/api/source-preview.md) without fetches: `inputs`, then either `value` or `error`, and `policy` for a source that has one.

An unknown or missing input, a malformed field or a timestamp off the grid is `400`; a body over 4 MiB is `413`; a private source is `422`. Simulations share the preview's per-address limit (`429`) and capacity (`503`).


## OpenAPI

````yaml gateway-openapi.yaml POST /v1/source/simulate
openapi: 3.1.0
info:
  contact: {}
  description: >-
    HTTP entry point to the Molpha oracle protocol. The gateway dispatches
    oracle rounds to nodes over libp2p, waits for threshold Schnorr signatures
    delivered directly by the nodes, and returns signed payloads. The x402
    endpoints (`/v1/x402/*`) are always served and require a configured trusted
    facilitator.
  title: Molpha Gateway API
  version: '1.0'
  x-mint:
    metadata:
      description: >-
        Run oracle rounds, sign in, preview sources and read the node set of a
        Molpha gateway.
servers:
  - url: https://gateway.molpha.io
    description: Devnet
security: []
tags:
  - name: health
    description: Liveness
  - name: gateway
    description: Gateway identity, program and authentication settings
  - name: nodes
    description: The on-chain node set
  - name: providers
    description: Integrated data providers
  - name: round
    description: Subscription and delegate rounds
  - name: session
    description: Sign-in sessions (served when `GET /v1/info` reports `sessionAuth` true)
  - name: source
    description: Unsigned source preview and simulation
  - name: x402
    description: x402 pay-per-request rounds
paths:
  /v1/source/simulate:
    post:
      tags:
        - source
      summary: Simulate a source
      description: >-
        Evaluates a source over inputs the caller supplies, with the nodes' own
        evaluator: the policy, transform, assertions and output encoding,
        exactly what a node does after its inputs were read. Each input is given
        as an http `body` (decoded with the source's decode block, as a node
        decodes what it fetched), as decoded `fields` (a chain input also gives
        its `block`), or as an `error` (`fetch` or `decode`). Nothing is
        fetched, signed, charged or recorded, so a source's logic, a policy's
        fallback included, can be checked against any data. A refusal at a stage
        is a 200 with `error`; `policy` says what the policy concluded.


        Every input of the source must be given; an unknown input, a malformed
        field or a body over 1 MiB is 400. A private source with
        `{{secret.NAME}}` placeholders is 422. Rate-limited per client address
        together with previews (429 with `Retry-After`).
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/SimulateRequest'
        description: Source and its inputs
        required: true
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/SimulateResponse'
        '400':
          description: Malformed body, source or input
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '413':
          description: Body over 4 MiB
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '422':
          description: A source the gateway does not simulate
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '429':
          description: Too many requests from this address
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '503':
          description: Previews at capacity
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
components:
  schemas:
    SimulateRequest:
      properties:
        inputs:
          additionalProperties:
            $ref: '#/components/schemas/SimulateInput'
          description: Inputs gives what every input of the source returned, by input name.
          type: object
        source:
          description: Source is a source document, as the `source` of an execute request.
          type: object
        timestampMs:
          description: >-
            TimestampMs is the round timestamp the source is evaluated at (unix
            milliseconds on the 100 ms

            grid); absent takes the gateway's clock.
          example: 1795000000500
          type: integer
      type: object
    SimulateResponse:
      properties:
        data:
          $ref: '#/components/schemas/SimulateData'
        status:
          example: ok
          type: string
      type: object
    ErrorResponse:
      properties:
        error:
          example: validation failed
          type: string
      type: object
    SimulateInput:
      properties:
        block:
          description: Block is the block or slot a chain input was read at.
          type: integer
        body:
          example: '{"price":"65000.12"}'
          type: string
        bodyHex:
          type: boolean
        error:
          description: 'Error marks the input as failed: fetch or decode.'
          enum:
            - fetch
            - decode
          type: string
        fields:
          additionalProperties:
            $ref: '#/components/schemas/SimulateField'
          description: Fields are decoded fields by name.
          type: object
      type: object
    SimulateData:
      properties:
        error:
          allOf:
            - $ref: '#/components/schemas/PreviewError'
          description: >-
            Error is the stage the source refused at; absent when it gave a
            value.
        inputs:
          items:
            $ref: '#/components/schemas/SimulatedRead'
          type: array
        policy:
          allOf:
            - $ref: '#/components/schemas/PreviewPolicy'
          description: >-
            Policy is what the source's policy concluded; absent for a source
            without a policy.
        source:
          type: object
        sourceId:
          type: string
        timestampMs:
          description: TimestampMs is the round timestamp the source was evaluated at.
          example: 1795000000500
          type: integer
        value:
          allOf:
            - $ref: '#/components/schemas/PreviewValue'
          description: >-
            Value is what nodes would sign over these inputs; absent when the
            source refused.
      type: object
    SimulateField:
      properties:
        type:
          enum:
            - decimal
            - string
            - bool
            - bytes
            - 'null'
          type: string
        value:
          type: string
      type: object
    PreviewError:
      properties:
        input:
          type: string
        message:
          type: string
        stage:
          example: fetching data
          type: string
      type: object
    SimulatedRead:
      properties:
        block:
          type: integer
        error:
          description: >-
            Error is fetch or decode when the input failed (given as an error,
            or a body its decode refused).
          type: string
        fields:
          items:
            $ref: '#/components/schemas/PreviewField'
          type: array
        kind:
          enum:
            - http
            - evm
            - starknet
            - svm
          type: string
        name:
          type: string
      type: object
    PreviewPolicy:
      properties:
        agg:
          description: Agg is the combined value; absent when the combine step failed.
          example: '65000.12'
          type: string
        candidates:
          items:
            $ref: '#/components/schemas/PreviewPolicyCandidate'
          type: array
        'n':
          description: N is the number of qualifying candidates.
          type: integer
        outcome:
          description: >-
            Outcome is conclusive or inconclusive; absent when the policy
            refused before reaching one.
          enum:
            - conclusive
            - inconclusive
          type: string
        reason:
          description: >-
            Reason is the INCONCLUSIVE reason code (0x01 insufficient sources,
            0x02 spread, 0x03 primary

            disagrees, 0x10 and up an inconclusiveIf entry); absent when
            conclusive.
          example: '0x01'
          type: string
        signed:
          description: >-
            Signed is true when the outcome was inconclusive and the fallback
            was signed: value is then the fallback.
          type: boolean
      type: object
    PreviewValue:
      properties:
        decimals:
          example: 8
          type: integer
        display:
          example: '67412.18'
          type: string
        encoded:
          example: '0x000000000000000000000000000000000000000000000000000006219ec58280'
          type: string
        preimage:
          description: >-
            Preimage is what a keccak256 output hashed, 0x hex; absent for other
            outputs.
          type: string
        type:
          example: int256
          type: string
      type: object
    PreviewField:
      properties:
        name:
          type: string
        type:
          enum:
            - decimal
            - string
            - bool
            - bytes
            - 'null'
          type: string
        value:
          type: string
      type: object
    PreviewPolicyCandidate:
      properties:
        error:
          description: Error is fetch or eval when an error disqualified the candidate.
          enum:
            - fetch
            - eval
          type: string
        errorMessage:
          type: string
        name:
          type: string
        qualified:
          type: boolean
        rule:
          description: >-
            Rule is the index of the first rule that was false; absent when none
            was.
          type: integer
        value:
          description: Value is a qualified candidate's value; absent for a chain source.
          type: string
      type: object

````

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