openapi: 3.1.0
info:
  title: SQD Portal API - Substrate Dataset Endpoints
  description: API endpoints for interacting with Substrate datasets under the SQD Portal, specifically for the Polkadot dataset.
  version: 1.0.0
servers:
  - url: https://portal.sqd.dev/datasets/polkadot
    description: SQD Portal's endpoint for Polkadot
paths:
  /metadata:
    get:
      summary: Dataset Metadata
      x-mint:
        href: "/en/api/substrate/metadata"
      x-codeSamples:
        - lang: shell
          source: |
            curl --compressed 'https://portal.sqd.dev/datasets/polkadot/metadata'
      description: "Retrieves dataset metadata: name, aliases, start block, and real-time status."
      responses:
        "200":
          description: Dataset metadata response
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/DatasetMetadata"
  /stream:
    post:
      summary: Stream Blocks
      x-mint:
        href: "/en/api/substrate/stream"

      x-codeSamples:
        - lang: shell
          label: Query Balances.Transfer Events
          source: |
            curl --compressed -X POST 'https://portal.sqd.dev/datasets/polkadot/stream' \
              -H 'Content-Type: application/json' \
              -d '{
                "type": "substrate",
                "fromBlock": 20000000,
                "toBlock": 20000100,
                "fields": {
                  "block": {
                    "number": true,
                    "timestamp": true
                  },
                  "event": {
                    "name": true,
                    "args": true
                  }
                },
                "events": [{
                  "name": ["Balances.Transfer"]
                }]
              }'
        - lang: shell
          label: Query Calls with Extrinsics
          source: |
            curl --compressed -X POST 'https://portal.sqd.dev/datasets/polkadot/stream' \
              -H 'Content-Type: application/json' \
              -d '{
                "type": "substrate",
                "fromBlock": 20000000,
                "toBlock": 20000100,
                "fields": {
                  "block": {
                    "number": true,
                    "timestamp": true
                  },
                  "call": {
                    "name": true,
                    "args": true,
                    "success": true
                  },
                  "extrinsic": {
                    "hash": true,
                    "fee": true,
                    "success": true
                  }
                },
                "calls": [{
                  "name": ["Balances.transfer_keep_alive"],
                  "extrinsic": true
                }]
              }'
        - lang: shell
          label: Query EVM.Log Events (Frontier)
          source: |
            curl --compressed -X POST 'https://portal.sqd.dev/datasets/moonbeam-substrate/stream' \
              -H 'Content-Type: application/json' \
              -d '{
                "type": "substrate",
                "fromBlock": 5000000,
                "toBlock": 5000100,
                "fields": {
                  "block": {
                    "number": true,
                    "timestamp": true
                  },
                  "event": {
                    "name": true,
                    "args": true
                  }
                },
                "evmLogs": [{
                  "topic0": ["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"]
                }]
              }'
      description: "Returns blocks matching the query. Substrate datasets contain only finalized blocks."
      requestBody:
        description: "Data query to filter and retrieve blocks. Request body may be gzipped (Content-Encoding: gzip)."
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/DataQuery"
            examples:
              balances-transfer:
                summary: Query Balances.Transfer Events
                value:
                  type: substrate
                  fromBlock: 20000000
                  toBlock: 20000100
                  fields:
                    block:
                      number: true
                      timestamp: true
                    event:
                      name: true
                      args: true
                  events:
                    - name:
                        - "Balances.Transfer"
              calls-with-extrinsics:
                summary: Query Calls with Extrinsics
                value:
                  type: substrate
                  fromBlock: 20000000
                  toBlock: 20000100
                  fields:
                    block:
                      number: true
                      timestamp: true
                    call:
                      name: true
                      args: true
                      success: true
                    extrinsic:
                      hash: true
                      fee: true
                      success: true
                  calls:
                    - name:
                        - "Balances.transfer_keep_alive"
                      extrinsic: true
              evm-logs:
                summary: Query EVM.Log Events (Frontier)
                value:
                  type: substrate
                  fromBlock: 5000000
                  toBlock: 5000100
                  fields:
                    block:
                      number: true
                      timestamp: true
                    event:
                      name: true
                      args: true
                  evmLogs:
                    - topic0:
                        - "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
      responses:
        "200":
          description: A stream of blocks in JSON lines format, optionally gzipped. Can be empty if the data query has a bounded range and all blocks in the range have been skipped. May include X-Sqd-Finalized-Head-Number and X-Sqd-Finalized-Head-Hash headers, indicating the latest finalized block that's on the same chain as the returned blocks.
          headers:
            X-Sqd-Finalized-Head-Number:
              schema:
                type: integer
                format: int64
              description: Block number of the latest finalized block. Returned blocks can be above, at, or below this block number.
              required: false
            X-Sqd-Finalized-Head-Hash:
              schema:
                type: string
              description: Hash of the latest finalized block. All returned blocks are guaranteed to belong to the same (not necessarily final) chain as this block.
              required: false
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: "#/components/schemas/Block"
        "204":
          description: Indicates that the requested block range is entirely above the range of blocks available in the dataset. The portal may wait for up to 5s before returning this.
          content:
            text/plain:
              schema:
                type: string
          headers:
            X-Sqd-Finalized-Head-Number:
              schema:
                type: integer
                format: int64
              description: Block number of the latest finalized block.
              required: false
            X-Sqd-Finalized-Head-Hash:
              schema:
                type: string
              description: Hash of the latest finalized block.
              required: false
        "400":
          description: >
            Possible causes: (1) request headers or body encoding are incorrect; (2) the query is invalid - the response will include an explanation; (3) fromBlock is below the dataset's start_block (see /metadata).
          content:
            text/plain:
              schema:
                type: string
        "404":
          description: Dataset not found
          content:
            text/plain:
              schema:
                type: string
        "409":
          description: Conflict due to a mismatched parent block hash. This will happen when the client requests a range starting with an orphaned block in the real-time setting. The response contains a list of previous blocks belonging to the current chain; the client should find a block matching one of these (by both hash and block number) in its records and restart the stream and its business logic from that block.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ConflictResponse"
        "429":
          description: Too many requests; rate limit exceeded. May include a Retry-After header indicating the number of seconds to wait before retrying the request.
          content:
            text/plain:
              schema:
                type: string
          headers:
            Retry-After:
              schema:
                type: integer
              description: Seconds to wait before retrying
        "500":
          description: Internal server error. Do not retry requests causing these.
          content:
            text/plain:
              schema:
                type: string
        "503":
          description: The server could not process the request at the moment. The client should retry the request later. May include a Retry-After header indicating the number of seconds to wait before retrying the request.
          content:
            text/plain:
              schema:
                type: string
          headers:
            Retry-After:
              schema:
                type: integer
              description: Seconds to wait before retrying
  /finalized-stream:
    post:
      summary: Stream Finalized Blocks
      x-mint:
        href: "/en/api/substrate/finalized-stream"

      x-codeSamples:
        - lang: shell
          label: Query Balances.Transfer Events
          source: |
            curl --compressed -X POST 'https://portal.sqd.dev/datasets/polkadot/finalized-stream' \
              -H 'Content-Type: application/json' \
              -d '{
                "type": "substrate",
                "fromBlock": 20000000,
                "toBlock": 20000100,
                "fields": {
                  "block": {
                    "number": true,
                    "timestamp": true
                  },
                  "event": {
                    "name": true,
                    "args": true
                  }
                },
                "events": [{
                  "name": ["Balances.Transfer"]
                }]
              }'
        - lang: shell
          label: Query Calls with Extrinsics
          source: |
            curl --compressed -X POST 'https://portal.sqd.dev/datasets/polkadot/finalized-stream' \
              -H 'Content-Type: application/json' \
              -d '{
                "type": "substrate",
                "fromBlock": 20000000,
                "toBlock": 20000100,
                "fields": {
                  "block": {
                    "number": true,
                    "timestamp": true
                  },
                  "call": {
                    "name": true,
                    "args": true,
                    "success": true
                  },
                  "extrinsic": {
                    "hash": true,
                    "fee": true,
                    "success": true
                  }
                },
                "calls": [{
                  "name": ["Balances.transfer_keep_alive"],
                  "extrinsic": true
                }]
              }'
      description: "Returns only finalized blocks matching the query. Same request format as /stream."
      requestBody:
        description: Data query to filter and retrieve blocks
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/DataQuery"
            examples:
              balances-transfer:
                summary: Query Balances.Transfer Events
                value:
                  type: substrate
                  fromBlock: 20000000
                  toBlock: 20000100
                  fields:
                    block:
                      number: true
                      timestamp: true
                    event:
                      name: true
                      args: true
                  events:
                    - name:
                        - "Balances.Transfer"
              calls-with-extrinsics:
                summary: Query Calls with Extrinsics
                value:
                  type: substrate
                  fromBlock: 20000000
                  toBlock: 20000100
                  fields:
                    block:
                      number: true
                      timestamp: true
                    call:
                      name: true
                      args: true
                      success: true
                    extrinsic:
                      hash: true
                      fee: true
                      success: true
                  calls:
                    - name:
                        - "Balances.transfer_keep_alive"
                      extrinsic: true
      responses:
        "200":
          description: A stream of finalized blocks in JSON lines format, optionally gzipped. Never returns blocks above the finalized head.
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: "#/components/schemas/Block"
        "204":
          description: Indicates that the requested block range is entirely above the range of blocks available in the dataset. The portal may wait for up to 5s before returning this.
          content:
            text/plain:
              schema:
                type: string
        "400":
          description: >
            Possible causes: (1) request headers or body encoding are incorrect; (2) the query is invalid - the response will include an explanation; (3) fromBlock is below the dataset's start_block (see /metadata).
          content:
            text/plain:
              schema:
                type: string
        "404":
          description: Dataset not found
          content:
            text/plain:
              schema:
                type: string
        "429":
          description: Too many requests; rate limit exceeded. May include a Retry-After header indicating the number of seconds to wait before retrying the request.
          content:
            text/plain:
              schema:
                type: string
          headers:
            Retry-After:
              schema:
                type: integer
              description: Seconds to wait before retrying
        "500":
          description: Internal server error. Do not retry requests causing these.
          content:
            text/plain:
              schema:
                type: string
        "503":
          description: The server could not process the request at the moment. The client should retry the request later. May include a Retry-After header indicating the number of seconds to wait before retrying the request.
          content:
            text/plain:
              schema:
                type: string
          headers:
            Retry-After:
              schema:
                type: integer
              description: Seconds to wait before retrying
  /head:
    get:
      summary: Dataset Head
      x-mint:
        href: "/en/api/substrate/head"

      x-codeSamples:
        - lang: shell
          source: |
            curl --compressed 'https://portal.sqd.dev/datasets/polkadot/head'
      description: Returns the highest available block number and hash.
      responses:
        "200":
          description: Block head info
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/BlockHead"
  /finalized-head:
    get:
      summary: Finalized Dataset Head
      x-mint:
        href: "/en/api/substrate/finalized-head"

      x-codeSamples:
        - lang: shell
          source: |
            curl --compressed 'https://portal.sqd.dev/datasets/polkadot/finalized-head'
      description: Returns the highest available finalized block number and hash.
      responses:
        "200":
          description: Finalized block head info
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/BlockHead"
  /timestamps/{timestamp}/block:
    get:
      summary: Block at Timestamp
      x-mint:
        href: "/en/api/substrate/timestamp-block"
      x-codeSamples:
        - lang: shell
          label: Resolve timestamp to block
          source: |
            curl 'https://portal.sqd.dev/datasets/polkadot/timestamps/1700000000/block'
        - lang: python
          label: Resolve timestamp to block
          source: |
            import requests

            response = requests.get(
                "https://portal.sqd.dev/datasets/polkadot/timestamps/1700000000/block"
            )
            block_number = response.json()["block_number"]
        - lang: javascript
          label: Resolve timestamp to block
          source: |
            const response = await fetch("https://portal.sqd.dev/datasets/polkadot/timestamps/1700000000/block");
            const { block_number } = await response.json();
      description: >
        Resolves a Unix timestamp (in seconds) to a block number. Returns the first block whose
        timestamp is greater than or equal to `timestamp`. Resolution prefers archival data and
        falls back to the real-time source when available; the `x-sqd-data-source` response header
        reports which source served the result (`network` or `real_time`).
      parameters:
        - name: timestamp
          in: path
          required: true
          schema:
            type: integer
            format: int64
            default: 1700000000
            example: 1700000000
          description: Unix timestamp in seconds.
      responses:
        "200":
          description: Block number resolved.
          headers:
            x-sqd-data-source:
              schema:
                type: string
                enum: [network, real_time]
              description: Source that served the result.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/BlockNumberResponse"
              examples:
                "polkadot":
                  summary: First block at or after the timestamp
                  value:
                    block_number: 18169650
        "404":
          description: No block found at or after the given timestamp.
        "500":
          description: Internal server error. Do not retry.
        "503":
          description: Upstream data source temporarily unavailable. Retry later.
