Access GraphQL API via Python client


Hey everyone,

there already is a sample app that queries the Github GraphQL API using a Ruby library Code.
To do so, it’s required to provide the schema as JSON and your personal access token.

Do you know whether it’s possible to access the Github GraphQL API using Python?
I found Graphene for example but I’m not sure how to provide the schema and how it’s done with the personal access token.

Thanks a lot and Best Regards


:wave: Hi @WGierke!

I’m not very familiar with Graphene, but I believe it’s a Python library made for creating GraphQL schemas, not consuming them.

The GraphQL Client that the code you linked to is relying on makes it easier in Ruby to embed fragments in Rails views and access the response of a GraphQL query as methods.

Taking a step back, you can execute GraphQL queries by any HTTP library. Such a request (in the form of cURL) would look like so:

$ curl -H "Authorization: token $GITHUB_PRODUCTION_TOKEN" -X POST -d '{"query": "query { viewer { login } }"}'

Breaking this down, we’re:

  1. Setting an HTTP header with the value "Authorization: token $GITHUB_PRODUCTION_TOKEN" (where $GITHUB_PRODUCTION_TOKEN is a valid OAuth token).
  2. Sending an HTTP POST as the method with -X POST
  3. Specifying a JSON payload which has a key of query and a value of the following GraphQL query:
query { 
  viewer { 

If everything works successfully, you’d get something similar to the following response:


I’m unfamiliar with Python, but I imagine you may be able to use an HTTP library like this to accomplish a similar API call.

I hope this is helpful!

Brooks Swinnerton


So is there a way to use the Github API v4 with python


@abrad1212 I think the idea is to just use a http request library. Most GraphQL libraries seem to be for building APIs yourself, not consuming one (although I’m not familiar with the libraries linked above).

So I think something like the requests library is a safe bet. Just make POST requests, e.g. r ='', data = {'query': 'your graphql query string'}) (you may need to set an auth token in the headers depending on your queries).

Hope this helps.


The data parameter will form-encode your data and the API won’t understand the request. You should use r ='', json = {'query': 'your graphql query string'}) instead.


Just summarizing all the answers as a single snippet…

import requests

url = ''
json = { "query" : "{ viewer  { login } }" }
api_token = "your api token here..."
headers = {'Authorization': 'token %s' % api_token}

r =, json=json, headers=headers)
print (r.text)

Hope it helps! :slightly_smiling_face: