In this ~45 minute introduction, the goal is:
Introduce the modern dataRetrieval workflows.
The intended audience is someone:
New to dataRetrieval
Has some R experience
USGS Water Data APIs *
Surface water levels
Groundwater levels
Site metadata
Peak flows
Rating curves
Discrete water-quality data
Water Quality Portal (WQP) Data
Discrete water-quality data
USGS and non-USGS data
dataRetrieval is available on the Comprehensive R Archive Network (CRAN) repository. To install dataRetrieval on your computer, open RStudio and run this line of code in the Console:
Then each time you open R, you’ll need to load the library:



Within R, you can call help files for any dataRetrieval function:
Click here to open a new window:

Are you a seasoned dataRetrieval user?
Here are resources for recent major changes:
There’s been a lot of changes to dataRetrieval over the past year. If you’d like to see an overview of those changes, visit: Changes to dataRetrieval
Biggest changes:
NWIS servers will be shut down, so all readNWIS functions will eventually stop working
read_waterdata functions are modern and should be used when possible
The “USGS Water Data APIs” are the new home for USGS data
The Water Data APIs limit how many queries a single IP address can make per hour
You can run new dataRetrieval functions without a token
You might run into errors quickly. If you (or your IP!) have exceeded the quota, you will see:
! HTTP 429 Too Many Requests.
• You have exceeded your rate limit. Make sure you provided your API key from https://api.waterdata.usgs.gov/signup/, then either try again later or contact us at https://waterdata.usgs.gov/questions-comments/?referrerUrl=https://api.waterdata.usgs.gov for assistance.
Request a USGS Water Data API Token: https://api.waterdata.usgs.gov/signup/
Save it in a safe place (KeePass or other password management tool)
Add it to your .Renviorn file as API_USGS_PAT.
Restart R
Check that it worked by running (you should see your token printed in the Console):
See next slide for a demonstration.
My favorite method to do add your token to .Renviron is to use the usethis package. Let’s pretend the token sent you was “abc123”:
Save that file using the save button
Restart R/RStudio.
Run after restarting R:

After save and restart, check that it worked by running:
The USGS uses various codes for basic retrievals. These codes can have leading zeros, therefore they need to be a character surrounded in quotes (“00060”).
read_waterdata_parameter_codes()read_metadata("statistic-codes")Here are some examples of a few common codes:
|
|
We’re going walk through 3 retrievals:
Workflow 1: Daily Data
Uses the new USGS Water Data API
Modern data access point going forward
Workflow 2: Discrete Data
Uses new USGS Samples Data
Modern data access point going forward
Workflow 3: Continuous Data
Uses the new USGS Water Data API
Modern data access point going forward
Let’s pull daily mean discharge data for site “USGS-0940550”, getting all the data from October 10, 2024 onward.
Requesting:
https://api.waterdata.usgs.gov/ogcapi/v0/collections/daily/items?f=json&lang=en-US&monitoring_location_id=USGS-09405500¶meter_code=00060&statistic_id=00003&limit=50000
Remaining requests this hour:1717
In RStudio, click on the data frame in the upper right Environment tab to open a Viewer.
Let’s use ggplot2 to visualize the data.
Use your “tab” key!
When you look at the help file for the new functions, you’ll notice there are lots of possible inputs (arguments).
You DO NOT need to (and should not!) specify all of these parameters.
However, also consider what happens if you leave too many things blank. What do you suppose will be returned here?
Since no list of sites or bounding box was defined, ALL the daily data in ALL the country with parameter code “00060” and statistic code “00003” will be returned.
The “time” argument has a few options:
A single date (or date-time): “2024-10-01” or “2024-10-01T23:20:50Z”
A bounded interval: c(“2024-10-01”, “2025-07-02”)
Half-bounded intervals: c(“2024-10-01”, NA)
Duration objects: “P1M” for data from the past month or “PT36H” for the last 36 hours
Here are a bunch of valid inputs:
# Ask for exact times:
time = "2025-01-01"
time = as.Date("2025-01-01")
time = "2025-01-01T23:20:50Z"
time = as.POSIXct("2025-01-01T23:20:50Z",
format = "%Y-%m-%dT%H:%M:%S",
tz = "UTC")
# Ask for specific range
time = c("2024-01-01", "2025-01-01") # or Dates or POSIXs
# Asking beginning of record to specific end:
time = c(NA, "2024-01-01") # or Date or POSIX
# Asking specific beginning to end of record:
time = c("2024-01-01", NA) # or Date or POSIX
# Ask for period
time = "P1M" # past month
time = "P7D" # past 7 days
time = "PT12H" # past hoursUse your “tab” key!
Let’s get orthophosphate (“00660”) data from the Shenandoah River at Front Royal, VA (“USGS-01631000”).
GET: https://api.waterdata.usgs.gov/samples-data/results/basicphyschem?mimeType=text%2Fcsv&monitoringLocationIdentifier=USGS-01631000&usgsPCode=00660
[1] 104
That’s a LOT of columns returned. We won’t look at them here, but you can use View in RStudio to explore on your own.
Continuous data is the high-frequency sensor data.
We’ll look at Suisun Bay a Van Sickle Island NR Pittsburg CA (“USGS-11455508”), with parameter code “99133” which is Nitrate plus Nitrite.
[1] "monitoring_location_id"
[2] "parameter_code"
[3] "statistic_id"
[4] "time"
[5] "value"
[6] "unit_of_measure"
[7] "approval_status"
[8] "last_modified"
[9] "qualifier"
[10] "time_series_id"
Requesting:
https://api.waterdata.usgs.gov/ogcapi/v0/collections/continuous/items?f=json&lang=en-US&skipGeometry=TRUE&limit=50000&monitoring_location_id=USGS-11455508¶meter_code=99133&time=2024-01-01T00%3A00%3A00Z%2F2024-06-01T00%3A00%3A00Z
read_waterdata_ts_meta discovers available daily and continuous time series
read_waterdata_field_meta discovers available field measurement
read_waterdata_combined_meta combines the time series and field measurement discovery. This function also provides the most flexibility with geographic queries.
summarize_waterdata_samples discovers discrete data at specific monitoring locations
The next slides will demo how to use those.
read_waterdata_sampleAny use of trade, firm, or product name is for descriptive purposes only and does not imply endorsement by the U.S. Government.