Get a list of species by country from IUCN

How many species are in each country according to IUCN Red List?

Today, my goal is to get a list of species per country according to the ICUN Red List. I could use the rredlist R package. There is a function that sounds helpful: rl_sp_country(). But just to test my API query techniques, I will use the IUCN API to find it out. Note: the rredlist package, does exactly the same, an API query.

You can check my other posts playing around with the iNaturalist API: First steps with the iNaturalist API and Using the iNaturalist API to detect novel records.

Let’s go!

The use of the IUCN API is strictly under the Terms of Use of the IUCN Red List of Threatened Species.

library(knitr)
library(countrycode)
library(rredlist)
library(httr)
library(jsonlite)
library(tidyverse)

We will use data from Latin America and the package countrycode to get the ISO2 code for each country.

LatinAmerica <- tibble(country= c('Mexico', 'Brazil', 'Costa Rica', 'Colombia', 'Peru', 'Argentina', 'Ecuador', 'Panama', 'Chile', 'Venezuela', 'Belize', 'Honduras', 'Bolivia', 'Guatemala', 'Cuba', 'Nicaragua', 'Paraguay', 'Bahamas', 'Jamaica', 'Trinidad and Tobago', 'Guyana', 'Dominican Republic', 'El Salvador', 'Suriname', 'Uruguay', 'Haiti')) %>%
  mutate(iso=countrycode(country, 'country.name', 'iso2c'))

LatinAmerica %>% kable()
countryiso
MexicoMX
BrazilBR
Costa RicaCR
ColombiaCO
PeruPE
ArgentinaAR
EcuadorEC
PanamaPA
ChileCL
VenezuelaVE
BelizeBZ
HondurasHN
BoliviaBO
GuatemalaGT
CubaCU
NicaraguaNI
ParaguayPY
BahamasBS
JamaicaJM
Trinidad and TobagoTT
GuyanaGY
Dominican RepublicDO
El SalvadorSV
SurinameSR
UruguayUY
HaitiHT

Now, let’s build a function to get the list of species per country.

The function gets a list of countries iso codes and a IUCN token, and returns a tibble with the scientific name and taxon id of the species, the IUCN category and the country iso. We will use the packages httr and jsonlite to process the JSON call. To check that is doing what we want, the function will print on screen the process, with the result number and the queried country.

getSpeciesByCountriesIUCN <- function(country_iso, token) {

  speciesByCountries <- tibble(scientific_name = character(),
                               taxon_id = numeric(),
                               category = character(),
                               country = character())

  num_results = 0 # used to put the API to sleep and print on the console the num

  for(country_iso_i in country_iso) {  

    if ((num_results %% 10) + 10 == 10) {
      Sys.sleep(0) # every 10 calls, the code can stop for x seconds
    }

    call_url <- str_glue('https://apiv3.iucnredlist.org/api/v3/country/getspecies/',
                         '{country_iso_i}?token={token}')

    get_json_call <- GET(url = call_url) %>%
      content(as = "text") %>% fromJSON(flatten = TRUE)

    results <- as_tibble(get_json_call$result)

    speciesByCountries_i <- tibble(scientific_name = results$scientific_name,
                                   taxon_id = results$taxonid,
                                   category = results$category,
                                   country = country_iso_i)

    speciesByCountries <- rbind(speciesByCountries, speciesByCountries_i)
    num_results <- num_results + 1
    cat(num_results, ': ', country_iso_i, '\n')
  }
  return(speciesByCountries)
}

So, let’s try it out!

To use it you WILL need a token from IUCN. Here you can find more information on how to get it. https://apiv3.iucnredlist.org/api/v3/token.

token <- '' # paste here the token you get from IUCN
speciesByLatamCountriesIUCN <- getSpeciesByCountriesIUCN(LatinAmerica$iso, token)
head(speciesByLatamCountriesIUCN, n=5) %>%
  kable()
scientific_nametaxon_idcategorycountry
Abaeis nicippe173005000LCMX
Abarema idiopoda146784206LCMX
Abarema zolleriana198888990VUMX
Abatia mexicana126620170VUMX
Abeillia abeillei22687170LCMX

Finally, let’s do some summaries to find out how many species we have per country, and, for instance, how many are not threatened according to the IUCN categories (LC), (VU), and (NT), and the percentage over the total.

speciesByLatamCountriesIUCN %>%
  mutate(countryName=countrycode(country, 'iso2c', 'country.name')) %>%
  group_by(countryName) %>%
  summarise(species_richness=n_distinct(scientific_name),
            non_threatened=n_distinct(scientific_name[category == 'LC' |
                                                         category == 'VU'|
                                                         category ==  'NT'])) %>%
  ungroup() %>%
  mutate(`%` = scales::label_percent()(round(non_threatened / species_richness, 2))) %>%
  rename(`Country name`=countryName,
         `Number of species`=species_richness,
         `Number of non-threatened species`=non_threatened) %>%
  kable(format.args= list(big.mark = ','))
Country nameNumber of speciesNumber of non-threatened species%
Argentina4,5874,14690%
Bahamas2,5162,29991%
Belize3,7503,52294%
Bolivia6,5366,05793%
Brazil18,07914,91082%
Chile2,3821,97683%
Colombia14,57012,62887%
Costa Rica7,4556,73490%
Cuba3,4593,01487%
Dominican Republic2,8702,56289%
Ecuador11,3439,08780%
El Salvador3,0612,87294%
Guatemala5,8805,19488%
Guyana5,2114,93595%
Haiti2,8702,41084%
Honduras5,6675,15491%
Jamaica2,7022,32486%
Mexico12,56410,00280%
Nicaragua5,4925,15094%
Panama7,6456,82289%
Paraguay2,4752,36395%
Peru10,1698,98188%
Suriname4,4124,22096%
Trinidad & Tobago3,0752,86893%
Uruguay1,9011,74292%
Venezuela10,1178,95388%

That’s all!

Florencia Grattarola
Florencia Grattarola
Postdoc Researcher

Uruguayan biologist doing research in macroecology and biodiversity informatics.