Skip to content

Instantly share code, notes, and snippets.

@uebayasi
Created April 24, 2026 12:09
Show Gist options
  • Select an option

  • Save uebayasi/a26d816d85aca189ec172c0d58989cb4 to your computer and use it in GitHub Desktop.

Select an option

Save uebayasi/a26d816d85aca189ec172c0d58989cb4 to your computer and use it in GitHub Desktop.
Using Google Script REST API (script_v1) from within GAS
import { script_v1 } from 'googleapis'
import { request } from './request'
#
# - You might want to call Script REST API from within GAS
# - googleapis node client does not work on GAS
# - You still want to re-use type definitions
#
# If you need more entries, check
# https://github.com/googleapis/google-api-nodejs-client/blob/main/src/apis/script/v1.ts
#
export type GoogleAPis = {
script: {
projects: {
create: (
body: script_v1.Params$Resource$Projects$Create['requestBody'],
params?: Omit<script_v1.Params$Resource$Projects$Create, 'requestBody'>
) => script_v1.Schema$Project
get: (
scriptId: string,
params?: script_v1.Params$Resource$Projects$Get
) => script_v1.Schema$Project
}
}
}
export const googleapis: GoogleAPis = {
script: {
projects: {
create: (
body: script_v1.Params$Resource$Projects$Create['requestBody'],
params?: Omit<script_v1.Params$Resource$Projects$Create, 'requestBody'>
) =>
request<script_v1.Schema$Project>({
path: `projects`,
method: 'post',
params,
body,
}),
get: (
scriptId: string,
params?: script_v1.Params$Resource$Projects$Get
) =>
request<script_v1.Schema$Project>({
path: `projects/${scriptId}`,
method: 'get',
params,
})
}
}
}
const URL = 'https://script.googleapis.com/v1'
const e = encodeURIComponent
const makeQueryString = (params?: any): string =>
params
? Object.keys(params)
.map((key) => `${e(key)}=${e(params[key])}`)
.join('&')
: ''
export function request<T>(meta: {
path: string
method: GoogleAppsScript.URL_Fetch.HttpMethod
params?: any
body?: any
}): T {
const queryString = makeQueryString(meta.params)
const url = `${URL}/${meta.path}${queryString && '?' + queryString}`
const res = UrlFetchApp.fetch(url, {
method: meta.method,
headers: { Authorization: `Bearer ${ScriptApp.getOAuthToken()}` },
contentType: 'application/json',
payload: meta.body ? JSON.stringify(meta.body) : undefined,
muteHttpExceptions: true,
})
if (res.getResponseCode() >= 400) throw new Error(res.getContentText())
return JSON.parse(res.getContentText()) as T
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment