R/auth.R

Defines functions save_api_key get_api_key

Documented in get_api_key save_api_key

#' Get and set API keys
#'
#' Get and set the API key used to make requests to the Epidata API. Without a
#' key, requests may be subject to rate limits and other limitations.
#'
#' We recommend you register for an API key. While most endpoints are available
#' without one, there are [limits on API usage for anonymous
#' users](https://cmu-delphi.github.io/delphi-epidata/api/api_keys.html),
#' including a rate limit. If you regularly request large amounts of data,
#' please consider [registering for an API
#' key](https://api.delphi.cmu.edu/epidata/admin/registration_form).
#'
#' API keys are strings read from the environment variable `DELPHI_EPIDATA_KEY`.
#' We recommend setting your key with `save_api_key()`, which will open your
#' `.Renviron` file in a text editor. You will need to write
#' `DELPHI_EPIDATA_KEY=yourkeyhere` (without quotes) in the file and save it.
#' Once the `.Renviron` file has been saved as instructed, it will be read
#' automatically when you start future R sessions
#' (see [`?Startup`][base::Startup] for details on `.Renviron` files).
#' Alternatively, you can modify the environment variable at the command line
#' before/while launching R, or inside an R session with [`Sys.setenv()`],
#' but these will not persist across sessions.
#'
#' Once an API key is set, it is automatically used for all requests made by
#' functions in this package.
#'
#' @return For `get_api_key()`, returns the current API key as a string, or
#'   `""` if none is set.
#'
#' @references
#' - [Delphi Epidata API Keys
#'   documentation](https://cmu-delphi.github.io/delphi-epidata/api/api_keys.html).
#' - [Delphi Epidata API Registration
#'   Form](https://api.delphi.cmu.edu/epidata/admin/registration_form).
#'
#' @export
get_api_key <- function() {
  key <- Sys.getenv("DELPHI_EPIDATA_KEY", unset = "")
  if (key != "") {
    return(key)
  }

  cli::cli_warn(
    c(
      "No API key found. You will be limited to non-complex queries and encounter rate limits if you proceed.",
      "i" = "See {.help save_api_key} for details on obtaining and setting API keys."
    ),
    .frequency = "regularly",
    .frequency_id = "delphi.epidata.key"
  )
  return("")
}

#' @rdname get_api_key
#' @export
save_api_key <- function() {
  cli::cli_inform(
    c(
      "i" = "This function will open your {.code .Renviron} file in a text editor. You will need to
      write {.code DELPHI_EPIDATA_KEY=yourkeyhere} (without quotes) in the file and save it. If the editor
      does not open, you will need to edit the file manually.",
      "i" = "Press enter to continue."
    )
  )
  readline()

  # Prefer a project-level .Renviron in the working directory; fall back to
  # the user-level one R reads at startup (see ?Startup).
  if (file.exists(".Renviron")) {
    path <- ".Renviron"
  } else {
    path <- path.expand(file.path("~", ".Renviron"))
    if (!file.exists(path)) {
      file.create(path)
    }
  }
  utils::file.edit(path)

  if (path == ".Renviron") {
    cat("\n\n")
    cli::cli_inform(
      c(
        "i" = "Your project {.code .Renviron} file has been updated. Make sure not to share this
        file (add it to .gitignore or equivalents)."
      )
    )
  }
  cli::cli_inform(c("i" = "Restart R for the change to take effect."))
}

Try the epidatr package in your browser

Any scripts or data that you put into this service are public.

epidatr documentation built on Sept. 21, 2026, 5:08 p.m.