clasp (Command Line Apps Script Projects)

repository·master·Indexed 26 days ago

https://github.com/google/clasp

A CLI tool that allows developers to develop Google Apps Script projects locally, enabling the use of local file systems, source control like Git, and modern development tools. It provides commands for project management, including creating, cloning, pushing, and pulling code, as well as advanced features like tailing execution logs and managing deployments. Version 3.3.0 supports integration as a Gemini CLI extension and a Claude Code CLI extension.

Tokens
13.2K
Snippets
24
Records
107
Agent score
90%

What's inside clasp

  1. Associate a Google Cloud Platform project with your script

    master

    To use a specific Google Cloud Platform (GCP) project with your script, you must associate it via the Apps Script editor:

    1. Run clasp open to open the script in your browser.
    2. In the Apps Script editor, click Resources > Cloud Platform project....
    3. Enter your GCP project ID (e.g., project-id-xxxxxxxxxxxxxxxxxxx).
    4. Once associated, you can specify this ID in the projectId field of your .clasp.json file.
  2. Install clasp as a Gemini CLI Extension

    master

    You can install clasp as a Gemini CLI extension to make it available as an MCP server.

    Prerequisites:

    1. Enable the Google Apps Script API.
    2. Perform a clasp login with your specific login parameters before using the extension.
    gemini extensions install https://github.com/google/clasp
  3. Migrate from clasp 2.x to 3.x

    master

    TypeScript Support

    Clasp 3.x no longer transpiles TypeScript code. You must use a bundler (like Rollup) to transform TypeScript into JavaScript before pushing with clasp push.

    Command Renames

    Several commands have been renamed for consistency. Note that many old commands retain aliases for compatibility.

    2.x Command3.x Command
    openopen-script
    open --webopen-web-app
    open --addonopen-container
    open --credsopen-credentials-setup
    login --creds <file>login -u <name> --creds <file>
    logs --openopen-logs
    apis --openopen-api-console
    apis enable <api>enable-api <api>
    apis disable <api>disable-api <api>
    deploy -i <id>update-deployment <id>
  4. Set up clasp run to execute Apps Script functions

    master

    To use clasp run [functionName] for remote execution of Apps Script functions, you must complete the following configuration steps:

    1. Configure Project ID: Ensure your .clasp.json contains the projectId (the GCP Project ID, not the Script ID).
      {
        "scriptId": "...",
        "projectId": "my-sample-project-191923"
      }
    2. Link GCP Project:
      • Open the Google Cloud Consent screen and set the Application name to clasp project.
      • Run clasp open-script.
      • In the Apps Script editor, go to ⚙️ Project Settings > Google Cloud Platform (GCP) Project and ensure the correct Project Number is set.
    3. Configure OAuth Credentials:
      • Run clasp open-credentials-setup to follow the guide for creating a Desktop App OAuth client ID.
      • Download the credentials and save them as client_secret.json in your project directory.
    4. Configure Manifest (appsscript.json):
      • Add the executionApi configuration:
        "executionApi": {
          "access": "ANYONE"
        }
      • Ensure all required OAuth scopes (e.g., Gmail, Drive) are explicitly listed in the manifest.
    5. Authorize via CLI: Run clasp login using your custom credentials and the project scopes: clasp login --user <name> --use-project-scopes --creds client_secret.json
    6. Enable API: If using Google Workspace, enable the Apps Script API in the Google Cloud Console.
    7. Deploy as API Executable: If you encounter a "Script API executable not published/deployed." error, you must deploy the script:
      • Run clasp open-script.
      • Click Deploy > New deployment.
      • Select the type API Executable and click Deploy.
  5. Run an Apps Script function with clasp run

    master

    Once configured, you can execute Apps Script functions remotely.

    1. Push your local files to the script: clasp push.
    2. Execute the function:
      • Interactive mode: Run clasp run. You will be prompted to select a function from a list.
      • Direct mode: Run clasp run <functionName> (e.g., clasp run helloWorld).

    The function's result will be displayed in your terminal output.

    clasp run helloWorld
  6. Configure a custom Google Cloud Project for clasp

    master

    To improve security or comply with organizational restrictions, you can use your own OAuth client instead of the default clasp client.

    1. Create a Project: Create a new project in the Google Cloud Console.
    2. Create an OAuth Client: Create a client of type Desktop Application. Download the client secrets file. Use this file with clasp login --creds <filename>.
    3. Enable Required APIs:
      • Apps Script API (script.googleapis.com) - Required
      • Service Usage API (serviceusage.googleapis.com) - Required
      • Google Drive API (drive.googleapis.com) - Required
      • Cloud Logging API (logging.googleapis.com) - Required

    If registering OAuth scopes for external use, include the following:

    • https://www.googleapis.com/auth/script.deployments
    • https://www.googleapis.com/auth/script.projects
    • https://www.googleapis.com/auth/script.webapp.deploy
    • https://www.googleapis.com/auth/drive.metadata.readonly
    • https://www.googleapis.com/auth/drive.file
    • https://www.googleapis.com/auth/service.management
    • https://www.googleapis.com/auth/logging.read
    • https://www.googleapis.com/auth/userinfo.email
    • https://www.googleapis.com/auth/userinfo.profile
    • https://www.googleapis.com/auth/cloud-platform
  7. Find the Google Script ID for .clasp.json

    master

    To populate the scriptId field in your .clasp.json file, follow these steps:

    1. Open your Google Script project URL in a browser.
    2. Navigate to File > Project properties > Script ID.
    3. Copy the ID and paste it into the scriptId field of your .clasp.json file.
  8. Run functions requiring specific OAuth scopes

    master

    If your Apps Script function requires specific scopes (like Gmail or Drive), you must synchronize your clasp login credentials with the scopes declared in your appsscript.json manifest.

    1. Add the required scopes to your appsscript.json file.
    2. Re-authenticate using the --use-project-scopes flag to combine manifest scopes with default clasp scopes: clasp login --user <name> --use-project-scopes --include-clasp-scopes --creds client_secret.json
    3. To add one-off scopes without modifying the manifest, use the --extra-scopes flag with a comma-separated list: clasp run --user <name> --extra-scopes scopeA,scopeB <functionName>
  9. Install clasp as a Claude Code CLI Extension

    master

    There are two ways to use clasp with Claude Code CLI:

    Run this command directly in Claude Code:

    /plugin install @google/clasp

    2. Manual Installation

    Add clasp as an MCP server using one of these methods:

    Via npx:

    claude mcp add clasp -- npx -y @google/clasp mcp

    Via JSON configuration file:

    claude mcp add-json clasp "$(cat claude-mcp.json)"
  10. Manage multiple users with clasp

    master

    You can use the global --user option to switch between different authorized accounts. This is useful for running commands as different users or when using clasp run-function.

    clasp login # Saves as default credentials
    clasp clone # User not specified, runs using default credentials
    clasp login --user testaccount # Authorized new named credentials
    clasp run-function --user testaccount myFunction # Runs function as test account
    clasp login --user testaccount
    clasp run-function --user testaccount myFunction