Author: By Raj
Part of our Apps Script API Integrations guides. Need this built for your team? Hire a Google Apps Script developer.
Estimated reading time: 10 minutes
How to Call Any REST API from Google Apps Script (UrlFetchApp Guide)
UrlFetchApp is the gateway from Google Workspace to Shopify, Stripe, internal microservices, and legacy REST endpoints. Master GET/POST, headers, JSON, file uploads, and retries here.
Always set muteHttpExceptions: true when you need response bodies on 4xx errors. Parse with JSON.parse inside try/catch and log response.getResponseCode().
Pair with /blog/apps-script-oauth2-guide when endpoints require bearer tokens refreshed hourly.
GET with query parameters
Build query strings with URLSearchParams pattern manually: base + '?' + Object.keys(params).map(k => k+'='+encodeURIComponent(params[k])).join('&').
Cache GET responses in CacheService for 300 seconds when data is public and slow-changing.
POST JSON and form bodies
Use payload: JSON.stringify(obj) and contentType: 'application/json'. For form urlencoded, use payload: { a: 1, b: 2 } object form.
Binary uploads need blob.getBytes() in advanced options, consult API docs.
Retries and idempotency
Retry 5xx and 429 with exponential backoff. Send Idempotency-Key header on POST payments where API supports it.
Persist idempotency keys per row in Sheet to prevent duplicate charges on script rerun.
UrlFetch quotas
Each fetch counts toward daily limits, see /blog/google-apps-script-quotas-and-limits. Collapse ten GETs into one bulk endpoint when your API allows.
Log fetch count per run in Script Properties for ops dashboards.
Example code
function apiGet(url, headers) {
const res = UrlFetchApp.fetch(url, {
method: 'get',
headers: headers || {},
muteHttpExceptions: true,
});
if (res.getResponseCode() >= 400) {
throw new Error('GET failed ' + res.getResponseCode() + ': ' + res.getContentText().slice(0, 200));
}
return JSON.parse(res.getContentText());
}| Approach | Best for | Tradeoff |
|---|---|---|
| Apps Script native | Google Workspace-centric workflows | 6-min limit, quotas |
| Zapier / Make | No-code, many connectors | Per-task cost, vendor lock-in |
| Python + Cloud | Heavy data / ML | Hosting cost, separate auth |
| API integration services | Production custom logic | Build cost, you own code |
FAQ
Can UrlFetchApp call localhost?
No. Targets must be public HTTPS endpoints reachable from Google's servers.
How do I send Bearer tokens?
headers: { Authorization: 'Bearer ' + token } alongside muteHttpExceptions as needed.
Does UrlFetch follow redirects on POST?
Be cautious, some APIs break on redirect. Log final response code and disable followRedirects if docs require.
Can I parse XML responses?
Use XmlService.parse(response.getContentText()) for legacy enterprise APIs.
Shopify example?
See /blog/connect-shopify-api-to-google-sheets for Admin API headers X-Shopify-Access-Token pattern.
Need this done for you? I handle this as part of my consulting work, fixed-price quote within 24 hours.
Book a call with Raj →Get the full How to Call Any REST API from Google Apps Script (UrlFetchApp Guide) script template
I'll email you a production-ready, commented version you can deploy in 10 minutes.
Continue reading
API Integrations
Connect Shopify API to Google Sheets with Apps Script
API Integrations
Send automated SMS from Google Sheets (Twilio): Production Apps Script Guide
API Integrations
Google Apps Script vs Zapier vs Make: Which Should You Use?
From another topic
How to Automate Google Sheets with Apps Script (Beginner Guide) →Need help with this? I handle this as part of my Apps Script API Integrations service.
Shopify, Stripe, Slack, HubSpot, webhooks, and REST API connections.
See how it works →