Getting Started with Javascript

In this guide we will show you how to create a basic setup so that you can benefit from the dfuse GraphQL API with Javascript.

1. Getting an API Key

Start by obtaining an API key. You can get one here.

2. Adding the Client Library

The simplest way to query the dfuse GraphQL API is by using the dfuse JS client library.

Here are a few of its key features:

  • Handles API token issuance
  • Refreshes your API token upon expiration
  • Automatically reconnects if the connection closes

You can add it to your project using Yarn or NPM.


npm install @dfuse/client

yarn add @dfuse/client

3. Initializing the dfuse Client

With the initial setup completed, you can start coding. The first thing we will do is initialize the dfuse client, using the API key you created in the first step and you’ll now specify the Ethereum API Endpoint or EOSIO API Endpoint you want to connect to.


const { createDfuseClient } = require("@dfuse/client")

const client = createDfuseClient({
  apiKey: 'web_abcdef12345678900000000000', // Your API key goes here
  network: 'mainnet.eth.dfuse.io' // Specify the API endpoint you want to connect to here
})

4. Crafting our GraphQL Query

Next, we you create our GraphQL query. Here, you will use a GraphQL subscription to stream results as they come. You will use the searchTransactionsForward operation, with the "receiver:eosio.token action:transfer" query (See the Search Query Language reference here).


const query = `
  subscription {
    searchTransactionsForward(query: "receiver:eosio.token action:transfer") {
      cursor
      trace {
        matchingActions {
          json
        }
      }
    }
  }
`

5. Executing our Query

Finally, you can combine the dfuse client instance we created in step 3 with the GraphQL query you created in step 4 to start streaming the results of our query.

The function passed as the 2nd parameter to client.graphql() will be called every time a new result is returned by the API.


client.graphql(query, (message) => {
  if (message.type === "data") {
    console.log(message.data);
  }
})