Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to query the Open Targets genetics API with curl

Querying the Open Targets Genetics API with curl

Let's say you want to find the symbol of a gene given its Ensembl ID. The GraphQL query to do that is:

{
  geneInfo(geneId: "ENSG00000091831") {
    symbol
  }
} 

Using curl, we can query the API as follows:

curl -X POST https://genetics-api.opentargets.io/graphql -H 'Content-Type: application/json' -d '{ "query": "{ geneInfo(geneId: \"ENSG00000091831\") { symbol }}"}'

In the command above, we are sending the GraphQL query as a string that is assigned to the query property in the JSON payload. Remember that we need to escape the quotes with a backslash inside a string in JSON.

This is an ok method for querying the API when the query is simple. However, GraphQL queries tend to quickly become tedious to type in the command line.

Another way is to save the query separately in a file. For example, if we want to find which studies and lead variants are assigned to a gene as a result of the L2G pipeline, we can save the following query in a file query.json.

{
  "query": "{
    studiesAndLeadVariantsForGeneByL2G(geneId: \"ENSG00000158158\") {
      variant {
        id
        rsId
      }
      study {
        studyId
        traitReported
      }
    }
  }"
}

Then, we can use this file to query the API:

curl -X POST https://genetics-api.opentargets.io/graphql -H 'Content-Type: application/json' -d @query.json

Make sure that the file that contains the query, in this case query.json, is located in the same directory where the curl command is being invoked from.

You can then pipe the results to jq or any other command line tool used for exploring/manipulating the data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment