# Issue retrieving data via API

**URL:** <https://community.opentargets.org/t/issue-retrieving-data-via-api/508>\
**Category:** GraphQL API\
**Created:** [16 March 2022 09:37 UTC](https://community.opentargets.org/t/issue-retrieving-data-via-api/508 "2022-03-16T09:37:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ipediez](https://dub1.discourse-cdn.com/flex017/user_avatar/community.opentargets.org/ipediez/32/181_2.png) [@Ipediez](https://community.opentargets.org/u/Ipediez)\
**Post date:** [16 March 2022 09:37 UTC](https://community.opentargets.org/t/issue-retrieving-data-via-api/508/1 "2022-03-16T09:37:42Z")

</div>

Hi,

I’m trying to access OpenTargets data through API but keep getting a 400 error. I’ve tried two approaches getting the same error:

1. First, I constructed the query using the playground and then adapted it as an R script following the examples provided at the [EBI OpenTargets API training course from September 2021](https://drive.google.com/drive/folders/1UJTC6-gnNwuERGCfj4L377827o7B3hEb). I got the error 400.

2. Then, to check if the error was related to the script I had created, I downloaded the example scripts provided at that same training course, getting the same error.

I’ll post here both scripts:

**EBI training example:**

```auto
# Install relevant library for HTTP requests
library(httr)

# Set gene_id variable
gene_id <- "ENSG00000091831"

# Build query string
query_string = "
  query target($ensemblId: String!){
    target(ensemblId: $ensemblId){
      id
      approvedSymbol
      approvedName
      bioType
      genomicLocation{
        chromosome
        start
        end
        strand
      }
    }
  }
"

# Set base URL of GraphQL API endpoint
base_url <- "https://api.platform.opentargets.org/api/v4/graphql"

# Set variables object of arguments to be passed to endpoint
variables <- list("ensemblId" = gene_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 data to RStudio console
print(content(r)$data)

```

**The query I’m interested in:**

```auto
library(httr)
ensemblId <- "ENSG00000064012"
query_string <- "
  query search{
    target($ensemblId: String!) {
      functionDescriptions
      knownDrugs {
        uniqueDrugs
        count
        rows {
          drugType
          mechanismOfAction
          disease {
            name
          }
          drug{
            name
            id
            isApproved
          }
        }
      }
    }
  }
"

base_url <- "https://api.platform.opentargets.org/api/v4/graphql"

# Set variables object of arguments to be passed to endpoint
variables <- list("ensemblId" = ensemblId)

# 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 data to RStudio console
print(content(r)$data)

```

---

<div class="post-metadata">

**Author:** ![dsuveges](https://dub1.discourse-cdn.com/flex017/user_avatar/community.opentargets.org/dsuveges/32/30_2.png) [@dsuveges](https://community.opentargets.org/u/dsuveges)\
**Post date:** [18 March 2022 11:52 UTC](https://community.opentargets.org/t/issue-retrieving-data-via-api/508/2 "2022-03-18T11:52:25Z")

</div>

Hi @Ipediez!

Thank you for reaching out! You have just pointed out an inaccuracy in our training material. The field name is not ` bioType` instead `biotype`. Other than that the query looks good. Your query of interest looks like [this](https://api.platform.opentargets.org/api/v4/graphql/browser?operationName=assocs&query=query%20search%28%24ensemblId%3A%20String%21%29%7B%0A%20%20%20%20target%28ensemblId%3A%20%24ensemblId%29%20%7B%0A%20%20%20%20%20%20functionDescriptions%0A%20%20%20%20%20%20knownDrugs%20%7B%0A%20%20%20%20%20%20%20%20uniqueDrugs%0A%20%20%20%20%20%20%20%20count%0A%20%20%20%20%20%20%20%20rows%20%7B%0A%20%20%20%20%20%20%20%20%20%20drugType%0A%20%20%20%20%20%20%20%20%20%20mechanismOfAction%0A%20%20%20%20%20%20%20%20%20%20disease%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20drug%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%20%20%20%20id%0A%20%20%20%20%20%20%20%20%20%20%20%20isApproved%0A%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%7D).

You need to provide the query variable as this:  
`{ "ensemblId": "ENSG00000064012" }`

When not sure about the query, I recommend to use our graphql playground [here](https://api.platform.opentargets.org/api/v4/graphql/browser). This tool provides suggestions on the accessible field names and the documentation of the schema is just right there. If you want to reproduce a content of a widget, you can get the graphql request by clicking the “API query” button.
