USGS Water Services

USGS Water Services

  • Category: Science


Accessing USGS Water Services API with JavaScript

If you are looking for a way to access hydrological data for rivers, streams, and other bodies of water across the United States, then the USGS Water Services API is a great resource to use. This public API provides access to a wealth of data, including real-time streamflow data, water quality data, groundwater levels, and much more.

In this blog post, we will explore how to access the USGS Water Services API with JavaScript and provide some useful example code to get you started.

Setting up the API Request

The first step to accessing the USGS Water Services API with JavaScript is to make an API request. You can use the fetch() method to make a GET request to the API endpoint URL and pass in any necessary parameters as a query string.

Here's an example of how to make an API request to get real-time streamflow data for a specific site:

const siteCode = "01581680";
const url = `https://waterservices.usgs.gov/nwis/iv/?sites=${siteCode}&format=json`;

fetch(url)
  .then((response) => response.json())
  .then((data) => console.log(data));

In this example, we are using the fetch() method to make a GET request to the USGS Water Services API endpoint. We are passing in a sites parameter with a specific site code, and we are requesting the data in JSON format.

Reading API response

Once you have made an API request, you can access the data returned by the API in the then() method of the fetch() call. The data returned by the USGS Water Services API is in JSON format, so we can easily parse it using the json() method of the response object.

Here's how you can access the streamflow data returned by the API:

fetch(url)
  .then((response) => response.json())
  .then((data) => {
    const streamflow = data.value.timeSeries[0].values[0].value[0].value;
    console.log(`Streamflow at ${siteCode}: ${streamflow} cfs`);
  });

In this example, we are accessing the timeSeries property of the data object returned by the API. We then access the values property of the first time series object returned (which corresponds to the real-time streamflow data for the site code we requested). Finally, we access the value property of the first element of the values array to get the actual streamflow value.

Conclusion

In this blog post, we have explored how to access the USGS Water Services API with JavaScript. We have seen how to make an API request and how to parse the JSON data returned by the API. With this knowledge, you can now start building your own applications that use the USGS Water Services API to access hydrological data in real-time.

Visit to USGS Water Services website

Similar APIs of Science

Auroras.live DIY

Auroras.live DIY

Science

Friendly way of accessing various aurora hunting data: current space weather conditions, weather, skycams, satellite images, and archived information from the past 15 years.

skystargalaxy

Mercury Retrograde API

Mercury Retrograde API

Science

You may have heard friends say, “Oh, no – Mercury is about to retrograde again!” If you don’t know astrology, you may have wondered what they were talking about. Of all planetary aspects, this one seems to garner the most attention from readers, and it certainly generates the most mail. The reason for their interest is clear – this phenomenon is one of the few that affects everyone in a fairly uniform way, and its effects are always obvious. Once you begin to pay attention to how events in your life change during these phases, you will soon see how important it is to take note of them. Keeping track of Mercury retrograde periods can allow you to increase your productivity and avoid at least some of the frustration they can bring about. This API serves JSON over HTTPs. It allows you to determine whether Mercury is in retrograde for a given date.

mercuryplanetearth

Materials Platform for Data Science

Materials Platform for Data Science

Science

The MPDS application programming interface (API) presents the materials data of the PAULING FILE database online in the machine-readable formats, suitable for automated processing. The intended audience is software engineers and data scientists. The API is available by a subscription (SLA). To start using the API the reader needs a valid API key from the MPDS account. We encourage to contact us for opening such the account.

materialsplateformdata

iDigBio

iDigBio

Science

Access millions of museum specimens from organizations around the world. The API gives you Bio records according to available records in the directory. Provides you with the media, versioning, limiting, sorting of the queries.

urlmuseumlife

protocols.io

protocols.io

Science

protocols.io is the #1 open access repository for science methods. The leading platform for data management and protocol sharing. Store, organize, modify, and publish your work all in one place.

biolodychemistryworkflows

SHARE

SHARE

Science

A free, open, dataset about research and scholarly activities. SHARE is a higher education initiative whose mission is to maximize research impact by making research widely accessible, discoverable, and reusable. To fulfill this mission SHARE is building a free, open, data set about research and scholarly activities across their life cycle.

researchstudyscience

Open Science Framework

Open Science Framework

Science

The OSF serves as a repository and archive for study designs, materials, data, manuscripts, or anything else associated with your research during the research process. Every project and file on the OSF has a permanent unique identifier, and every registration (a permanent, time-stamped version of your projects and files) can be assigned a DOI. You can use the OSF to measure your impact by monitoring the traffic to projects and files you make public. With the OSF you have full control of what parts of your research are public and what remains private.

repositoryarchivestudy

Launch Library

Launch Library

Science

Upcoming Space Launches. Provides data like "A batch of 60 satellites for Starlink mega-constellation - SpaceX's project for space-based Internet communication system."

dataspacelaunch

SpaceX

SpaceX

Science

Company, vehicle, launchpad and launch data. Want to know a specific aircrafts launch year, unic code, utc number or local timing? Or interested in rocket technology and curious about the number of enginers in an API? Go ahead. Try the rocket API and much more by SpaceX.

SpaceXTeslaPaypal

CORE

CORE

Science

Access the world's Open Access research papers. A unique and free API providing real-time machine access to metadata and full texts of research papers in CORE. One API for accessing data across all data providers. Perfect for developing and running innovative applications that need global access to research papers.

researchpapersuniversity