Top 7 Tips and Tricks For Twitter API Posting

These 7 tips and tricks will help you get the most out of the Twitter API.

There are a ton of tools out there for posting to Twitter using a web app. But, if you are planning to use an API for your connectivity to the social network, then you have to do things a bit differently.

twitter api tips

What is the Twitter API?

The Twitter API allows you to programmatically access Twitter for Tweets, Direct Messages, Spaces, Twitter Threads, and more. There is a separate Twitter Ads API which we will not be discussing in this writeup.

Some of the most popular use cases for the Twitter API include creating Tweets, deleting Tweets, pulling a historical list of Tweets, and getting analytics data for Tweets or users.

To get started with Twitter’s API, you can either apply for a developer account to get your own API keys, and start digging into the docs for the Twitter API v2, or you can use a social media API provider like Ayrshare.

Tip 1: Choosing Between Twitter API v1.1 vs v2

If you are just starting out with a new developer account, you probably should start building with the Twitter API version 2. But some features, including some Twitter data from v1.1, are still missing in v2. For example, Media Upload, Direct Messages, Trends, and Friendships are not yet available in v2. If you need any endpoints not yet available in v2 start with v1.1. On the flip side, you’ll get some cool new features with v2, such as polls.

You’ll need to apply for special access to use v1.1 in the Twitter Developer Portal under “User authentication settings”. More information on Twitter’s v2 roadmap.

twitter roadmap
Twitter API Roadmap, Mid-March 2022

Alternatively, the Ayrshare API abstracts all that away and you get the best of the Twitter API v.1.1 and v2 in a simpler to use API that also gives you access to the APIs of Facebook, Instagram, YouTube, TikTok, Linkedin and more.

Tip 2: Uploading Images and Videos via the API

Only Twitter API v1.1 currently allows you to upload and attach media, such as images or videos, to a Tweet.

Unlike some other social media APIs, Twitter does not just accept a URL of the media, which makes things a bit more difficult and error prone.

Upload Twitter Images

Images must be POSTed as either a Base 64 encoded file or a raw binary file. We’ve found the Base64 encoding more reliable, but also caused the media to be larger and which could cause the file to be larger than Twitter’s allowed size limits.

The response will contain a media id. Save this id since you need it to post the Tweet.

Upload Twitter Videos

Videos, or large images, should be POSTed as chunks of bytes. There are three steps when posting the video:

  1. Initialize the upload using the INIT command
  2. Upload each chunk of bytes using the APPEND command
  3. Complete the upload using the FINALIZE command

One trick is to make the max chunk size 1MB. Anything larger often causes failures at Twitter. Similar to uploading an image, the response will contain a media id to be used when posting the Tweet.

Here is an example of the INIT data that can be posted in Node.js using the request package.

const formData = {
       command: "INIT",
       media_type: “video/mp4”,
       total_bytes: 384922834,
       media_category: "tweet_video",
     };
     const options = {
       url: "https://upload.twitter.com/1.1/media/upload.json",
       oauth: oauthCredentials,
       headers: {
         "content-type": "multipart/form-data",
       },
       formData: formData,
       redirect: 'follow'
     };
request.post(options, (err, response, body) => {...});

Note the “follow” redirects and the content-type. Both are important to successfully upload the media to Twitter.

Tip 3: Automation Bots

In February of 2022, Twitter released a new feature called the Automated label. This let’s users self-declare that the content on this Twitter handle is posted via some kind of automation and is not written by a human.

There are many good use cases for automated posting to Twitter via an API. For example, if your app automatically posts weather forecasts every hour, a museum that posts an image of a painting every day, a job site that publishes every new job opening, or a data provider that posts stock prices when they hit a new 52 week high.

Examples of automated labels on Twitter accounts

Even with this label, remember that you still have to follow all Twitter rules including their automation rules. Twitter takes platform manipulation very seriously and they have algorithms in place to monitor all user activities. In the second half of 2020, Twitter had 4.7 million spam reports from users, and challenged 143 million accounts that they identified as spam.

