AI-generated Key Takeaways
-  The Google Chat API allows you to programmatically add users, Google Groups, or Chat apps to spaces using the create()method and specifying the member type and identifier.
-  Authentication can be done as a Chat user, a Chat app (with limitations), or a Google Workspace administrator with useAdminAccess.
-  Google Groups can only be added to named spaces, not direct messages or group chats, using user authentication with the chat.membershipsscope.
-  When adding members, consider the auto-accept policy: if enabled, the member joins directly; otherwise, they receive an invitation. 
-  Refer to the provided code samples (Node.js, Python, Java, Apps Script) and replace placeholders with your specific values. 
This guide explains how to use the  create() 
 
method on the Membership 
resource of the Google Chat API to invite or add a user,
Google Group, or Chat app to a space also known as
creating a membership. When creating a membership, if the specified member has
their auto-accept policy turned off, then they're invited, and must accept the
space invitation before joining. Otherwise, creating a membership adds the
member directly to the specified space.
If you're a Google Workspace administrator, you can add users, Google Groups, or Chat apps to any space in your Google Workspace organization.
The  Membership 
resource 
represents whether a human user or Google Chat app is invited to,
part of, or absent from a space.
Prerequisites
Node.js
- A Business or Enterprise Google Workspace account with access to Google Chat .
- Set up your environment: - Create a Google Cloud project .
- Configure the OAuth consent screen .
- Enable and configure the Google Chat API with a name, icon, and description for your Chat app.
- Install the Node.js Cloud Client Library .
- Create access credentials based on how you want to authenticate in your Google Chat API
        request: - To authenticate as a Chat user, create OAuth client ID
            credentials 
and save the credentials as a JSON file named credentials.jsonto your local directory.
- To authenticate as the Chat app, create service account
            credentials 
and save the credentials as a JSON file named credentials.json.
 
- To authenticate as a Chat user, create OAuth client ID
            credentials 
and save the credentials as a JSON file named 
 
- Choose an authorization scope based on whether you want to authenticate as a user or the Chat app.
Python
- A Business or Enterprise Google Workspace account with access to Google Chat .
- Set up your environment: - Create a Google Cloud project .
- Configure the OAuth consent screen .
- Enable and configure the Google Chat API with a name, icon, and description for your Chat app.
- Install the Python Cloud Client Library .
- Create access credentials based on how you want to authenticate in your Google Chat API
        request: - To authenticate as a Chat user, create OAuth client ID
            credentials 
and save the credentials as a JSON file named credentials.jsonto your local directory.
- To authenticate as the Chat app, create service account
            credentials 
and save the credentials as a JSON file named credentials.json.
 
- To authenticate as a Chat user, create OAuth client ID
            credentials 
and save the credentials as a JSON file named 
 
- Choose an authorization scope based on whether you want to authenticate as a user or the Chat app.
Java
- A Business or Enterprise Google Workspace account with access to Google Chat .
- Set up your environment: - Create a Google Cloud project .
- Configure the OAuth consent screen .
- Enable and configure the Google Chat API with a name, icon, and description for your Chat app.
- Install the Java Cloud Client Library .
- Create access credentials based on how you want to authenticate in your Google Chat API
        request: - To authenticate as a Chat user, create OAuth client ID
            credentials 
and save the credentials as a JSON file named credentials.jsonto your local directory.
- To authenticate as the Chat app, create service account
            credentials 
and save the credentials as a JSON file named credentials.json.
 
- To authenticate as a Chat user, create OAuth client ID
            credentials 
and save the credentials as a JSON file named 
 
- Choose an authorization scope based on whether you want to authenticate as a user or the Chat app.
Apps Script
- A Business or Enterprise Google Workspace account with access to Google Chat .
- Set up your environment: - Create a Google Cloud project .
- Configure the OAuth consent screen .
- Enable and configure the Google Chat API with a name, icon, and description for your Chat app.
- Create a standalone Apps Script project , and turn on the Advanced Chat Service .
- In this guide, you must use either user or app authentication . To authenticate as the Chat app, create service account credentials. For steps, see Authenticate and authorize as a Google Chat app .
 
- Choose an authorization scope based on whether you want to authenticate as a user or the Chat app.
Invite or add a user to a space as a user
To invite or add a user to a space with user authentication , pass the following in your request:
- Specify the chat.membershipsauthorization scope.
- Call the  CreateMembership()method.
- Pass parentas the resource name of the space in which to create membership.
- Pass membershipas an instance ofMembershipwith its fieldmemberset with the following:- The typefield set toHUMAN.
- The namefield set tousers/{user}where{user}is the person that you want to add to the space. To specify the Chat user , replace{user}with any of the following:- The ID for the person 
in the People API. For example, if the People API person 
 resourceNameispeople/123456789, then user the valueusers/123456789.
