This page explains how to create and send card-based messages as a Google Chat app. Cards support a defined layout, interactive UI elements like buttons, and rich media like images. To learn more about the ways you can use cards, see Design dynamic, interactive, and consistent UIs with cards. For more information about messages, see Google Chat messages overview.
Prerequisites
Node.js
- A Google Workspace account with access to Google Chat.
- A Chat app. To build a Chat app, follow this quickstart.
Note: The Node.js code samples in this guide are written to run as a Google Cloud Function.
Python
- A Google Workspace account with access to Google Chat.
- A Chat app. To build a Chat app, follow this quickstart.
Note: The Python code samples in this guide are written to run as a Google Cloud Function, using Python 3.9.
Apps Script
- A Google Workspace account with access to Google Chat.
- A Chat app. To build a Chat app, follow this quickstart.
Create card messages
This section describes how to create card messages in two ways: by responding to a user interaction and calling the Google Chat API asynchronously.
Respond to a user
Chat apps can create card messages to respond to a user interaction, such as when a user sends the Chat app a message or adds the Chat app to a space. To learn more about responding to user interactions, see Receive and respond to Chat app interaction events.
In this example, a user sends a message to a Chat app and the Chat app responds by sending a card message that displays the user's name and avatar image:
Node.js
Python
Apps Script
Call the Google Chat API
The following explains how to create a card message by asynchronously calling the Google Chat API.
Calling the Chat API asynchronously requires authentication. Since only Chat apps can create card messages, a Chat app must use app authentication to create and send card messages (Chat apps can't use user authentication to send card messages on behalf of users). To learn more, see Google Chat authentication overview.
In this example, you create the following card message using app authentication:

To set up authentication and learn about creating messages asynchronously, see the Chat API guide.
Python
- In your working directory, create a file named
chat_create_card_message.py
. Include the following code in
chat_create_card_message.py
:from httplib2 import Http from oauth2client.service_account import ServiceAccountCredentials from apiclient.discovery import build # Specify required scopes. SCOPES = ['https://www.googleapis.com/auth/chat.bot'] # Specify service account details. CREDENTIALS = ServiceAccountCredentials.from_json_keyfile_name( 'credentials.json', SCOPES) # Build the URI and authenticate with the service account. chat = build('chat', 'v1', http=CREDENTIALS.authorize(Http())) # Create a Chat message. result = chat.spaces().messages().create( # The space to create the message in. # # Replace SPACE with a space name. # Obtain the space name from the spaces resource of Chat API, # or from a space's URL. parent='spaces/SPACE', # The message to create. body= { 'cardsV2': [{ 'cardId': 'createCardMessage', 'card': { 'header': { 'title': 'A Card Message!', 'subtitle': 'Created with Chat REST API', 'imageUrl': 'https://developers.google.com/chat/images/chat-product-icon.png', 'imageType': 'CIRCLE' }, 'sections': [ { 'widgets': [ { 'buttonList': { 'buttons': [ { 'text': 'Read the docs!', 'onClick': { 'openLink': { 'url': 'https://developers.google.com/chat' } } } ] } } ] } ] } }] } ).execute() print(result)
In the code, replace
SPACE
with a space name, which you can obtain from thespaces.list()
method in the Chat API, or from a space's URL.In your working directory, build and run the sample:
python3 chat_create_card_message.py
To learn more about working with messages in the Chat API, see the following:
Troubleshoot
When a Google Chat app or card returns an error, the Chat interface surfaces a message saying "Something went wrong." or "Unable to process your request." Sometimes the Chat UI doesn't display any error message, but the Chat app or card produces an unexpected result; for example, a card message might not appear.
Although an error message might not display in the Chat UI, descriptive error messages and log data are available to help you fix errors when error logging for Chat apps is turned on. For help viewing, debugging, and fixing errors, see Troubleshoot and fix Google Chat errors.
Related topics
- Open interactive dialogs: Create cards that open in a window.
- Read form data input by users on cards: Create cards that let users input information, such as a text field or date time picker.
- Format a message: Learn how to format text in messages.
- Get a message: Get details about a message, like when it was sent or what it says.
- List messages: See a paginated, filterable list of messages in a space.
- Update a message: Change message attributes, such as what it says. You can also append text to a card message, or a card to a text message.
- Delete a message: Remove a message from a space.