According to Twitter’s self-reported data, they get over 600,000 spam reports every month.

Spam Reports on Twitter

Tip 4: Image and video sizing and file formats

Make sure that you know the current specs for images and videos on Twitter. The maximum file size for an image is 5MB and for a video is 512 MB, with a runtime of 2 minutes and 20 seconds. Twitter does grant special permissions for larger and longer (10 minutes) videos if you are an Amplify partner.

If you post one image, the recommended size is 1200 x 675 but if you are sharing two or more, the recommended size changes.

The maximum resolution for videos is 1280×1024 but depending on whether your video is landscape, portrait, or square shape, then the recommended size differs.

These specs matter to ensure that Twitter accepts the Tweets that you send with media and that your media looks great on all Twitter clients.

Tip 5: Twitter thread Tweetstorm

If you are active user on Twitter, then you surely see multi-Tweet threads posted by users. These are common because Twitter limits an individual Tweet to 280 characters, and if you have something longer to post, then breaking it into multiple Tweets creates a thread.

These threads are often called “Tweetstorms” and can sometimes be quite long. To help the reader navigate such a thread, the user often uses notation such as 2/10 to signify that there are 10 total Tweets in the thread and you are reading the second one.

Examples of Twitter threads aka Tweetstorms

Using the Twitter mobile app, users can easily create such a thread by composing all the Tweets at once using the “+” button in the compose screen.

Composing a Twitter thread using the Twitter iOS mobile app

However, the Twitter API does not allow for directly posting such a Twitter thread. It requires a more complex implementation of creating a Tweet and then replying to each in a sequence while tracking the number of characters per Tweet and the total Tweets for the counter.

An easier solution is to use the Ayrshare Twitter thread poster which automatically creates a Tweetstorm for you. You can send in a long set of text and a list of media assets to the API endpoint, and the system will automatically break it into the correct number of Tweets, add numbers like 2/10 and add the media assets into the right Tweets. 

Tip 6: Follow rules for mentions frequency of people with no association 

You need to be very careful when mentioning another user in a Tweet send via the API. Here is a quote from Twitter:

“Automating these actions to reach many users on an unsolicited basis is an abuse of the feature, and is not permitted. For example, sending automated replies to Tweets based on keyword searches alone is not permitted. Spammy or duplicative use of mentions and replies may result in enforcement action, such as the removal of your Tweets from Search or the suspension of your app or account.

Source: Twitter

In our experience, Twitter is very serious about this and will often suspend your account with no warning if you run afoul of this rule.

The right way to use mentions in your Tweets is to

  1. Limit yourself to one mention per Tweet.
  2. Only mention users who have interacted with your user account previously. For example if they mentioned your Twitter handle in their own Tweet.
  3. Limit mentions. Even if another user has mentioned you, keep the volume and frequency of mentions to a minimum.
  4. Ask for permission if you want to mention someone who has no history of mentioning you.
  5. If a user asks to opt out of your service, then respect that and stop mentioning them. Make it easy for them to opt out so they do not complain to Twitter directly.

This logic is not easy to implement, which is why Ayrshare provides additional services on top of the Twitter API to help our clients respect these rules from Twitter, and protect their accounts from suspension.

Tip 7: Do not post duplicate content in your account or across multiple accounts

Every post should be as unique as possible. Twitter users do not want to see duplicate content since it looks like spam. 

Duplicate posts get poor visibility and engagement, and Twitter suspends accounts with too many duplicate or similar posts. Posting duplicate content across multiple Twitter accounts will lead to the same outcome, so don’t do that either.

Conclusion

Twitter’s API is a powerful tool for developers to engage with the social media network on behalf of their users. Over the past few years, Twitter has been more prolific in rolling out new features and giving access to more data to build on. Every Twitter developer should ensure that they follow these tips to get the most value out of the Twitter API.