Connect to External APIs Using UrlFetchApp in Google Apps Script (Google Sheet)

Ever wanted to pull in real-time data from other websites into your Google Sheet—like weather updates, currency rates, or stock prices? That’s where UrlFetchApp in Google Apps Script comes in. It allows your spreadsheet to talk to external APIs (Application Programming Interfaces), making your data live, dynamic, and more powerful.

This is incredibly useful for beginners who want to automate data fetching without learning a full programming language or building separate applications. With just a few lines of code, your Google Sheet becomes a live data dashboard!

Connect to External APIs Using UrlFetchApp in Google Apps Script (Google Sheet)

What Is UrlFetchApp in Apps Script?

UrlFetchApp is a built-in service in Google Apps Script that lets your script send HTTP requests (like GET or POST) to external APIs and receive data back. Simply put, it’s how your spreadsheet can connect to other web services and pull in data automatically.

Real-Life Example: Get Real-Time Exchange Rates in Google Sheets

Let’s say you’re managing an international business, and you regularly deal with USD, EUR, and INR. Instead of checking conversion rates manually, you want Google Sheets to fetch the latest exchange rates from an API like ExchangeRate-API.

Sample Output Table in Google Sheet

Currency Rate (to USD)
EUR 0.92
INR 83.10

Step-by-Step: How to Connect to an API with UrlFetchApp

1. Choose a Free API

For this example, we’ll use https://open.er-api.com/v6/latest/USD, which provides exchange rates for free and doesn’t require an API key.

2. Open Apps Script

  1. Open your Google Sheet
  2. Click Extensions → Apps Script

3. Add the Script


function getExchangeRates() {
  const url = "https://open.er-api.com/v6/latest/USD";
  const response = UrlFetchApp.fetch(url);
  const data = JSON.parse(response.getContentText());
  const rates = data.rates;

  const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  sheet.getRange("A1").setValue("Currency");
  sheet.getRange("B1").setValue("Rate (to USD)");

  const currencies = ["EUR", "INR"];
  for (let i = 0; i < currencies.length; i++) {
    sheet.getRange(i + 2, 1).setValue(currencies[i]);
    sheet.getRange(i + 2, 2).setValue(rates[currencies[i]]);
  }
}

4. Run the Function

  1. Click the Run ▶️ button in the Apps Script editor
  2. Authorize the script when prompted
  3. Your sheet will populate with live currency rates!

Key Benefits of Using UrlFetchApp

  • Live Data: Keep your sheets always up-to-date with real-world values
  • Automation: No more manual copy-pasting or downloading files
  • Versatile: Connect to any API that returns JSON, XML, or plain text

Pro Tips

  • Use Triggers to auto-run your script every hour or day
  • Store API keys in the PropertiesService instead of hardcoding them
  • Use try/catch blocks to handle errors gracefully if the API fails

Quick Reference Cheat Sheet

Code Description
UrlFetchApp.fetch(url) Sends a request to the specified API URL
JSON.parse() Converts the text response into a usable JavaScript object
response.getContentText() Returns the raw text of the API response
sheet.getRange().setValue() Writes values into your Google Sheet

Connecting Google Sheets to external APIs with UrlFetchApp is a game-changer. Whether you’re pulling in weather forecasts, exchange rates, or public data, the possibilities are endless. With just a little bit of scripting, your sheet becomes smarter, more dynamic, and a lot more useful. So go ahead—start experimenting and bring your spreadsheet to life!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top