Press "Enter" to skip to content

Automatically Include Your Latest Tweet in Your Gmail Signature

Down below you’ll find a bit of code I wrote to have Google Apps Script automatically get my latest tweet and include it in my Gmail signature. Is this necessary? No. Is it kind of cool and will it make your friends jealous? You tell me.

First, a shout-out to Wesley Chun (@wescpy) for the inspiration. And to Martin Hawksey (@mhawksey) for his wonderful TwtrService which made working with Twitter’s API a breeze. Let’s get started.

Getting Apps Script to talk to Twitter

The steps below will walk you through creating a Twitter app that can be called by TwtrService. In this example we’ll link these two pieces together to retrieve your latest tweet. But TwtrService is capable of a lot more.

  1. Create a new Twitter App at This app is what our Google Apps Script project will use to handle calls to Twitter’s API.

    IMPORTANT: Be sure to enter in the Callback URL field.
  2. Grab your newly created Twitter App’s API Key and API Secret. You can find these beneath the “Keys and Access Tokens” heading. Enter them on THIS PAGE.
  3. Click through the prompts to authorize Apps Script to connect to an external service, then sign in with the Twitter account you used to create the app in step 1. Authorize the app to talk to Google Apps script.
  4. If all went as planned, you should see the below screen. Go ahead and close the tab.

The code

We’ll first need to create a new Apps Script project and enable the Gmail API. Next we’ll add the TwtrService library to said Apps Script project and paste in the code that does the heavy lifting. The getLatestTweet function will retrieve your latest tweet and return it, along with some custom HTML, for use as your Gmail signature. The setSignature function calls getLatestTweet and sets whatever it gets back as your Gmail signature.

  1. Create a new project at Be sure to give it a title.
  2. Enable the Gmail API for this project under Resources–>Advanced Google services then click the link to enable the API in the Google Developers Console.
  3. Search for the Gmail API and click through to enable it. Close the API Manager tab.
  4. Return to your Apps Script project and click OK. Be sure that you’ve got the Gmail API enabled.
  5. Click on Resources–>Libraries…
  6. The key for the TwtrService library is MarIlVOhstkJA6QjPgCWAHIq9hSqx7jwh. Paste it in the proper field and click Select.
  7. Select the latest version of the library (14 as of today) and click Save.
  8. Paste in the code below. Be sure to put your Twitter handle in the proper place on lines 3, 14, & 16 as well as your Gmail address on line 24. Lastly, please be sure to modify the HTML on lines 12-16 suit your needs.

Testing functionality and automating the script

Let’s check to make sure the script is running as expected. If it is, we’ll setup a time-based trigger so that your Gmail signature is always up to date with your latest tweet.

  1. Select the getLatestTweet function from the drop-down and click the Run button.
  2. Look for errors under View–>Execution transcript
  3. If all’s well, go ahead and repeat steps 1&2, but this time select the setSignature function before running.
  4. Compose a Gmail message in a new tab to see your new Twitter-iffic signature.
  5.  To automate this process, let’s set a time-driven trigger for the setSignature function under Resources–>Current project’s triggers.


Thanks again to Wesley Chun (@wescpy) and Martin Hawksey (@mhawksey). Click here to read up on some of the cool things you can do with your Apps Script super powers and TwtrService. Also, be sure to check out the Google Developers YouTube Channel for more inspiration.

Like what you see? Share it on...


  1. Jorge Jorge

    Hello Matt,

    Thanks for your post. It’s very interesting tool. I go step by step but get error: “TypeError: No se puede leer la propiedad “id_str” de undefined. I don’t have any idea. Any suggestion?

    Thanks in advance

    • Matt Matt

      It’s hard to say what’s going wrong here. Can you please post the execution transcript here?

      • Jorge Jorge

        Hello Matt,

        Thanksfor your reply. I get the error “Missing required scope “” for modifying non-primary SendAs (línea 24, archivo “Código”)”
        when I check the “setSignture” function.

        Any idea?. Thanks

      • Jorge Jorge

        It’s posible to config in a setSignture function an Email address other than gmail? I set in this function “” and get that error: “Missing required scope “” for modifying non-primary SendAs (línea 24, archivo “Código”)”
        and this is the code of that line: ” var rsp = Gmail.Users.Settings.SendAs.patch(data, ‘me’, ‘’);”

        That is the log:
        [17-01-28 20:54:50:027 CET] Iniciando ejecución
        [17-01-28 20:54:50:043 CET] CacheService.getUserCache() [0,001 segundos]
        [17-01-28 20:54:50:063 CET] Cache.get([consumerKey]) [0,02 segundos]
        [17-01-28 20:54:50:065 CET] CacheService.getUserCache() [0,001 segundos]
        [17-01-28 20:54:50:084 CET] Cache.get([consumerSecret]) [0,019 segundos]
        [17-01-28 20:54:50:085 CET] CacheService.getScriptCache() [0,001 segundos]
        [17-01-28 20:54:50:104 CET] Cache.get([service_url]) [0,018 segundos]
        [17-01-28 20:54:50:106 CET] CacheService.getScriptCache() [0,001 segundos]
        [17-01-28 20:54:50:125 CET] Cache.get([hash]) [0,019 segundos]
        [17-01-28 20:54:50:128 CET] CacheService.getUserCache() [0,001 segundos]
        [17-01-28 20:54:50:148 CET] Cache.get([oauth_token]) [0,019 segundos]
        [17-01-28 20:54:50:150 CET] CacheService.getUserCache() [0,001 segundos]
        [17-01-28 20:54:50:171 CET] Cache.get([oauth_token_secret]) [0,021 segundos]
        [17-01-28 20:54:50:352 CET] UrlFetchApp.fetch([, {headers={Authorization=OAuth realm=””,oauth_consumer_key=”e7kUqpdbnAsLOB6DNqAcEavgg”,oauth_token=”2453066827-AAsd}, =, =}]…) [0,098 segundos]
        [17-01-28 20:54:50:353 CET] HTTPResponse.getResponseCode() [0 segundos]
        [17-01-28 20:54:50:353 CET] HTTPResponse.getContentText() [0 segundos]
        [17-01-28 20:54:50:437 CET] UrlFetchApp.fetch([]) [0,083 segundos]
        [17-01-28 20:54:50:760 CET] Ejecución fallida: Missing required scope “” for modifying non-primary SendAs (línea 24, archivo “Código”) [0,63 segundos de tiempo de ejecución total]


        • Matt Matt

          Sorry, Jorge. This only works for Gmail/Google Apps email addresses.

  2. Jorge Jorge

    Hi Matt,
    It’a all runnong rigth. Do tou know if there is any option to apply css style to text var?.
    I’m try to apply around link rel but gmail don’t apply it.

    Any idea where find it?.
    Thanks for all.

Leave a Reply

Your email address will not be published. Required fields are marked *