Automate daily Tweets with Postman and the Twitter API v2 🌞
Tags: postman, twitter, api, web3
Published: 2021-11-14 03:23:39


Has your Twitter feed been blowing up with "gm's" lately?
 
{% twitter 1459628182810857481 %}

If you're not in the know, this is [crypto speak](https://www.one37pm.com/nft/art/nft-twitter-meanings-definitions) for "good morning", and is a customary way web3 and crypto nerds clock in for the day.

Do you always forget to gm? Do you not care but want to give the illusion you're hip and GMI? Use [Postman](https://postman.com/) monitors with the [Twitter API v2](https://developer.twitter.com/en/docs/twitter-api) to automate posting your daily salute to Twitter. 

## 1. Get a Twitter Developer account

On top of a normal Twitter account, you will need to set up a free Twitter developer account [here](https://developer.twitter.com/en/apply-for-access).

Make a hobbyist account and explain that you will use your account to post your own tweets. Part of your on-boarding will be to "create a project". 

## 2. Create an App

Once you have your developer account and project set up in your [Twitter developer dashboard](https://developer.twitter.com/en/portal/dashboard), you need to add an App to your project in order to use the API. 

![Adding an app to a project in the Twitter developer dashabord](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iki61m5ht3kz4jcs5vlh.png)
 
Give your new app a name

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xvmnnu89f8jqfz8t2jxp.png)

## 3. Get your secrets

Next you will be given some secrets. Treat these credentials like passwords and store them somewhere secure. The keys are hidden in this image, but you'll see something like this:

![Copy the API Key, API Key Secret, and Bearer token for your new app](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kz1wam2ulgjmchn59aud.png)

## 4. Give your app read and write permissions
You need to enable your app to post tweets on your behalf. Go to your app's "Settings" tab and click the button to edit the App permissions.

![Edit app permissions in Twitter dashboard](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8oamqh4oqqlzfcrpi4gv.png)

Update the app permissions from "Read" to "Read and write". Click save to apply the changes.

![Update app permissions from "Read" to "Read and write"](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z1qop8vys9fgr1ktf162.png)
  
## 5. Generate additional credentials
You will also need to securely jot down an **Access Token** and **Access Token Secret**, which can be generated in your app's "Keys and tokens" tab. 

![Generate Access Token and Access Token Secret for your app in the Twitter dashboard](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ykadtzpeectem4a57ymt.png)

Copy the generated token and token secret with your other secrets. These are the final credentials we need to authorize requests to the Twitter API. 

![Copy the generated Access Token and Access Token Secret](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s5nqb35nt6h7hgn0mybr.png)

## 6. Log in to Postman 

[Postman](https://www.postman.com/) is a free tool for building, consuming, testing, documenting, and monitoring APIs. You can create a free account [here](https://postman.com). 

Once you have an account you can access the tool from the browser without downloading anything, simply by  going to https://postman.com
 
## 7. Create a personal workspace

To make API requests in Postman, first you need a workspace. You can create a workspace from the "Workspaces" dropdown. 

**IMPORTANT:** The workspace visibility should be set to **Personal**, since we will be storing our secret keys in a way that would make them visible if the workspace were public. 

![Create a new workspace in Postman](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c7puw8g7fsk1bwv1lmbt.png)
 

## 8. Create a collection

In Postman, API requests live inside **Collections**. 

Create a collection either with the "Create a collection" button, or the plus "+" icon in the collections tab. Name your collection `Twitter API v2`

## 9. Add a request to post Tweets

Now we can build a request to the Twitter API that will post a tweet on your behalf. First, "Add a request", and name it `post a tweet`

![Add a request in Postman](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n2v63cxkhqlyrlxeuzox.png)

### Using the Twitter API v2 

We will use this endpoint in the Twitter API v2 to tweet: 

`POST https://api.twitter.com/2/tweets`

You can read more about it [here in the docs](https://developer.twitter.com/en/docs/twitter-api/tweets/manage-tweets/api-reference/post-tweets).

Paste this endpoint into the request URL in Postman, and set the request method to `POST`.

![Update the request method and URL](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wmlyk0dmyqj5c1rzifv3.png)

### Add a body to your request

The Twitter API v2 expects a JSON body, where a `text` property is the text content of your tweet. 

In the "Body" tab of your request in Postman, select "raw" > "JSON" as the datatype and paste this JSON data in the body: 

```json
{
  "text": "gm"
}
```

![Build Tweet request in Postman](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e55yhuyf64ursg1ztrao.png)
 
### Authorize your request

You can't just go tweeting as anybody. In order to tweet you will need to authorize your request with your credentials. 

This endpoint accepts OAuth 1.0 authorization. We can use Postman's "Authorization" tab on the request to set this up. Select OAuth 1.0 as the Authorization type.  

![Select OAuth1 as the Authorization type for this request](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wmhrn9lvvjcip830d3bd.png)
 
First, make sure the request is set up to add the authorization data to the "Request Headers": 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i66vzsnkkf20w7ihxztc.png)

On the right, we will enter our Twitter app credentials: **API Key**, **API Key Secret**, **Access Token**, and **Access Token Secret** for the fields **Consumer Key**, **Consumer Secret**, **Access Token**, **Token Secret**, respectively. Leave everything else as the default. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ga244qhslw2mjscyrgkp.png)

> NOTE: Usually you do not want to hard code sensitive data like this directly in the Authorization tab. 

> Normally you should [use variables](https://learning.postman.com/docs/sending-requests/variables/) to obscure your secrets. Variables defined only in the "Initial Value" column are local to your Postman app and won't be shared. 

> However, in order to automate our tweets in a later step **we need the secrets to be accessible by Postman servers in your workspace**. Be sure not to make your workspace public or people could steal your API credentials and tweet abuse your Twitter. 

Save all your changes and send the request. In Postman you should get a status `201` response from the Twitter API with the `id` of your new Tweet. Go check out Twitter!

![Tweet from clairefroe with text "gm", sent from the Postman using the Twitter API v2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ndcmklt67ssyr839nlj.png)
 
## 10. Automate daily tweets in Postman

Postman allows you to create [monitors](https://learning.postman.com/docs/monitoring-your-api/intro-monitors/) that run your collections at specified intervals to alert you when your servers have caught on fire. 

This feature can double as a way to schedule cron jobs for running API calls, which is exactly what we'll do to run our `post a tweet` request every morning. 

### Set up a Postman monitor 

In the same personal workspace you made, click the "Monitors" tab on the far left menu, then "Create a Monitor". 

Give your monitor a name. Select the collection you made and configure it to run every morning. Set it to a bright and early time to make yourself appear industrious ☕.  

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yyekfi8fkagd2keappyj.png)

Scroll down and click the big orange "Create Monitor" button to set up the scheduled run. Give your monitor a test run by clicking the "Run" button manually - check your Twitter and make sure it works. 

Congratulations! Sit back and let the robots take over. 

## BONUS: Add random emojis for humanizing effect

If you want to further hide the sham that you have a bot tweeting your gms, add a personalized random emoji to each tweet using scripts in Postman.

### Add a pre-request script to get a random emoji

Head back to the Collections tab in your workspace and click on the `post a tweet` request. 

Open the "Pre-request Scripts" tab on the request. Any Node.js code we write in here will be executed right before our request is sent off. Paste in this code, but use any custom emojis that suit your personality:

```js
const emojis = ["🌞","💪","🍠","🍵"]

const emojiDeJour = emojis[Math.floor(Math.random() * emojis.length)]

pm.collectionVariables.set("emojiDeJour", emojiDeJour)
``` 

To explain, first we are defining an array of emojis. Then we select a random emoji and assign it to the local variable `emojiDeJour`. Finally we use Postman's [`pm` helper object](https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/#the-pm-object) to programmatically set our `emojiDeJour` as a collection variable of the same name. This will allow us to access the `emojiDeJour` in the body of our request. 

![Postman pre-request script with code for selecting a random emoji from a list](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rf9bih5ygecbp1kmccy2.png)

### Add the emoji to our tweet in the request body

Finally, interpolate the `emojiDeJour` from the collection variables using Postman's `{{variable}}` syntax with double curly braces. Update the request body to: 

```json
{
    "text": "gm {{emojiDeJour}}"
}
```

Save your changes! Now each time request is sent you get a random emoji at the end of your tweet. I got sweet potato. 

![Tweet with a sweet potato emoji sent using Postman and the Twitter API](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vcinaxek45dth81exgzu.png)

## Conclusion 

Postman monitors are a simple and free way to run scheduled API calls, making it easy to automate tweets using the Twitter API. Twitter has a fabulous API for interacting with the app, just take care your [use case is not prohibited](https://developer.twitter.com/en/developer-terms/more-on-restricted-use-cases) before making automations. Also note that the Twitter API is [rate limited](https://developer.twitter.com/en/docs/twitter-api/rate-limits) and you should make no more than 200 tweets via the API in a 15 minute window. But if you tweet that much you will have lost all your followers anyway.

You can see a completed example of the Postman workspace [here](https://www.postman.com/claireman/workspace/automated-twitter-gms/overview) (just make sure YOU make a personal workspace)

gn 🌚

*Disclaimer: I work for Postman, but the thoughts and promotion of gm culture in this article are my own.*