Upload media as a file attachment

This guide explains how to use the Google Chat API to upload media (a file) to Google Chat and then attach it to a message.



  • Python 3.6 or greater
  • The pip package management tool
  • The Google client libraries for Python. To install them, run the following command in your command-line interface:

    pip3 install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib oauth2client
  • A published Chat app. To create and publish a Chat app, see Build a Google Chat app.

  • Authorization configured for the Chat app. Uploading media as a file attachment requires User authentication with the chat.messages.create or chat.messages authorization scope.

Upload as a file attachment

To upload media and attach it to a message, pass the following in your request::

  • Specify the chat.messages.create or chat.messages authorization scope.
  • Call the following Google Chat methods:
    1. To upload the file, call the upload method on the Media resource.
      • Set parent to the space name of the space that hosts the file.
      • In body (the request body), set filename to the name of the uploaded file attachment.
      • Set media_body as an instance of the file to be uploaded.
    2. To create a message with the uploaded file attached, call the create method on the Messages resource.

The following example uploads a PNG image file and attaches it to a message.


  1. In your working directory, create a file named chat_media_and_attachment_upload.py.
  2. Include the following code in chat_media_and_attachment_upload.py:

    import os.path
    from google.auth.transport.requests import Request
    from google.oauth2.credentials import Credentials
    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build
    from googleapiclient.errors import HttpError
    from googleapiclient.http import MediaFileUpload
    # 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.messages.create"]
    def main():
        Authenticates with Chat API via user credentials,
        then uploads a file as media, creates a message, and
        attaches the file to the message.
        # Authenticate with Google Workspace
        # and get user authorization.
        flow = InstalledAppFlow.from_client_secrets_file(
                      'client_secrets.json', SCOPES)
        creds = flow.run_local_server()
        # Build a service endpoint for Chat API.
        service = build('chat', 'v1', credentials=creds)
        # Upload a file to Google Chat.
        media = MediaFileUpload('test_image.png', mimetype='image/png')
        # Create a message and attach the uploaded file to it.
        attachment_uploaded = service.media().upload(
            # The space to upload the attachment in.
            # Replace SPACE with a space name.
            # Obtain the space name from the spaces resource of Chat API,
            # or from a space's URL.
            # The filename of the attachment, including the file extension.
            body={'filename': 'test_image.png'},
            # Media resource of the attachment.
        # Create a Chat message with attachment.
        result = service.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.
            # Must match the space name that the attachment is uploaded to.
            # The message to create.
                'text': 'Hello, world!',
                'attachment': [attachment_uploaded]
    if __name__ == '__main__':
  3. In the code, replace SPACE with the space name to upload the attachment in, which you can obtain from the spaces.list method in the Chat API, or from a space's URL.

  4. In your working directory, build and run the sample:

    python3 chat_media_and_attachment_upload.py

The Chat API returns a response body containing attachmentDataRef with details about the uploaded file.

Limits and considerations

As you prepare to upload files and attach them to messages, take note of these limits and considerations: