
How to use GraphQL with Python, Javascript, and Ruby
In the last few articles, we argued for using GraphQL APIs for blockchain data and showed how to create your first blockchain GraphQL query. In this article, we will show how to call GraphQL APIs from different programming languages.
Always build and test your queries in the Bitquery IDE before embedding them in code. The IDE can also generate code snippets for you in several languages.
Basics
Before starting, let’s cover some basics.
- All GraphQL requests are POST requests.
- You need an OAuth access token, which you generate from your Bitquery account or through the IDE. See the token generation docs for the steps.
- The token goes in the
Authorizationheader of every request:Authorization: Bearer <token>. - The endpoint stays the same for all requests:
https://streaming.bitquery.io/graphql. Some chains are served fromhttps://streaming.bitquery.io/eap.
We will use the following query in all examples. It fetches the latest Ethereum block:
{
EVM(network: eth) {
Blocks(limit: {count: 1}, orderBy: {descending: Block_Number}) {
Block {
Number
Time
}
}
}
}
Getting an access token
Sign up at account.bitquery.io, open the API section, and generate an access token. The same flow is available from the IDE. Include the token as a header in every request:
Authorization: Bearer <YOUR_ACCESS_TOKEN>
Now let’s call the API from different languages.
GraphQL APIs using cURL
cURL lets you use HTTP methods directly from the terminal. It is the quickest way to test any API request.
curl -X POST https://streaming.bitquery.io/graphql \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
--data '{"query": "{ EVM(network: eth) { Blocks(limit: {count: 1}, orderBy: {descending: Block_Number}) { Block { Number Time } } } }"}'
GraphQL APIs using Python
Python is a beginner-friendly programming language. The example below uses the requests library.
import requests
URL = "https://streaming.bitquery.io/graphql"
TOKEN = "YOUR_ACCESS_TOKEN"
def run_query(query):
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {TOKEN}",
}
response = requests.post(URL, json={"query": query}, headers=headers)
if response.status_code == 200:
return response.json()
raise Exception(f"Query failed with status {response.status_code}: {response.text}")
query = """
{
EVM(network: eth) {
Blocks(limit: {count: 1}, orderBy: {descending: Block_Number}) {
Block {
Number
Time
}
}
}
}
"""
result = run_query(query)
print(f"Result - {result}")
GraphQL Python Libraries
The libraries below are the most widely used GraphQL libraries in 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 GraphQL library for Python 3, inspired by data classes.
- gql — A GraphQL client for Python with sync and async transports, including WebSocket support for subscriptions.
GraphQL APIs using Javascript
Javascript is one of the most adopted programming languages in the world. The built-in fetch API (available in browsers and Node.js 18+) is all you need.
const query = `
{
EVM(network: eth) {
Blocks(limit: {count: 1}, orderBy: {descending: Block_Number}) {
Block {
Number
Time
}
}
}
}
`;
const url = "https://streaming.bitquery.io/graphql";
const opts = {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer YOUR_ACCESS_TOKEN",
},
body: JSON.stringify({ query }),
};
fetch(url, opts)
.then((res) => res.json())
.then(console.log)
.catch(console.error);
GraphQL Javascript(NodeJS) Libraries
The following are the most popular javascript libraries for GraphQL.
- GraphQL.js — The JavaScript reference implementation for GraphQL.
- 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 and more. It allows you to build UI components that fetch data via GraphQL easily.
- graphql-ws — A client and server implementation of the GraphQL over WebSocket protocol, useful for Bitquery subscriptions.
GraphQL APIs using Ruby
Ruby keeps things short. The following example uses only the standard library.
require 'net/http'
require 'uri'
require 'json'
uri = URI.parse("https://streaming.bitquery.io/graphql")
headers = {
'Content-Type' => 'application/json',
'Authorization' => 'Bearer YOUR_ACCESS_TOKEN'
}
query = "{ EVM(network: eth) { Blocks(limit: {count: 1}, orderBy: {descending: Block_Number}) { Block { Number Time } } } }"
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.request_uri, headers)
request.body = { query: query }.to_json
response = http.request(request)
puts "Body: #{response.body}"
GraphQL Ruby Libraries
You can use the libraries below to use GraphQL with ruby.
- graphql-ruby — Ruby implementation of 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 GraphQL.
- GQLi — A GraphQL client and DSL. Allowing to write queries in native Ruby.
Beyond request-response
Everything above is a one-shot HTTP query. If your application needs data as it happens, the same GraphQL syntax works as a subscription over WebSocket: swap query for subscription and connect to the WebSocket endpoint. For high-throughput use cases, Kafka and gRPC streams push raw on-chain data without polling. The docs have working subscription examples for each language.
Additional Resources
To see examples in other programming languages, check the official GraphQL website. To learn 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:
- Create your first Blockchain GraphQL query
- API to get Ethereum Smart Contract Events
- Why GraphQL is better for blockchain data APIs
- APIs to get Latest Uniswap Pair Listing
- Simple rest APIs to get Uniswap data (DEX Data APIs)
- API to Get Ethereum Token Balance
- Simple API To Get Ethereum Supply And Data
- How to investigate an Ethereum address?
- Bitcoin Taproot – A Technical Explanation
- Who is actually using Ethereum?
- How to get newly created Ethereum Tokens?
- Querying Binance Smart Chain (BSC)
About Bitquery
Bitquery provides blockchain data APIs and streams across more than 40 chains. The current lineup includes GraphQL APIs and subscriptions for historical and real-time data, Kafka and gRPC data streams for low-latency pipelines, an MCP server that lets AI agents query on-chain data directly, and Coinpath MoneyFlow for fund-flow tracing and investigations.
If you have any questions about our products, ask them on our Telegram channel. Also, subscribe to our newsletter below, we will keep you updated with the latest in the cryptocurrency world.
Subscribe to our newsletter
Subscribe and never miss any updates related to our APIs, new developments & latest news etc. Our newsletter is sent once a week on Monday.