- The ID for the user in the Directory API.
- The user's email address. For example, users/222larabrown@gmail.comorusers/larabrown@cymbalgroup.com. If the user uses a Google Account or belongs to a different Google Workspace organization, you must use their email address.
 
- The ID for the person 
in the People API. For example, if the People API person 
 
 
- The 
The following example adds a user to a space with user authentication :
Node.js
Python
Java
Apps Script
To run the sample, replace the following:
-  SPACE_NAME: the ID from the space'sname. You can obtain the ID by calling theListSpaces()method or from the space's URL.
-  USER_NAME: a user ID.
The Chat API returns an instance of  Membership 
 
that details the user membership that was created.
Invite or add a Google Group to a space
To invite or add a Google Group to a space with user authentication ( App authentication doesn't support inviting or adding a Google Group to a space), pass the following in your request:
- Specify the chat.membershipsauthorization scope.
- Call the  CreateMembership()method.
- Pass parentas the resource name of the space in which to create membership.
- Pass membershipas an instance ofMembershipwith the fieldnameofgroupMemberset togroups/{group}where{group}is the group ID that you want to create membership for. The ID for the group can be retrieved using the Cloud Identity API .
Google Groups can't be added to a group chat or direct message, but only to a named space.
The following example adds a group to a named space with user authentication:
Node.js
Python
Java
Apps Script
To run the sample, replace the following:
-  SPACE_NAME: the ID from the space'sname. You can obtain the ID by calling theListSpaces()method or from the space's URL.
-  GROUP_NAME: a group ID.
The Chat API returns an instance of  Membership 
 
that details the user membership that was created.
Add a Chat app to a space
A Chat app can't add another app as a member to a space. To add a Chat app to a space or a direct message between two human users, pass the following in your request with user authentication ( App authentication doesn't support inviting or adding a Chat app to a space):
- Specify the chat.memberships.appauthorization scope.
- Call the  CreateMembership()method.
- Pass parentas the resource name of the space in which to create membership.
- Pass membershipas an instance ofMembershipwith its fieldmemberset with the following:- The typefield set toBOT.
- The namefield set tousers/app; an alias that represents the app calling the Chat API.
 
- The 
The following example adds a Chat app to a space:
Node.js
Python
Java
Apps Script
To run the sample, replace  SPACE_NAME 
 
with the ID from
the space's  name 
 
.
You can obtain the ID by calling the  ListSpaces() 
 
method or from the space's URL.
The Chat API returns an instance of  Membership 
 
that details the user membership that was created.
Invite or add a user to a space as a Chat app
App authentication requires one-time administrator approval .
To invite or add a user to a space with app authentication , pass the following in your request:
- Specify the chat.app.membershipsauthorization scope.
- Call the  createmethod on themembershipresource .
- Set parentto the resource name of the space in which to create membership.
- Set membertousers/{user}where{user}is the person that you want to create membership for, and is either:- The ID for the person 
in the People API. For example, if the People API person 
 resourceNameispeople/123456789, then setmembership.member.nametousers/123456789.
- The ID for the user in the Directory API.
- The user's email address. For example, users/222larabrown@gmail.comorusers/larabrown@cymbalgroup.com. If the user uses a Google Account or belongs to a different Google Workspace organization, you must use their email address.
 
- The ID for the person 
in the People API. For example, if the People API person 
 
Write a script that calls Chat API
The following example adds a user to a space with app authentication :
Python
- In your working directory, create a file named chat_membership_app_create.py.
-  Include the following code in chat_membership_app_create.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.memberships" ] def main (): ''' Authenticates with Chat API using app authentication, then adds a user to a Chat space by creating a membership. ''' # 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 () . members () . create ( # The space in which to create a membership. parent = 'spaces/ SPACE ' , # Specify which user the membership is for. body = { 'member' : { 'name' : 'users/ USER ' , 'type' : 'HUMAN' } } ) . execute () # Prints details about the created membership. print ( result ) if __name__ == '__main__' : main ()
-  In the code, replace the following: -  SPACE: a space name, which you can obtain from thespaces.listmethod in the Chat API, or from a space's URL.
-  USER: a user ID.
 
-  
-  In your working directory, build and run the sample: python3 chat_membership_app_create.py
Add users or Google Groups to a space as a Google Workspace administrator
If you're a Google Workspace administrator, you can call the create() 
method to add users, Google Groups, or Chat apps to any space in
your Google Workspace organization.
To call this method as a Google Workspace administrator, do the following:
- Call the method using user authentication, and specify an authorization scope that supports calling the method using administrator privileges .
- In your request, specify the query parameter useAdminAccesstotrue.
For more information and examples, see Manage Google Chat spaces as a Google Workspace administrator .
Limitations and considerations
- With App authentication 
,
a Chat app can invite or add users, but not
Google Groups or Chat apps. To add itself, a
Chat app must use user authentication 
with the chat.membershipsauthorization scope.

