Create a named space in Google Chat (original) (raw)

This guide explains how to create a named space using thecreate()method on the Space resource of the Google Chat API.

A named space (where thespaceType isSPACE) is a place where people send messages, share files, and collaborate. Named spaces can include Chat apps. Named spaces have space managers who can apply administrative settings, descriptions, and add or remove people and apps.

To create different types of Chat spaces (including direct messages or group messages), use the setUp() method on theSpace resource to create the space and add members at the same time. For details, Set up a space.

After creating a named space, the only member of the space is the authenticated user. To add members to the space, call thecreate() method on the Membership resource for each person or app that you want to add. Or, you can use the setUp() method to create a named space and add members to it at the same time.

Prerequisites

Node.js

Python

Java

Apps Script

To create a named space withuser authentication, pass the following in your request:

Here's how to create a named space:

Node.js

Python

Java

Apps Script

Create a named space as a Chat app

App authentication requires one-timeadministrator approval.

To invite or add a user to a space withapp authentication, pass the following in your request:

Write a script that calls Chat API

Here's how to create a named space:

Python

  1. In your working directory, create a file named chat_space_create_named_app.py.
  2. Include the following code in chat_space_create_named_app.py:
from google.oauth2 import service_account  
from apiclient.discovery import build  
# Define your app's authorization scopes.  
# When modifying these scopes, delete the file token.json, if it exists.  
SCOPES = ["https://www.googleapis.com/auth/chat.app.spaces.create"]  
def main():  
    '''  
    Authenticates with Chat API using app authentication,  
    then creates a Chat space.  
    '''  
    # Specify service account details.  
    creds = (  
        service_account.Credentials.from_service_account_file('credentials.json')  
        .with_scopes(SCOPES)  
    )  
    # Build a service endpoint for Chat API.  
    chat = build('chat', 'v1', credentials=creds)  
    # Use the service endpoint to call Chat API.  
    result = chat.spaces().create(  
      # Details about the space to create.  
      body = {  
        # To create a named space, set spaceType to SPACE.  
        'spaceType': 'SPACE',  
        # The user-visible name of the space.  
        'displayName': 'API-made',  
        # The customer ID of the Workspace domain.  
        'customer': 'CUSTOMER'  
      }  
      ).execute()  
    # Prints details about the created space.  
    print(result)  
if __name__ == '__main__':  
    main()  
  1. In the code, replace the following:
    • CUSTOMER: the customer ID of the domain of the space in the format customer/{customer} where {customer} is the IDfrom theAdmin SDK customer resource. To create a space in the same Google Workspace organization as the Chat app, use customers/my_customer.
  2. In your working directory, build and run the sample:
python3 chat_space_create_named_app.py  

Open the space in Google Chat

To navigate to the space, use the space's resource ID to build the space's URL. You can find the resource ID from the spacename in the Google Chat response body. For example, if your space'sname is spaces/1234567, you can navigate to the space using the following URL: https://mail.google.com/chat/u/0/#chat/space/1234567.

Limitations and considerations