# Grab Google Rankings from VALUE SERP API using R'

### What is VALUE SERP?

It's a great paid API that provides ranking data for a cheap price.

### How to use its API?

[>> Create an account <<](https://app.valueserp.com/signup)

When you create your account, you are given a few dollars to test the service.

### API Authentication

Grab your API key from your [profile](https://app.valueserp.com/profile)

![⚠️ Developer credentials (such as passwords, keys and client IDs) should be kept confidential. ⚠️](https://2998538899-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSXey8kI3RllV-BSG8m%2Fuploads%2F4Y3S7vFdzSwAQBkprtv4%2FScreenshot%202022-06-12%20at%206.09.25%20pm.png?alt=media\&token=9821b640-33cd-4a10-bdf1-77b1b03c14c7)

Here is how you can send query, replace the api\_key with your own below.

```r
# Loading the right libraries
library(httr)
library(jsonlite)

# Parameters list
params = list(
      `api_key` = 'XXXXXX',
      `q` = "covid",
      `gl` = "fr",
      `hl` = "fr",
      `num` = 20,
      `google_domain` = 'google.fr'
)
# q : the search query
# gl : 2 letter country code 
# hl : language code
# num : number of result asked

# ask for the data
res <- httr::GET(url = 'https://api.valueserp.com/search', query = params)

# translate to string
res_text <- httr::content(res, "text")

# translate to a more readable format
res_json <- jsonlite::fromJSON(res_text, flatten = TRUE)
```

You can inspect the result by running this command line

```r
View(res_json)
```

![](https://2998538899-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MSXey8kI3RllV-BSG8m%2Fuploads%2F2K51uEWtmxLyYWmL5nlJ%2FScreenshot%202022-06-12%20at%2011.00.43%20pm.png?alt=media\&token=e32986e4-75ed-4dc2-85e5-7c682f60a698)

To make it easier for you, I have created a function that you can copy and paste, just replace the `api_key` with your own below.

```r
serpValueRank <- function(myKwd, glang, hlang, nbr, tld){
  library(httr)
  library(jsonlite)
  all_full_txt <- data.frame(matrix(ncol = 2, nrow = 0))
  colnames(all_full_txt) <- c("kwd", "POS1")
  for (i in 1:length(myKwd)) {
    params = list(
      `api_key` = 'XXXXXX',
      `q` = myKwd[i],
      `gl` = glang,
      `hl` = hlang,
      `num` = nbr,
      `google_domain` = paste0('google', tld)
    )
    message(i, " ", myKwd[i])
    all_full_txt[i, "kwd"] <- myKwd[i]
    res <- httr::GET(url = 'https://api.valueserp.com/search', query = params)
    res_text <- httr::content(res, "text")
    res_json <- jsonlite::fromJSON(res_text, flatten = TRUE)
    for (rslt in 1:length(res_json[["organic_results"]][["link"]])) {
      all_full_txt[i, paste0("POS", rslt)] <-
      res_json[["organic_results"]][["link"]][[rslt]]
    }
  }
  return(all_full_txt)
}
```

after this has been copy-pasted, you can just launch the function as many times as you want.\
you can ask for one ranking

```r
ranking <- serpValueRank("covid", "fr", "fr", 20, ".fr")
View(ranking)
```

or several at once

```r

kwds <- c("covid", "covid test", "covid booster")

kwdsRankings <- serpValueRank(kwds, "fr", "fr", 20, ".fr")

View(kwdsRankings)
```

You can also save those results [as a CSV or an Excel file](https://www.rforseo.com/export-data/send-and-read-seo-data-to-excel)

⚠️ By default, VALUE SERP will separate all SERP features. Meaning 'organic result' will exclude the video carousel for example. If you would like all of them flattened into the organic\_results array, then you could use “flatten\_results=true”. The flattened “position” property will include every SERP features
