GraphQL API
The Open Targets Genetics Portal API is a GraphQL API that powers the user interface and that supports language-agnostic access to our data.
Compared to a REST API, our GraphQL API offers five key benefits:
  1. 1.
    You can construct a query that returns only the fields that you need
  2. 2.
    You can build graphical queries that traverse a data graph through resolvable entities and this reduces the need for multiple queries
  3. 3.
    You can access the GraphQL API playground with built-in documentation to build your query and run it in real-time
  4. 4.
    You can view the schema that shows the available fields for each object along with a description and data type attribute
  5. 5.
    You only have to use POST requests with a query string and variables object

Available endpoints

The base URL endpoint for the Genetics Portal GraphQL API is:
https://api.genetics.opentargets.org/graphql
Our GraphQL API supports queries for a single variant, gene, study, or study-variant pair. For more systematic queries (e.g. for multiple variants), please use our data downloads or our Google BigQuery instance - open-targets-prod.

Sample GraphQL query

Below is a sample GraphQL API query for the Locus2Gene data for FINNGEN_R5_E4_DM2 (Type 2 diabetes) and 11_72752390_G_A that you can run in the GraphQL API browser.
The query will recreate the data table available in the "Gene prioritisation using locus-to-gene pipeline" section on the FINNGEN_R5_E4_DM2 and 11_72752390_G_A study-locus page.
query genePrioritisationUsingL2G {
studyLocus2GeneTable(studyId: "FINNGEN_R5_E4_DM2", variantId: "11_72752390_G_A") {
rows {
gene {
symbol
id
}
yProbaModel
yProbaDistance
yProbaInteraction
yProbaMolecularQTL
yProbaPathogenicity
hasColoc
distanceToLocus
}
}
}
Using GraphQL's query strings and variables object constructs, you can also access the data using any programming language that supports HTTP POST requests. While this is a valid approach, we discourage users from repeatedly querying the GraphQL API one entity at a time. Instead, our comprehensive datasets available for download provide a simpler and more performant strategy to achieve the same result.

Sample scripts

Below is a sample script in Python for the same sample query above - FINNGEN_R5_E4_DM2 (Type 2 diabetes) and 11_72752390_G_A.
Python
R
#!/usr/bin/env python3
# Import relevant libraries for HTTP request and JSON formatting
import requests
import json
# Set study_id and variant_id variables
study_id = "FINNGEN_R5_E4_DM2"
variant_id = "11_72752390_G_A"
# Build query string
query_string = """
query genePrioritisationUsingL2G($myVariantId: String!, $myStudyId: String! ){
studyLocus2GeneTable(studyId: $myStudyId, variantId: $myVariantId){
rows {
gene {
symbol
id
}
yProbaModel
yProbaDistance
yProbaInteraction
yProbaMolecularQTL
yProbaPathogenicity
hasColoc
distanceToLocus
}
}
}
"""
# Set variables object of arguments to be passed to endpoint
variables = {"myVariantId": variant_id, "myStudyId": study_id}
# Set base URL of Genetics Portal GraphQL API endpoint
base_url = "https://api.genetics.opentargets.org/graphql"
# Perform POST request and check status code of response
r = requests.post(base_url, json={"query": query_string, "variables": variables})
print(r.status_code)
# Transform API response into JSON
api_response_as_json = json.loads(r.text)
# Print first element of JSON response data
print(api_response_as_json["data"]["studyLocus2GeneTable"]["rows"][0])
# Install relevant library for HTTP requests
library(httr)
# Set study_id and variant_id variables
study_id <- "FINNGEN_R5_E4_DM2"
variant_id <- "11_72752390_G_A"
# Build query string
query_string = "
query genePrioritisationUsingL2G($myVariantId: String!, $myStudyId: String! ){
studyLocus2GeneTable(studyId: $myStudyId, variantId: $myVariantId){
rows {
gene {
symbol
id
}
yProbaModel
yProbaDistance
yProbaInteraction
yProbaMolecularQTL
yProbaPathogenicity
hasColoc
distanceToLocus
}
}
}
"
# Set base URL of Genetics Portal GraphQL API endpoint
base_url <- "https://api.genetics.opentargets.org/graphql"
# Set variables object of arguments to be passed to endpoint
variables <- list("myStudyId" = study_id, "myVariantId" = variant_id)
# Construct POST request body object with query string and variables
post_body <- list(query = query_string, variables = variables)
# Perform POST request
r <- POST(url=base_url, body=post_body, encode='json')
# Print first entry of L2G data to RStudio console
head(content(r)$data$studyLocus2GeneTable$rows, 1)

Tutorials and how-to guides

For more information on how to use the GraphQL API and example queries based on actual use cases and research questions, check out the Open Targets Community.