X/Twitter

Options for posting via the API to X, formerly known as Twitter

X/Twitter API Endpoints

Overview

JSON for a basic post with a link and image to X/Twitter:

{
    "post": "The best Tweet ever #best https://www.twitter.com",
    "mediaUrls": ["https://img.ayrshare.com/012/gb.jpg"],
    "platforms": ["twitter"]
}
  • X/Twitter will automatically preview the link in the Tweet unless there is an image or video included. In the above example the image will show. Removing the image will cause the link preview to show.

  • If your video doesn't end in a known video extension such as mp4, please use the isVideo parameter. See the /post endpoint for details.

  • X/Twitter also supports sending media without post text. If you do not want post text included send an empty String post: ""

  • Please see the important guidelines and restrictions posting Twitter videos.

Upload Long Videos

Business or Enterprise Plan required.

X/Twitter requires videos have a maximum video length of 2 minutes and 20 seconds. However, if you have a have been approved by X/Twitter to upload longer videos, such as in the Amplify Partner Program, you can post videos up to 10 minutes in length.

In addition to X/Twitter approving your account for long videos, your Aryshare account must be enabled for long Twitter videos. Please contact us to enable this feature.

After your Ayrshare account is enabled for long videos and X/Twitter has approved your access, use the longVideo twitterOptions parameter when posting:

{
    "twitterOptions": {
        "longVideo": true
    }
}

X/Twitter Mentions

Mention another X/Twitter handle by adding @handle in the post text. For example:

{
    "post": "The best social media API @Ayrshare ever!",
    "platforms": ["twitter"]
}

Please review the important rules on mentions.

Post X/Twitter Thread (TweetStorm)

A X/Twitter Thread or TweetStorm can be posted via the API. A thread is a post broken up into a set of reply threads and link in X/Twitter with a line.

  • thread: true to automatically break apart the post text into threads based on line breaks.

  • threadNumber: true to automatically add numbers at the end of threads in the format of 1/n. For example the 2nd of 5 threads will have appended: 2/5

  • mediaUrls: [array of urls] to add each media object, an image or video, to a thread in order. Only one media object will be added to a thread in order.

{
    "twitterOptions": {
        "thread": true,        // required for TweetStorm
        "threadNumber": true,  // optional to add numbers to each thread 
        "mediaUrls": ["https://site.com/image1.png", "https://site.com/image2.png", ...]  // optional one media object is added to a thread in order
    }
}

If the post is sent as a X/Twitter Thread, the returned post analytics will be an array of Tweets, "twitter": []. See Post Analytics 200 Response for more information.

Skip Images and Multiple Media URLs

Skip an image for the thread by using null in the array. For example:

["https://site.com/image1.png", null, "https://site.com/image2.png"]

This will place image1 on the first Tweet, no image on the second Tweet, and image2 on the third Tweet.

Multiple images can be added to a Tweet in a Thread by adding an object {} with the media URLs in the mediaUrls array. Any unique object keys can be used. For example:

{
    "twitterOptions": {
        "thread": true,
        "threadNumber": true, 
        "mediaUrls": [
            "https://img.ayrshare.com/random/photo-1.jpg", 
            {"1": "https://img.ayrshare.com/random/photo-2.jpg", "2": "https://img.ayrshare.com/random/photo-3.jpg"},
            "https://img.ayrshare.com/random/photo-4.jpg"
        ]  
    }
}

In this example, the first Tweet will contain photo-1.jpg, the second Tweet photo-2.jpg and photo-3.jpg, and the third Tweet photo-4.jpg.

Thread Output

Ayrshare automatically breaks up the post text into appropriate length tweet (> 280 characters). Add paragraphs with \n\n to the post text to indicate a unique thread should be created. For example:

{
    "post": "This is tweet 1\n\nThis is tweet 2.",
    "platforms": [
        "twitter"
    ],
    "twitterOptions": {
        "thread": true
    }
}

will result in two Tweets in the thread.

If you want to add paragraphs, but not break into Tweets, use \u2063\n\u2063\n

{
    "post": "This is paragraph 1\u2063\n\u2063\nThis is paragraph 2.",
    "platforms": [
        "twitter"
    ],
    "twitterOptions": {
        "thread": true
    }
}

will result in one Tweet with two paragraphs since the post is below 280 characters.

Delete X/Twitter Thread

To delete a Tweet Storm, call the /post delete endpoint with the top level post ID returned in the response. All threads will be deleted.

Alternative Text for Images

Add alternative text, also known as alt text, to a Tweet's image. X/Twitter alt text is an accessibility feature used for additional user info and screen readers.

Use the altText in the twitterOptions object.

"twitterOptions": {
    "altText": ["This is my best pic", "😃 here is the next one"] // Array of Alt Texts
}

Each alt text must correspond to an image in the mediaUrls array. The alt text will be applies to each image in order.

Notes:

  • Alt text can not be applied to videos. If a video is included in the mediaUrls with altText, the video will not be posted.

  • Alt text must be 1,000 characters or less.

Quote Tweet

You can quote another Tweet by specifying the low-level Tweet ID. The ID can be retrieved via the /post response in the postIds field, get history, or directly from the Tweet URL: https://twitter.com/Ayrshare/status/1651601430669664256

"twitterOptions": {
    "quoteTweetId": "651601430669664256" // low-level Tweet Id
}

X/Twitter Polls

Conduct a Twitter Poll with the twitterOptions poll parameter.

"twitterOptions": {
    "poll": {
        "duration": 5, // required. Number in minutes
        "options": ["yes", "maybe", "no"] // required
    }
}
  • duration: A number of minutes specifying the duration the poll will be conducted.

  • options: An array of strings of the poll options.

Subtitles / Captions for Videos

You can add subtitles, also known as captions, to videos by including an SRT file. Use the subTitle field in the twitterOptions object to specify the URL to your SRT file.

{
    "twitterOptions": {
        "subTitle": "https://www.mysite.com/subtitles.srt
    }
}

The URL must start with https:// and end in .srt and be a valid SRT file.

X/Twitter Video Compatibility

Some video software creates MP4 files that are not compatible with X/Twitter. For example, Camtasia versions older than 2019.0.9 create MP4 files that X/Twitter rejects. Also more than one audio track often causes problems.

If you receive back the following message while posting, it indicates the video is not compatible with Twitter and needs to be re-encoded.

"file is currently unsupported"

Please check your video software for compatibility. For example, Adobe Media Encoder has an export preset for Twitter 1080p Full HD.

Please see here for more X/Twitter API examples.

Last updated