# Using Google Apps Scripts

Using Speckle in Google Apps Scripts (opens new window) is very simple, and fun! With just a few lines of code, you'll be able to send, receive and leverage our API from Google Sheets, Docs etc.

Here's a few examples to get you started.

# Prerequisites

First of all we need to create a Personal Access Token, make sure to select the scopes that you'll need for running your queries and mutations. As always, treat this token as a password!

# Writing your queries

To learn how to write queries check out our section on the Server API, and use the GraphQL explorer to make sure your query or mutation works as expected.

Next, since App Scripts doesn't have a GraphQL library we need to transform them a bit to work with UrlFetchApp, it's just a matter of setting the right payload and parameters.

Here's what a function that runs a query to get the currently logged user looks like, remember to replace YOUR_SERVER_ADDRESS and YOUR_PERSONAL_TOKEN. You can extend its fields to get the streams, branches and commits as well:

function getMe() {
  let url = "https://YOUR_SERVER_ADDRESS/graphql";
  let graphql = JSON.stringify({
    query: `query User {
                      user{
                        id,
                        email,
                        name
                      }
                    }`,
    variables: null,
  });
  let params = {
    method: "POST",
    payload: graphql,
    headers: {
      "Content-Type": "application/json",
      Authorization: "Bearer YOUR_PERSONAL_TOKEN",
    },
  };
  var response = UrlFetchApp.fetch(url, params);

  Logger.log(response);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

TIP

How do you create a token, we hear you ask? Read the section on personal access tokens!

Here's a function that runs a mutation to create a new stream:

function createStream() {
  let url = "https://YOUR_SERVER_ADDRESS/graphql";
  let graphql = JSON.stringify({
    query: `mutation streamCreate($myStream: StreamCreateInput!) { streamCreate(stream: $myStream) }`,
    variables: {
      myStream: {
        name: "Sample Stream",
        description: "Created from Google Sheets!",
      },
    },
  });
  let params = {
    method: "POST",
    payload: graphql,
    headers: {
      "Content-Type": "application/json",
      Authorization: "Bearer YOUR_PERSONAL_TOKEN",
    },
  };
  var response = UrlFetchApp.fetch(url, params);

  Logger.log(response);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

This is all! If you have any questions feel free to ping us on the 👉 forum (opens new window).

Last Updated: 10/20/2021, 9:55:23 AM