Skip to content

Instantly share code, notes, and snippets.

@buddies2705
Last active October 14, 2021 05:45
Show Gist options
  • Save buddies2705/f0fbc1a4fa2a0835c7e3758f34cabb30 to your computer and use it in GitHub Desktop.
Save buddies2705/f0fbc1a4fa2a0835c7e3758f34cabb30 to your computer and use it in GitHub Desktop.
Using GraphQL with Python
#!/usr/bin/python
# -*- coding: utf-8 -*-
import requests
def run_query(query): # A simple function to use requests.post to make the API call.
headers = {'X-API-KEY': 'YOUR API KEY'}
request = requests.post('https://graphql.bitquery.io/',
json={'query': query}, headers=headers)
if request.status_code == 200:
return request.json()
else:
raise Exception('Query failed and return code is {}. {}'.format(request.status_code,
query))
# The GraphQL query
query = """
query{
bitcoin{
blocks{
count
}
}
}
"""
result = run_query(query) # Execute the query
print 'Result - {}'.format(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment