A 100% pure Java library for the Twitter API with no extra dependency
Complete tweet lifecycle management including posting, retrieving, deleting, and retweeting.
Post new tweets with text, media, and metadata.
interface TweetsResources {
/**
* Post a simple text tweet
* @param status Tweet text (max 280 characters)
* @return Posted tweet status
*/
Status updateStatus(String status) throws TwitterException;
/**
* Post a tweet with advanced options using StatusUpdate
* @param statusUpdate Tweet parameters including media, location, reply settings
* @return Posted tweet status
*/
Status updateStatus(StatusUpdate statusUpdate) throws TwitterException;
}Usage Examples:
TwitterV1 v1 = twitter.v1();
// Simple text tweet
Status tweet = v1.tweets().updateStatus("Hello Twitter API!");
// Tweet with advanced options
StatusUpdate update = StatusUpdate.of("Check out this image!")
.possiblySensitive(false)
.placeId("place_id_here")
.displayCoordinates(true);
Status tweetWithOptions = v1.tweets().updateStatus(update);Retrieve individual tweets and collections of tweets.
interface TweetsResources {
/**
* Get a single tweet by ID
* @param id Tweet ID
* @return Tweet status object
*/
Status showStatus(long id) throws TwitterException;
/**
* Get multiple tweets by their IDs
* @param ids Array of tweet IDs (max 100)
* @return List of tweet status objects
*/
ResponseList<Status> lookup(long... ids) throws TwitterException;
/**
* Get retweets of a specific tweet
* @param statusId Original tweet ID
* @return List of retweet status objects (max 100)
*/
ResponseList<Status> getRetweets(long statusId) throws TwitterException;
}Usage Examples:
// Get single tweet
Status tweet = v1.tweets().showStatus(1234567890L);
System.out.println(tweet.getText());
// Get multiple tweets
ResponseList<Status> tweets = v1.tweets().lookup(1234567890L, 9876543210L);
for (Status status : tweets) {
System.out.println(status.getUser().getScreenName() + ": " + status.getText());
}
// Get retweets of a tweet
ResponseList<Status> retweets = v1.tweets().getRetweets(1234567890L);
System.out.println("Retweet count: " + retweets.size());Delete tweets from authenticated user's timeline.
interface TweetsResources {
/**
* Delete a tweet
* @param statusId ID of tweet to delete
* @return Deleted tweet status
*/
Status destroyStatus(long statusId) throws TwitterException;
}Usage Example:
// Delete a tweet
Status deletedTweet = v1.tweets().destroyStatus(1234567890L);
System.out.println("Deleted: " + deletedTweet.getText());Retweet and unretweet functionality.
interface TweetsResources {
/**
* Retweet a tweet
* @param statusId ID of tweet to retweet
* @return Retweet status object
*/
Status retweetStatus(long statusId) throws TwitterException;
/**
* Remove a retweet (unretweet)
* @param statusId ID of original tweet to unretweet
* @return Original tweet status
*/
Status unRetweetStatus(long statusId) throws TwitterException;
/**
* Get user IDs who retweeted a specific tweet
* @param statusId Tweet ID
* @param cursor Cursor for pagination (-1 for first page)
* @return User IDs with pagination cursors
*/
IDs getRetweeterIds(long statusId, long cursor) throws TwitterException;
/**
* Get user IDs who retweeted a specific tweet with count limit
* @param statusId Tweet ID
* @param count Maximum number of IDs to return (max 5000)
* @param cursor Cursor for pagination (-1 for first page)
* @return User IDs with pagination cursors
*/
IDs getRetweeterIds(long statusId, int count, long cursor) throws TwitterException;
}Usage Examples:
// Retweet a tweet
Status retweet = v1.tweets().retweetStatus(1234567890L);
System.out.println("Retweeted: " + retweet.getRetweetedStatus().getText());
// Unretweet
Status originalTweet = v1.tweets().unRetweetStatus(1234567890L);
// Get retweeter IDs
IDs retweeterIds = v1.tweets().getRetweeterIds(1234567890L, -1);
for (long userId : retweeterIds.getIDs()) {
System.out.println("Retweeted by user ID: " + userId);
}Upload images, videos, and GIFs to include in tweets.
interface TweetsResources {
/**
* Upload media file for use in tweets
* @param mediaFile Media file to upload
* @return Uploaded media object with media ID
*/
UploadedMedia uploadMedia(File mediaFile) throws TwitterException;
/**
* Upload media from input stream
* @param fileName Name of the media file
* @param media Input stream of media data
* @return Uploaded media object with media ID
*/
UploadedMedia uploadMedia(String fileName, InputStream media) throws TwitterException;
/**
* Upload large media files using chunked upload (for videos > 15MB)
* @param fileName Name of the media file
* @param media Input stream of media data
* @return Uploaded media object with media ID
*/
UploadedMedia uploadMediaChunked(String fileName, InputStream media) throws TwitterException;
}Usage Examples:
// Upload image file
File imageFile = new File("photo.jpg");
UploadedMedia media = v1.tweets().uploadMedia(imageFile);
// Tweet with uploaded media
StatusUpdate update = StatusUpdate.of("Check out this photo!")
.mediaIds(media.getMediaId());
Status tweet = v1.tweets().updateStatus(update);
// Upload from input stream
try (FileInputStream fis = new FileInputStream("video.mp4")) {
UploadedMedia videoMedia = v1.tweets().uploadMediaChunked("video.mp4", fis);
StatusUpdate videoTweet = StatusUpdate.of("Here's a video!")
.mediaIds(videoMedia.getMediaId());
v1.tweets().updateStatus(videoTweet);
}Get oEmbed data for embedding tweets in web pages.
interface TweetsResources {
/**
* Get oEmbed data for a tweet
* @param req oEmbed request parameters
* @return oEmbed response data
*/
OEmbed getOEmbed(OEmbedRequest req) throws TwitterException;
}Usage Example:
// Get oEmbed data for a tweet
OEmbedRequest request = OEmbedRequest.of(1234567890L)
.maxWidth(550)
.hideMedia(false)
.hideThread(false)
.omitScript(false)
.align("center")
.related("twitterapi,twitter")
.lang("en");
OEmbed oEmbed = v1.tweets().getOEmbed(request);
System.out.println("HTML: " + oEmbed.getHtml());
System.out.println("Width: " + oEmbed.getWidth());
System.out.println("Height: " + oEmbed.getHeight());Comprehensive tweet composition with media, location, and reply settings.
class StatusUpdate {
/**
* Create basic status update with text
* @param status Tweet text
* @return StatusUpdate builder
*/
static StatusUpdate of(String status);
/**
* Set tweet as reply to another tweet
* @param inReplyToStatusId ID of tweet being replied to
* @return StatusUpdate with reply settings
*/
StatusUpdate inReplyToStatusId(long inReplyToStatusId);
/**
* Set geographic location for tweet
* @param location Latitude/longitude coordinates
* @return StatusUpdate with location
*/
StatusUpdate location(GeoLocation location);
/**
* Set place ID for tweet location
* @param placeId Twitter place ID
* @return StatusUpdate with place
*/
StatusUpdate placeId(String placeId);
/**
* Set whether to display exact coordinates
* @param displayCoordinates True to show coordinates publicly
* @return StatusUpdate with coordinate display setting
*/
StatusUpdate displayCoordinates(boolean displayCoordinates);
/**
* Mark tweet as possibly sensitive content
* @param possiblySensitive True if content may be sensitive
* @return StatusUpdate with sensitivity flag
*/
StatusUpdate possiblySensitive(boolean possiblySensitive);
/**
* Attach media to tweet using media IDs
* @param mediaIds Media IDs from uploadMedia() calls
* @return StatusUpdate with attached media
*/
StatusUpdate mediaIds(long... mediaIds);
/**
* Enable automatic reply metadata population
* @param autoPopulateReplyMetadata True to auto-populate reply data
* @return StatusUpdate with reply metadata setting
*/
StatusUpdate autoPopulateReplyMetadata(boolean autoPopulateReplyMetadata);
/**
* Exclude reply user IDs from automatic reply metadata
* @param excludeReplyUserIds User IDs to exclude from reply
* @return StatusUpdate with exclusion settings
*/
StatusUpdate excludeReplyUserIds(long... excludeReplyUserIds);
/**
* Add attachment URL (for quote tweets)
* @param attachmentUrl Quote tweet URL
* @return StatusUpdate with attachment
*/
StatusUpdate attachmentUrl(String attachmentUrl);
}Configure tweet embedding options.
class OEmbedRequest {
/**
* Create oEmbed request for tweet ID
* @param statusId Tweet ID to embed
* @return OEmbedRequest builder
*/
static OEmbedRequest of(long statusId);
/**
* Create oEmbed request for tweet URL
* @param url Tweet URL to embed
* @return OEmbedRequest builder
*/
static OEmbedRequest of(String url);
/**
* Set maximum width for embedded tweet
* @param maxWidth Maximum width in pixels
* @return OEmbedRequest with width setting
*/
OEmbedRequest maxWidth(int maxWidth);
/**
* Hide media in embedded tweet
* @param hideMedia True to hide media attachments
* @return OEmbedRequest with media visibility setting
*/
OEmbedRequest hideMedia(boolean hideMedia);
/**
* Hide conversation thread in embedded tweet
* @param hideThread True to hide thread context
* @return OEmbedRequest with thread visibility setting
*/
OEmbedRequest hideThread(boolean hideThread);
/**
* Omit JavaScript from oEmbed HTML
* @param omitScript True to exclude script tags
* @return OEmbedRequest with script setting
*/
OEmbedRequest omitScript(boolean omitScript);
/**
* Set alignment of embedded tweet
* @param align Alignment ("left", "right", "center", "none")
* @return OEmbedRequest with alignment
*/
OEmbedRequest align(String align);
/**
* Set related accounts for embedded tweet
* @param related Comma-separated list of related screen names
* @return OEmbedRequest with related accounts
*/
OEmbedRequest related(String related);
/**
* Set language for embedded tweet
* @param lang Language code (e.g., "en", "es", "fr")
* @return OEmbedRequest with language
*/
OEmbedRequest lang(String lang);
}Core tweet data model with full metadata.
interface Status extends TwitterResponse {
/**
* Tweet creation date and time
*/
LocalDateTime getCreatedAt();
/**
* Unique tweet ID
*/
long getId();
/**
* Tweet text content
*/
String getText();
/**
* Tweet source application
*/
String getSource();
/**
* Whether tweet text is truncated
*/
boolean isTruncated();
/**
* ID of tweet being replied to (if reply)
*/
long getInReplyToStatusId();
/**
* ID of user being replied to (if reply)
*/
long getInReplyToUserId();
/**
* Screen name of user being replied to (if reply)
*/
String getInReplyToScreenName();
/**
* Author of the tweet
*/
User getUser();
/**
* Geographic coordinates (if enabled)
*/
GeoLocation getGeoLocation();
/**
* Place information (if tweet is geo-tagged)
*/
Place getPlace();
/**
* Whether tweet contains potentially sensitive content
*/
boolean isPossiblySensitive();
/**
* Number of retweets
*/
int getRetweetCount();
/**
* Number of favorites/likes
*/
int getFavoriteCount();
/**
* Whether authenticated user has favorited this tweet
*/
boolean isFavorited();
/**
* Whether authenticated user has retweeted this tweet
*/
boolean isRetweeted();
/**
* Whether tweet contains a quoted tweet
*/
boolean isQuoteStatus();
/**
* Quoted tweet status (if quote tweet)
*/
Status getQuotedStatus();
/**
* Original tweet if this is a retweet
*/
Status getRetweetedStatus();
/**
* URL entities found in tweet text
*/
URLEntity[] getURLEntities();
/**
* User mention entities in tweet text
*/
UserMentionEntity[] getUserMentionEntities();
/**
* Hashtag entities in tweet text
*/
HashtagEntity[] getHashtagEntities();
/**
* Media entities (photos, videos) in tweet
*/
MediaEntity[] getMediaEntities();
/**
* Symbol entities (cashtags) in tweet text
*/
SymbolEntity[] getSymbolEntities();
}Media upload response with metadata.
interface UploadedMedia {
/**
* Media ID for use in tweets
*/
long getMediaId();
/**
* Media size in bytes
*/
long getSize();
/**
* Expiration time for media (Unix timestamp)
*/
long getExpiresAfterSecs();
/**
* Media processing state for videos
*/
ProcessingInfo getProcessingInfo();
}Tweet embedding data for web pages.
interface OEmbed {
/**
* HTML code for embedding tweet
*/
String getHtml();
/**
* Width of embedded tweet
*/
int getWidth();
/**
* Height of embedded tweet
*/
int getHeight();
/**
* oEmbed version
*/
String getVersion();
/**
* oEmbed type (always "rich")
*/
String getType();
/**
* Cache age in seconds
*/
long getCacheAge();
/**
* Tweet URL
*/
String getUrl();
/**
* Author name
*/
String getAuthorName();
/**
* Author URL
*/
String getAuthorUrl();
/**
* Provider name (Twitter)
*/
String getProviderName();
/**
* Provider URL
*/
String getProviderUrl();
}Install with Tessl CLI
npx tessl i tessl/maven-org-twitter4j--twitter4j-core