components:
  schemas:
    BlockNumberResponse:
      type: object
      properties:
        block_number:
          type: integer
          format: int64
          description: Number of the first block whose timestamp is greater than or equal to the requested timestamp.
          example: 18000000
      required:
        - block_number
    DatasetMetadata:
      type: object
      properties:
        dataset:
          type: string
          description: The default name used to reference this dataset
        aliases:
          type: array
          items:
            type: string
          description: Alternative names for the dataset
        real_time:
          type: boolean
          description: Indicates if the dataset has real-time data
        start_block:
          type: integer
          format: int64
          description: The block number of the first known block
      required:
        - dataset
        - aliases
        - real_time
    DataQuery:
      type: object
      properties:
        type:
          type: string
          enum: ["substrate"]
          default: "substrate"
        fromBlock:
          type: integer
          format: int64
          description: The number of the first block to fetch. If unsure how far into the past this can go consult /metadata
          default: 20000000
        toBlock:
          type: integer
          format: int64
          description: "The block number to fetch up to (inclusive). Optional; if omitted, streams until dataset height or timeout."
          default: 20000100
        parentBlockHash:
          type: string
          description: Expected hash of the parent of the first requested block
        includeAllBlocks:
          type: boolean
          description: If true, includes blocks with no matching data
          default: false
        fields:
          type: object
          description: Field selector. See the HTTP 200 response description for details on fields that aren't self-explanatory
          properties:
            block:
              type: object
              description: Field selector for block headers (the .header objects of JSON block records)
              properties:
                number:
                  type: boolean
                hash:
                  type: boolean
                parentHash:
                  type: boolean
                stateRoot:
                  type: boolean
                extrinsicsRoot:
                  type: boolean
                digest:
                  type: boolean
                  description: Block digest as JSON object
                specName:
                  type: boolean
                  description: Runtime spec name
                specVersion:
                  type: boolean
                  description: Runtime spec version
                implName:
                  type: boolean
                  description: Runtime implementation name
                implVersion:
                  type: boolean
                  description: Runtime implementation version
                validator:
                  type: boolean
                  description: Block validator/author address
                timestamp:
                  type: boolean
                  description: Block timestamp in milliseconds since Unix epoch
            extrinsic:
              type: object
              description: Field selector for extrinsics
              properties:
                index:
                  type: boolean
                version:
                  type: boolean
                success:
                  type: boolean
                hash:
                  type: boolean
                fee:
                  type: boolean
                  description: Extrinsic fee as a decimal string
                tip:
                  type: boolean
                  description: Extrinsic tip as a decimal string
                signature:
                  type: boolean
                  description: Extrinsic signature as JSON object
                error:
                  type: boolean
                  description: Extrinsic error as JSON object (if failed)
            call:
              type: object
              description: Field selector for calls
              properties:
                extrinsicIndex:
                  type: boolean
                address:
                  type: boolean
                  description: Call address within the extrinsic call tree (array of integers)
                name:
                  type: boolean
                  description: "Qualified call name (e.g. Balances.transfer_keep_alive)"
                success:
                  type: boolean
                args:
                  type: boolean
                  description: Call arguments as JSON
                origin:
                  type: boolean
                  description: Call origin as JSON
                error:
                  type: boolean
                  description: Call error as JSON (if failed)
            event:
              type: object
              description: Field selector for events
              properties:
                index:
                  type: boolean
                extrinsicIndex:
                  type: boolean
                name:
                  type: boolean
                  description: "Qualified event name (e.g. Balances.Transfer)"
                phase:
                  type: boolean
                  description: Event phase (ApplyExtrinsic, Finalization, Initialization)
                callAddress:
                  type: boolean
                  description: Address of the call that emitted this event
                topics:
                  type: boolean
                args:
                  type: boolean
                  description: Event arguments as JSON
          default:
            block:
              number: true
              hash: true
              parentHash: true
            event:
              name: true
            call:
              name: true
        events:
          type: array
          description: Event data requests. An empty object matches all events; an empty array matches no events
          items:
            type: object
            properties:
              name:
                description: "Filter by qualified event name (e.g. Balances.Transfer)"
                type: array
                items:
                  type: string
              extrinsic:
                description: Fetch parent extrinsics for matching events
                type: boolean
              call:
                description: Fetch the call that emitted each matching event
                type: boolean
              stack:
                description: Fetch the full call stack (all parent calls) for matching events
                type: boolean
        calls:
          type: array
          description: Call data requests. An empty object matches all calls; an empty array matches no calls
          items:
            type: object
            properties:
              name:
                description: "Filter by qualified call name (e.g. Balances.transfer_keep_alive)"
                type: array
                items:
                  type: string
              subcalls:
                description: Fetch all child calls (subcalls) for matching calls
                type: boolean
              extrinsic:
                description: Fetch parent extrinsics for matching calls
                type: boolean
              stack:
                description: Fetch all parent calls in the call tree
                type: boolean
              events:
                description: Fetch all events emitted by matching calls
                type: boolean
        evmLogs:
          type: array
          description: "EVM.Log event requests for Frontier EVM parachains (e.g. Moonbeam, Astar). Filters on EVM.Log events specifically"
          items:
            type: object
            properties:
              address:
                description: Filter by EVM contract address (lowercase hex)
                type: array
                items:
                  type: string
              topic0:
                description: Filter by first topic (event signature hash)
                type: array
                items:
                  type: string
              topic1:
                description: Filter by second topic
                type: array
                items:
                  type: string
              topic2:
                description: Filter by third topic
                type: array
                items:
                  type: string
              topic3:
                description: Filter by fourth topic
                type: array
                items:
                  type: string
              extrinsic:
                description: Fetch parent extrinsics for matching events
                type: boolean
              call:
                description: Fetch the call that emitted each matching event
                type: boolean
              stack:
                description: Fetch the full call stack for matching events
                type: boolean
        ethereumTransactions:
          type: array
          description: "Ethereum.transact call requests for Frontier EVM parachains. Filters on Ethereum.transact calls specifically"
          items:
            type: object
            properties:
              to:
                description: Filter by destination EVM contract address (lowercase hex)
                type: array
                items:
                  type: string
              sighash:
                description: Filter by function signature hash (4-byte hex)
                type: array
                items:
                  type: string
              extrinsic:
                description: Fetch parent extrinsics for matching calls
                type: boolean
              stack:
                description: Fetch all parent calls in the call tree
                type: boolean
              events:
                description: Fetch all events emitted by matching calls
                type: boolean
        contractsEvents:
          type: array
          description: "Contracts.ContractEmitted event requests for ink! smart contracts. Contract addresses must be hexadecimal (decoded from SS58) and lowercase"
          items:
            type: object
            properties:
              contractAddress:
                description: Filter by contract address (hexadecimal, lowercase)
                type: array
                items:
                  type: string
              extrinsic:
                description: Fetch parent extrinsics for matching events
                type: boolean
              call:
                description: Fetch the call that emitted each matching event
                type: boolean
              stack:
                description: Fetch the full call stack for matching events
                type: boolean
        gearMessagesEnqueued:
          type: array
          description: "Gear.UserMessageEnqueued event requests for Gear/Vara networks"
          items:
            type: object
            properties:
              programId:
                description: Filter by Gear program ID
                type: array
                items:
                  type: string
              extrinsic:
                description: Fetch parent extrinsics for matching events
                type: boolean
              call:
                description: Fetch the call that emitted each matching event
                type: boolean
              stack:
                description: Fetch the full call stack for matching events
                type: boolean
        gearUserMessagesSent:
          type: array
          description: "Gear.UserMessageSent event requests for Gear/Vara networks"
          items:
            type: object
            properties:
              programId:
                description: Filter by Gear program ID
                type: array
                items:
                  type: string
              extrinsic:
                description: Fetch parent extrinsics for matching events
                type: boolean
              call:
                description: Fetch the call that emitted each matching event
                type: boolean
              stack:
                description: Fetch the full call stack for matching events
                type: boolean
        reviveContractEmitted:
          type: array
          description: "Revive.ContractEmitted event requests for Revive smart contracts"
          items:
            type: object
            properties:
              contract:
                description: Filter by contract address
                type: array
                items:
                  type: string
              topic0:
                description: Filter by first topic
                type: array
                items:
                  type: string
              topic1:
                description: Filter by second topic
                type: array
                items:
                  type: string
              topic2:
                description: Filter by third topic
                type: array
                items:
                  type: string
              topic3:
                description: Filter by fourth topic
                type: array
                items:
                  type: string
              extrinsic:
                description: Fetch parent extrinsics for matching events
                type: boolean
              call:
                description: Fetch the call that emitted each matching event
                type: boolean
              stack:
                description: Fetch the full call stack for matching events
                type: boolean
      required:
        - fromBlock
    Block:
      type: object
      properties:
        header:
          type: object
          properties:
            number:
              type: integer
              format: uint64
            hash:
              type: string
            parentHash:
              type: string
            stateRoot:
              type: string
            extrinsicsRoot:
              type: string
            digest:
              type: object
              description: Block digest containing consensus engine data
            specName:
              type: string
              description: Runtime spec name
            specVersion:
              type: integer
              description: Runtime spec version
            implName:
              type: string
              description: Runtime implementation name
            implVersion:
              type: integer
              description: Runtime implementation version
            validator:
              type: string
              description: Block validator/author address
            timestamp:
              type: integer
              format: uint64
              description: Block timestamp in milliseconds since Unix epoch
        extrinsics:
          type: array
          items:
            type: object
            properties:
              index:
                type: integer
                description: Index of the extrinsic in the block
              version:
                type: integer
              success:
                type: boolean
              hash:
                type: string
                description: Extrinsic hash
              fee:
                type: string
                description: Extrinsic fee as a decimal string
              tip:
                type: string
                description: Extrinsic tip as a decimal string
              signature:
                type: object
                description: Extrinsic signature data
              error:
                type: object
                description: Error data if the extrinsic failed
        calls:
          type: array
          items:
            type: object
            properties:
              extrinsicIndex:
                type: integer
                description: Index of the parent extrinsic
              address:
                type: array
                items:
                  type: integer
                description: Call address within the extrinsic call tree
              name:
                type: string
                description: "Qualified call name (e.g. Balances.transfer_keep_alive)"
              success:
                type: boolean
              args:
                type: object
                description: Call arguments
              origin:
                type: object
                description: Call origin
              error:
                type: object
                description: Error data if the call failed
        events:
          type: array
          items:
            type: object
            properties:
              index:
                type: integer
                description: Index of the event in the block
              extrinsicIndex:
                type: integer
                description: Index of the parent extrinsic
              callAddress:
                type: array
                items:
                  type: integer
                description: Address of the call that emitted this event
              name:
                type: string
                description: "Qualified event name (e.g. Balances.Transfer)"
              phase:
                type: string
                description: Event phase (ApplyExtrinsic, Finalization, Initialization)
              topics:
                type: array
                items:
                  type: string
              args:
                type: object
                description: Event arguments
      required:
        - header
    BlockHead:
      type: object
      properties:
        number:
          type: integer
          format: int64
          description: Block number of the highest available block
        hash:
          type: string
      nullable: true
    ConflictResponse:
      type: object
      properties:
        previousBlocks:
          type: array
          items:
            type: object
            properties:
              number:
                type: integer
                format: int64
              hash:
                type: string
            required:
              - number
              - hash
      required:
        - previousBlocks
