Hey Siri, text my latest Tweet
Tags: twitter, api, ios, twitterapi
Published: 2019-11-21 17:06:45

---
title: Hey Siri, text my latest Tweet
description: Learn how to use Twitter Developer Labs and iOS Shortcuts to get your latest Tweet, translate it, and send it via iMessage.
tags: twitter, api, ios, twitter-api
published: true
---

As someone whose first language isn’t English, I tend to find myself Tweeting and dreaming in English; many of my friends do not. And I'm too lazy to translate my Tweets.

I also happen to like iOS 13 a lot. Dark Mode looks fantastic, and I'm enjoying Memojis a little too much. But my favorite app has to be Shortcuts. It ships by default with iOS 13, and it's more powerful than ever. With Shortcuts, you can instruct Siri to do things for you, including tasks no other app can do.

Shortcuts is a true hidden gem; its power and ease of use allows you to build automations without writing a single line of code. The app is extremely powerful; among other things, it has a HTTP client and a JSON parser. This is all you need to connect to any API.
So I thought, what if I can translate my latest Tweet, and send it only to my friends? Shortcuts can translate for me, and I can use Twitter Developer Labs to get my Tweets. Labs is our program where we’re building our next API in the open by inviting developers to help shape the future of the Twitter API. You can test drive new features and even full endpoints before they are broadly released, and you can tell us what you like and we can improve through our feedback channel (we already shipped some features based on feedback from people like you). It's free and you can try it out now.
Here's what we're going to do:

1. Get access to Labs
2. Tell Shortcuts to generate a bearer token
3. Get our latest Tweet using Labs
4. Ask Shortcuts to translate the Tweet
5. Send an iMessage with the Tweet


# 🧪 Enter Labs

_If you already activated ***Tweets and users*** you can jump to the [next section](#coding-with-no-code)._

For this tutorial, you need access to Labs and the API. First though, you will need a valid app – if you don't have one, you can apply on our [Developer site](https://developer.twitter.com). 
 
Once you have an app, [join Labs](https://developer.twitter.com/en/account/labs) and follow the instructions (it only takes a minute). When done, you should see a list of endpoints:

![A list of endpoints. Under "What's available" there's an API called "Tweets and Users". Next to it, a button says "Activate"](https://thepracticaldev.s3.amazonaws.com/i/9y2rilvjc4tfdimnwixt.png)

In this tutorial, you're going to deal with **Tweets and users**, so select **Activate** for that row, and attach the app you want to enable (you can enable one app per endpoint, but multiple endpoints can be attached to the same app).
Now, go to your [app list](https://apps.twitter.com/), locate the app you've just enabled for Tweets and users, and take note of the consumer key and secret.

<a name="coding-with-no-code"></a>
# 🤖 Coding with no code

Now we'll start coding… With no code. Shortcuts allows you to assemble tasks together, and if you know how to use an API, it's even more flexible.

Open the Shortcuts app (if you don't have it, you can download it for free from the App Store). Tap **Create Shortcut**.

On the first screen, tap **Add Action**. You'll see a lot of options, but you simply need a place to store your configuration (your credentials and your Twitter username). To do so, you're going to use a dictionary:

![This animation shows how to tap Add action, and type Dictionary to select a Dictionary.](https://thepracticaldev.s3.amazonaws.com/i/mh7fbloyq601jr86f5z2.gif)


Configure the dictionary with three keys:

- `consumer key` with your consumer key
- `consumer secret` with your consumer secret
- `username` with your Twitter username (without `@`)

In my case, it looks like this:

![My dictionary filled with three keys and values](https://thepracticaldev.s3.amazonaws.com/i/kau4z9v2b6p7z5y4prjs.jpeg)

Now let's request a Bearer token. To do so, we need to call the Twitter API by combining your consumer key and secret with a colon, for example, `consumer key:consumer secret`, then encode in base64, and send the resulting string in the `Authorization` header. In Shortcuts, we simply have to add a text component to combine the credentials, then add a Base64 Encode step:

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/j4mp88u126x47mvnuk5o.jpeg)

Next, create your actual request. Like I said, Shortcuts contains a full HTTP client, so for this step you just have to fill in the blanks:

1. Select **Get contents of URL**.
1. Use `https://api.twitter.com/oauth2/token` as the URL.
1. Tap **Show more**.
1. In **Method**, select POST.
1. In **Headers**, add Basic, then select **⚙️ Base64 Encoded**
1. In **Request Body**, select **Form**.
1. Add a field called `grant_type`. Its value should be `client_credentials`.

![This steps should have the Authorization header set to Basic, and the Request Body set to Form.](https://thepracticaldev.s3.amazonaws.com/i/btnchviuakj06y55ddem.jpeg)

Shortcuts has built-in JSON support. Our response will be JSON, so all we have to do is to tell Shortcut to retrieve the `access_token` field. Just add a **Get Dictionary Value** step after your request:

![Select the Dictionary Value step and drop it as the current step.](https://thepracticaldev.s3.amazonaws.com/i/cp0kyogktrnkp0d1y8hj.jpeg)

# Get your latest Tweet

You’re now ready to get your latest Tweet, and Labs is going to help us a lot. In [Tweets and users](https://developer.twitter.com/en/docs/labs/tweets-and-users?utm_source=devto), there are two API endpoints (one to get Tweets, one to get users). In this case, you're going to use the users endpoint, because it always returns the ID of the most recent Tweet from the users you're requesting. But wait, you need the full Tweet, not just its ID! How can you avoid making another request just to get the full Tweet?

Thankfully, one of my favorite Labs features is going to help you a lot here. It's called [expansions](https://developer.twitter.com/en/docs/labs/overview/whats-new/expansions?utm_source=devto), and it allows you to get a full object instead of its ID. It works like this:

- If I request `/labs/1/users?usernames=i_am_daniele` I get my user and most recent Tweet ID back
- If I request `/labs/1/users?usernames=i_am_daniele&expansions=most_recent_tweet_id` I get my user and my most recent Tweet back (including its text!)

So now you’re going to create two steps. The first is a simple **Get value** for `username` from the dictionary you created at the very beginning. The second step is another **Get contents of URL step** configured like this:

- Get contents of `https://api.twitter.com/labs/1/users?usernames=` Dictionary value `&expansions=most_recent_tweet_id` (yes, you can combine strings and variables together)
- Tap **Show more**
- Add a Header named **Authorization**. Its value should be `Bearer ` (leave a space after Bearer)
- Tap the magic wand icon on your keyboard, scroll back up a little, then tap on the "Dictionary value" label from "Get value for access_token in Contents of URL".

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/k8ta81i07zznt2hvejfr.gif)

Let's add these next steps to get the actual Tweet text:

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/8ukwvpw3cec0ym4dkzjy.jpeg)

# 🇺🇸🇪🇸 Translate

Now that you have your Tweet, you can ask Shortcuts to translate it. The app is integrated with Microsoft Translator, so all you have to do is to add the **Translate Text with Microsoft** block to your workflow.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/a7tzc2wnezlglgph5ibu.gif)

# 💬 ¡Enviamos!

Now it's time to send your text. First, combine the translated Tweet with a URL via a Text snippet, and then add a Send Message block. You can disable Show More -> Show when Run so your action runs seamlessly.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/1yyxzyahnyvtch3n2s4a.jpeg)

If you have Hey Siri enabled, try saying "Hey Siri, text my latest Tweet" to always be in touch with your friends, no matter what the language of your Tweets!