The Open Targets Genetics 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:
You can construct a query that returns only the fields that you need
You can build graphical queries that traverse a data graph through resolvable entities and this reduces the need for multiple queries
You can access the GraphQL API playground with built-in documentation to build your query and run it in real-time
You can view the schema that shows the available fields for each object along with a description and data type attribute
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.
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.
#!/usr/bin/env python3# Import relevant libraries for HTTP request and JSON formattingimport requestsimport json# Set study_id and variant_id variablesstudy_id ="GCST009240_9"variant_id ="1_55052794_A_G"# Build query stringquery_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 endpointvariables ={"myVariantId": variant_id,"myStudyId": study_id}# Set base URL of Genetics Portal GraphQL API endpointbase_url ="https://api.genetics.opentargets.org/graphql"# Perform POST request and check status code of responser = 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 dataprint(api_response_as_json["data"]["studyLocus2GeneTable"]["rows"][0])
# Install relevant library for HTTP requestslibrary(httr)# Set study_id and variant_id variablesstudy_id <-"GCST009240_9"variant_id <-"1_55052794_A_G"# Build query stringquery_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 endpointbase_url <-"https://api.genetics.opentargets.org/graphql"# Set variables object of arguments to be passed to endpointvariables <-list("myStudyId" = study_id, "myVariantId" = variant_id)# Construct POST request body object with query string and variablespost_body <-list(query = query_string, variables = variables)# Perform POST requestr <-POST(url=base_url, body=post_body, encode='json')# Print first entry of L2G data to RStudio consolehead(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.