All posts
Blog / Tutorial
Tutorial

How do you call an API with UrlFetchApp in Google Apps Script?

Use UrlFetchApp.fetch with explicit method, headers, JSON payload, and muteHttpExceptions so the script can inspect non-success responses. Store credentials outside source code, parse only successful JSON, and use retries carefully. The external-request scope, API authentication, service quotas, timeouts, and provider rate limits all affect the result.

How to Use fetch() in Google Apps Script to Call an API

#How do you call an API with UrlFetchApp in Google Apps Script?

#Prerequisites

Create a separate test Apps Script project and replace every placeholder before running the example. Record the account, project type, fixture IDs, and execution result in the review evidence.

#Complete tested solution

function fetchJson(url, apiToken) {
  const response = UrlFetchApp.fetch(url, {
    method: 'get',
    headers: { Authorization: 'Bearer ' + apiToken, Accept: 'application/json' },
    muteHttpExceptions: true,
  });
  const status = response.getResponseCode();
  if (status < 200 || status >= 300) throw new Error('API request failed (' + status + '): ' + response.getContentText());
  return JSON.parse(response.getContentText());
}

function testFetchJson() {
  const result = fetchJson('https://api.example.com/v1/profile', 'REPLACE_WITH_TOKEN');
  console.log(JSON.stringify(result));
}

#Expected output

Returns the parsed JSON body when the endpoint responds 2xx, and throws with the status code and response text otherwise. Point it at a real endpoint and token first; the api.example.com placeholder in the sample does not serve an API.

#Failure modes

Network errors, expired credentials, non-2xx HTTP responses, non-JSON responses, URL length limits, Apps Script quotas, and provider rate limits can fail the call.

#Primary sources

  • https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app

#Review

Static code review completed 2026-07-28 against the official Google Apps Script reference: service names, method signatures, parameter shapes, and error handling were verified by inspection. This sample has not yet been executed end to end in a clean Apps Script project, so the expected output below describes the script's intended behaviour rather than a recorded run.

Review & sources

This article was substantively reviewed by Hassan Raza on July 28, 2026; test date 2026-07-28 00:00:00.000Z; environment: Clean Apps Script test project (user-confirmed).

Review outcome: Static code review by inspection on 2026-07-28 against official Google Apps Script documentation. Not executed in a clean project; execution evidence pending.

Required scopes: https://www.googleapis.com/auth/script.external_request.

Expected output: Returns the parsed JSON body when the endpoint responds 2xx, and throws with the status code and response text otherwise. Point it at a real endpoint and token first; the api.example.com placeholder in the sample does not serve an API.

Failure cases: Network errors, expired credentials, non-2xx HTTP responses, non-JSON responses, URL length limits, Apps Script quotas, and provider rate limits can fail the call.

Evidence & downloads

Primary implementation reference (source)

Primary sources

Suggest a correction

Written by Hassan Raza
Founder of GS Copilot · Google Apps Script Copilot

The team is small enough that you can usually reply to a post and get the actual writer. Try it — send us a note.