PVWatts

PVWatts

Environment

Energy production photovoltaic (PV) energy systems

Visit API🔁 Alternatives

📚 Documentation & Examples

Everything you need to integrate with PVWatts

🚀 Quick Start Examples

PVWatts Javascript Examplejavascript
// PVWatts API Example
const response = await fetch('https://developer.nrel.gov/docs/solar/pvwatts-v5/', {
    method: 'GET',
    headers: {
        'Content-Type': 'application/json'
    }
});

const data = await response.json();
console.log(data);

PVWatts-v5 API Docs

The PVWatts-v5 API provides users with access to estimated solar energy production data for a specified location. Here we will cover how to use this API in JavaScript, along with some example codes.

API Key

Before you get started with the PVWatts-v5 API, you need to obtain an API key from the NREL Developer Network. You will then be able to use this key to access PVWatts-v5 and other NREL APIs.

Setting up

To use the PVWatts-v5 API in JavaScript, you'll need to make an HTTP GET request to the API's endpoint, passing in the required parameters as query string parameters. You can do this using the fetch() function. Here's an example of how to set this up.

const key = "YOUR_API_KEY";
const system_capacity = 4;
const module_type = 0;
const losses = 10;
const array_type = 0;
const tilt = 40;
const azimuth = -90;
const lat = 40.73061;
const lon = -73.93524;

const url = `https://developer.nrel.gov/api/solar/pvwatts/v5.json?api_key=${key}&system_capacity=${system_capacity}&module_type=${module_type}&losses=${losses}&array_type=${array_type}&tilt=${tilt}&azimuth=${azimuth}&lat=${lat}&lon=${lon}`;

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

In this example, we've set the key variable to your API key, and the other variables to the required parameters for the PVWatts-v5 API. Once you've constructed the URL, we use fetch() to make the API request. When the response comes back, we console.log() its data.

Example Code

Here are some example codes for the PVWatts-v5 API.

Calculate Energy Production

This example calculates the estimated energy production for a given location, using a 4 kW DC solar panel system.

const key = "YOUR_API_KEY";
const system_capacity = 4;
const module_type = 0;
const losses = 10;
const array_type = 0;
const tilt = 40;
const azimuth = -90;
const lat = 40.73061;
const lon = -73.93524;

const url = `https://developer.nrel.gov/api/solar/pvwatts/v5.json?api_key=${key}&system_capacity=${system_capacity}&module_type=${module_type}&losses=${losses}&array_type=${array_type}&tilt=${tilt}&azimuth=${azimuth}&lat=${lat}&lon=${lon}`;

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

Find Optimal Tilt and Azimuth

This example finds the optimal tilt and azimuth for a given location, based on the highest estimated energy production.

const key = "YOUR_API_KEY";
const system_capacity = 4;
const module_type = 0;
const losses = 10;
const array_type = 0;
const lat = 40.73061;
const lon = -73.93524;

let highestEnergy = 0;
let optimalTilt = 0;
let optimalAzimuth = 0;

for (tilt = 0; tilt <= 90; tilt++) {
  for (azimuth = -180; azimuth <= 180; azimuth++) {
    const url = `https://developer.nrel.gov/api/solar/pvwatts/v5.json?api_key=${key}&system_capacity=${system_capacity}&module_type=${module_type}&losses=${losses}&array_type=${array_type}&tilt=${tilt}&azimuth=${azimuth}&lat=${lat}&lon=${lon}`;

    fetch(url)
      .then((response) => response.json())
      .then((data) => {
        const energy = data.outputs.ac_annual;
        if (energy > highestEnergy) {
          highestEnergy = energy;
          optimalTilt = tilt;
          optimalAzimuth = azimuth;
        }
      })
      .catch((error) => console.log(error));
  }
}

console.log(`Optimal Tilt: ${optimalTilt}`);
console.log(`Optimal Azimuth: ${optimalAzimuth}`);
console.log(`Highest Energy: ${highestEnergy}`);

Calculate Energy Production for Multiple Locations

This example calculates the estimated energy production for multiple locations at once, using a 4 kW DC solar panel system.

const key = "YOUR_API_KEY";
const system_capacity = 4;
const module_type = 0;
const losses = 10;
const array_type = 0;
const tilt = 40;
const azimuth = -90;
const locations = [
  { lat: 40.73061, lon: -73.93524 },
  { lat: 34.05223, lon: -118.24368 },
  { lat: 51.50735, lon: -0.12776 },
];

locations.forEach((location) => {
  const url = `https://developer.nrel.gov/api/solar/pvwatts/v5.json?api_key=${key}&system_capacity=${system_capacity}&module_type=${module_type}&losses=${losses}&array_type=${array_type}&tilt=${tilt}&azimuth=${azimuth}&lat=${location.lat}&lon=${location.lon}`;

  fetch(url)
    .then((response) => response.json())
    .then((data) =>
      console.log(`${location.lat},${location.lon},${data.outputs.ac_annual}`)
    )
    .catch((error) => console.log(error));
});

Conclusion

The PVWatts-v5 API is an extremely useful tool for estimating the solar energy production of a given location. With the example codes in this article, you should now have a good understanding of how to use this API in JavaScript.

📊 30-Day Uptime History

Daily uptime tracking showing online vs offline minutes

Aug 9Aug 11Aug 13Aug 15Aug 17Aug 19Aug 21Aug 23Aug 25Aug 27Aug 29Aug 31Sep 2Sep 4Sep 704008001440Minutes
Online
Offline

Related APIs in Environment