Last active
October 14, 2021 05:45
-
-
Save buddies2705/f0fbc1a4fa2a0835c7e3758f34cabb30 to your computer and use it in GitHub Desktop.
Using GraphQL with Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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