How to use GraphQL with Python, Javascript, and Ruby

In the last few articles, we talked argued using GraphQL APIs for blockchain data and how to create your first blockchain GraphQL query. In this article, we will show how to call GraphQL APIs using different programming languages. 

However, you should always use our GraphQL playground to create and test your queries before embedding in your code.

Basics

Before starting, let’s cover some basics.

  • All GraphQL requests are POST method.
  • You also need API Key, which you get after signing up on our GraphQL IDE.
  • The API key must be included as an HTTP header in every GraphQL request. The header name is X-API-KEY
  • In GraphQL APIs endpoint remain the same for all the requests
  • Our GraphQL endpoint is — https://graphql.bitquery.io/

We will be using the following GraphQL query in all our examples.

query{
bitcoin{
blocks{
count
}
}
}

Getting API Key

To get the API Key, sign up on our GraphQL IDE and then get the API key, which you can find under you account.

bitquery Graphql API key
Bitquery Graphql API key

The API key must be included as an HTTP header in every GraphQL request. The header name is X-API-KEY

Now let’s see how to call GraphQL APIs using different programming languages.

GraphQL APIs using cURL

cURL allows using HTTP methods directly through our terminal. It is best for quick testing of any API request.

curl \
  -X POST \
  -H "Content-Type: application/json" \
  -H "X-API-KEY: YOU API KEY" \
  --data '{ "query": "{ bitcoin { blocks {count} } }" }' \
https://graphql.bitquery.io/

GraphQL APIs using Python

Python is a beginner-friendly programming language. The example below shows how to call a GraphQL API using python.

GraphQL Python Libraries 

The libraries below are the most widely used libraries using GraphQL with python.

  • Graphene — Graphene is an opinionated Python library for building GraphQL schemas/types fast and easily.
  • Ariadne — Ariadne is a Python library for implementing GraphQL servers using a schema-first approach.
  • Strawberry — Strawberry is a new GraphQL library for Python 3, inspired by data classes.

GraphQL APIs using Javascript

Javascript is one of the most adopted programming languages among developers all over the world. You can call a post request to get results from GraphQL APIs.

GraphQL Javascript(NodeJS) Libraries

The following are the most popular javascript libraries for GraphQL.

  • GraphQL.js — The JavaScript reference implementation for GraphQL, a query language for APIs created by Facebook.
  • graphql-request — Minimal GraphQL client supporting Node and browsers for scripts or simple apps.
  • Apollo client — Apollo Client is a fully-featured caching GraphQL client with integrations for React, Angular, and more. It allows you to build UI components that fetch data via GraphQL easily.

GraphQL APIs using Ruby

Ruby is also one of the most simple programming languages. The following example shows how to call a GraphQL API using ruby. 

GraphQL Ruby Libraries

You can use the libraries below to use GraphQL with ruby.

  • graphql-ruby — Ruby implementation of Facebook’s GraphQL.
  • graphql-client — A Ruby library for declaring, composing, and executing GraphQL queries.
  • graphql-batch — A query batching executor for the graphql gem.
  • agoo — Ruby web server that implements Facebook’s GraphQL.
  • GQLi — A GraphQL client and DSL. Allowing to write queries in native Ruby.

Additional Resources

To see the examples of other programming languages, check the official website of GraphQL. In addition, to learn more about more GraphQL tools and libraries, check Awesome GraphQL.

If you have any questions or need help with your blockchain investigation, just hop on our Telegram channel. Also, let us know if you are looking for blockchain data APIs.

You might also be interested in:

About Bitquery

Bitquery is a set of software tools that parse, index, access, search, and use information across blockchain networks in a unified way. Our products are:

  • Digital Assets API provides index information related to all major cryptocurrencies, coins, and tokens.
  • DEX API provides real-time deposits and transactions, trades, and other related data on different DEX protocols like Uniswap, Kyber Network, Airswap, Matching Network, etc.

If you have any questions about our products, ask them on our Telegram channel or email us at hello@bitquery.io. Also, subscribe to our newsletter below, we will keep you updated with the latest in the cryptocurrency world.

Privacy Preferences
When you visit our website, it may store information through your browser from specific services, usually in form of cookies. Here you can change your privacy preferences. Please note that blocking some types of cookies may impact your experience on our website and the services we offer.
Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
blank

The Ultimate Guide to NFT Analytics

Bitquery provides NFT APIs as-a-Service with access to a wide range of data, including sales data, ownership data, and transfer data. In this article, let’s focus on a breakdown of the analytics that are possible with the Bitquery NFT APIs.

Read More »