Created
April 24, 2026 12:09
-
-
Save uebayasi/a26d816d85aca189ec172c0d58989cb4 to your computer and use it in GitHub Desktop.
Using Google Script REST API (script_v1) from within GAS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | |
| }) | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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