faostat_login: Register for a FAOSTAT account and login to get an access...

View source: R/rest.R

faostat_loginR Documentation

Register for a FAOSTAT account and login to get an access token

Description

Authenticates with the FAOSTAT API and stores the access token for the current session. Credentials are resolved in this order:

  1. Arguments passed directly to the function.

  2. The FAOSTAT_USER and FAOSTAT_PASSWORD environment variables (set them in ‘~/.Renviron’ to avoid storing credentials in scripts).

  3. An interactive prompt (email via readline(), password via rstudioapi::askForPassword() when available, otherwise readline()).

API calls (get_faostat_bulk() etc.) trigger faostat_login() automatically when no token is present, so explicit calls are optional.

Usage

faostat_login(
  username = Sys.getenv("FAOSTAT_USER"),
  password = Sys.getenv("FAOSTAT_PASSWORD"),
  base_url = "https://faostatservices.fao.org/api/v1"
)

Arguments

username

character. Your FAOSTAT account email. Defaults to the FAOSTAT_USER environment variable, then an interactive prompt.

password

character. Your FAOSTAT account password. Defaults to the FAOSTAT_PASSWORD environment variable, then an interactive prompt.

base_url

character. Base URL for FAOSTAT authentication.

Examples

## Not run: 
# Simplest usage — prompts for credentials interactively if not set:
crop_production <- get_faostat_bulk(code = "QCL", data_folder = "data_raw")

# Silent usage — set in ~/.Renviron (run usethis::edit_r_environ() to open it):
# FAOSTAT_USER=your@email.com
# FAOSTAT_PASSWORD=yourpassword
crop_production <- get_faostat_bulk(code = "QCL", data_folder = "data_raw")

## End(Not run)

FAOSTAT documentation built on April 1, 2026, 5:06 p.m.