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.

Related APIs in Environment