Get and set your openFDA API keys
Arguments
- api_key
A single-length character vector with your openFDA API key. Do not pass the key in as a parameter in your scripts, though: see the 'Setting an openFDA API key' section below for more information. You can generate an API key on the FDA website.
- user
A single-length character vector, used as the
username
argument inkeyring::key_set()
andkeyring::key_get()
, respectively. The default is"openFDA"
. Supply a custom value to this argument if for some reason you want to store multiple API keys (though note that the API restricts the number of requests on a per-IP address basis, as well as on a per-key basis).
Value
For set_api_key()
, returns NULL
invisibly. The function is
called for its side effect: to store the openFDA API key securely using
keyring::key_set()
.
For get_api_key()
, returns the API key as a single string. An error
will be thrown if no key is found or if the returned key is an empty string
(""
).
Setting an openFDA API key
API keys are the sort of thing that should be stored securely. For that
reason, set_api_key()
and get_api_key()
utilise the
keyring package for storing/retrieving the API
key. It is best practice not to set your API key by passing it directly to
set_api_key()
, or you risk exposing it either in your scripts or in
.Rhistory
.
Instead, start an interactive R session and run set_api_key()
. R will
prompt you to enter your API key, which will then be stored for future use
without logging the key somewhere unsecure.
As for the key itself? It will be stored securely using keyring::key_set()
.
Once set on a machine, you shouldn't need to run set_api_key()
again.
Creating a new keyring
If your current keyring backend supports named keyrings,
then when using set_api_key()
for the first time you will be prompted to
create a new keyring called "openFDA"
using keyring::keyring_create()
.
When asked to do this, you will need to enter a password which can be used to
'lock' and 'unlock' this new keyring (see keyring::keyring_lock()
for
details). However, you shouldn't need to use this password in the future, as
the keyring will remain unlocked unless you use keyring::keyring_lock()
to
lock the "openFDA"
keyring.
Examples
#> Warning: Selecting ‘env’ backend. Secrets are stored in environment variables
#> Warning: Selecting ‘env’ backend. Secrets are stored in environment variables
# Set your openFDA API key with `set_api_key()`
api_key <- "example_api_key"
set_api_key(api_key)
#> Warning: Selecting ‘env’ backend. Secrets are stored in environment variables
#> Warning: Selecting ‘env’ backend. Secrets are stored in environment variables
#> ! OpenFDA API key set.
#> ℹ You can retrieve the key with the following code:
#> `keyring::key_get(service = "OPENFDA_KEY", user = "openFDA")`
# Retrieve it with `get_api_key()`
get_api_key()
#> Warning: Selecting ‘env’ backend. Secrets are stored in environment variables
#> Warning: Selecting ‘env’ backend. Secrets are stored in environment variables
#> [1] "example_api_key"
# An error will be thrown you try to set your API key to an empty string.
try(set_api_key(""))
#> Error in set_api_key("") :
#> ! If providing `api_key`, it must not be an empty string ("\"\"")