BLOG/TUTORIAL

Getting Started with the GraphQL API

January 28, 2024

A comprehensive guide to querying blockchain data with ChainVue's type-safe GraphQL API.

Why GraphQL?

GraphQL lets you request exactly the data you need—no more, no less. Unlike REST APIs where you get fixed response shapes, GraphQL puts you in control.

Need just the block height and hash? Ask for that. Need the full block with all transactions and their inputs/outputs? One query.

Your First Query

Let's start with something simple—getting the current sync state:

query {
  syncState(chainId: "iJhCezBExJHvtyH3fGhNnt2NhU4Ztkf2yq") {
    lastBlockHeight
    isHealthy
    lastSyncedAt
  }
}

Making Requests

The GraphQL endpoint is:

https://api.chainvue.io/graphql

Send POST requests with your query in the body:

curl -X POST https://api.chainvue.io/graphql \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{"query": "{ syncState(chainId: \"iJhCezBExJHvtyH3fGhNnt2NhU4Ztkf2yq\") { lastBlockHeight } }"}'

Query Variables

Use variables to keep your queries reusable:

query GetBlock($chainId: String!, $height: BigInt!) {
  block(chainId: $chainId, height: $height) {
    height
    hash
    time
    txCount
  }
}

# Variables:
{
  "chainId": "iJhCezBExJHvtyH3fGhNnt2NhU4Ztkf2yq",
  "height": 689000
}

Nested Queries

GraphQL lets you fetch related data in a single request:

query {
  block(chainId: "...", height: 689000) {
    height
    hash
    transactions {
      txid
      valueOut
      vout {
        value
        addresses
      }
    }
  }
}

Error Handling

GraphQL errors are returned in the response body:

{
  "errors": [
    {
      "message": "Block not found",
      "extensions": {
        "code": "NOT_FOUND"
      }
    }
  ],
  "data": null
}

Common error codes:

  • NOT_FOUND — Resource doesn't exist
  • UNAUTHORIZED — Invalid or missing API key
  • RATE_LIMITED — Exceeded rate limit
  • VALIDATION_ERROR — Invalid input

Introspection

GraphQL APIs are self-documenting. Use any GraphQL client (like Apollo Studio or GraphQL Playground) to explore the schema interactively.

Next Steps