Skip to content

Interacting With The API

Rhys Bartels-Waller edited this page Apr 24, 2020 · 8 revisions

Interacting with the API

The API is served over TCP, leveraging the existing infrastructure of HTTP. GraphQL adds a powerful query layer to give you complete control over the request, including the shape of the data in the response. Detailed below are some approaches for connecting and interacting with the API, however there may be other ways more suited for your use-case.

Setup up a local stack using docker-compose, or use a public deployment.

Command Line

➜  curl -X POST \
-H "Content-Type: application/json" \
-d '{"query": "{ cardano { blockHeight }}"}' \
http://localhost:3100/graphql  

{"data":{"cardano":{"blockHeight":70205}}}

Request within app

const query = `
  query getTransactions(
    $limit: Int,
    $offset: Int,
    $where: Transaction_bool_exp
  ) {
     transactions(
      limit: $limit,
      offset: $offset,
      where: $where
    ) {
      fee
      block {
        number
      }
      id
    }
  }`

const limit = 100
const offset = 2
const where = {
  block: {
    number: {
      _gte: 50
      _lt: 100
    }
  }
}

fetch('/graphql', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
  },
  body: JSON.stringify({
    query,
    variables: { limit, offset, where },
  })
})
  .then(r => r.json())
  .then(data => console.log(`Page ${offset} containing ${limit} transactions:', data))

Stateful client

A full example application will demonstrate this.

Clone this wiki locally