Open In App

Tweet using Node.js and Twitter API

Twitter is a social media website, with 450 million monthly active users (as of 2022). These users interact with each other using tweets . A tweet can contain Text, Video, GIF, Image. User post this tweet and other user can Like, Reply, Retweet, Bookmark, Mention other user in reply etc. 

The Twitter API is a toolkit that lets developers work with Twitter’s data and features. It allows them to build programs that can read and write Twitter data, and perform other functions too. The API has lots of “endpoints” that developers can use to get different kinds of Twitter data. For example, they can get tweets, user information, trending topics, and more. These endpoints use the REST architecture, which makes it easy for developers to communicate with Twitter’s servers. Overall, the Twitter API is a powerful tool that gives developers access to a wide range of Twitter data and functionality.



Problem Statement: To create and tweet a post (without image/media and with image/media) using Node.js and twitter-api-v2 library.

 



Approach: To create and post a tweet you can follow the steps below:

mkdir tweetPoster
cd tweetPoster
npm init
npm install twitter-api-v2
const { TwitterApi } = require("twitter-api-v2");
const client = new TwitterApi({
      appKey: "**",
      appSecret: "**",
      accessToken: "**",
      accessSecret: "**",
      bearerToken:"**",
});
const rwClient = client.readWrite;
const textTweet = async () => {
      try {
        await rwClient.v2.tweet(
            "This tweet has been created using nodejs");
        console.log("success");
      } catch (error) {
        console.error(error);
      }
};
{
      text: "any text you wish to post with image/media ",
      media: { media_ids: [mediaId] },
}
const mediaTweet = async () => {
      try {
        const mediaId = await client.v1.uploadMedia("image_path");
        await rwClient.v2.tweet({
              text: "Twitter is a fantastic social network. Look at this:",
              media: { media_ids: [mediaId] },
        });
        console.log("success");
      } catch (e) {
        console.error(e);
      }
};
textTweet();
mediaTweet();
node index.js




// Import twitter-api-v2
const { TwitterApi } = require("twitter-api-v2");
  
// Fill your API credentials
const client = new TwitterApi({
    appKey: "**",
    appSecret: "**",
    accessToken: "**",
    accessSecret: "**",
    bearerToken: "**",
});
  
// Provide read write controls
const rwClient = client.readWrite;
  
// Create textTweet function which post
// a text only tweet
const tweetText = async () => {
    try {
  
        // Use .tweet() method and pass the
        // text you want to post
        await rwClient.v2.tweet(
            "This tweet has been created using nodejs");
  
        console.log("success");
    } catch (error) {
        console.log(error);
    }
};
  
// Create tweet function which post
// tweet with media and text
const mediaTweet = async () => {
    try {
  
        // Create mediaID 
        const mediaId = await client.v1.uploadMedia(
  
            // Put path of image you wish to post
            "./1605232393098780672example.png"
        );
  
        // Use tweet() method and pass object with text 
        // in text feild and media items in media feild
        await rwClient.v2.tweet({
            text: 
"Twitter is a fantastic social network. Look at this:",
            media: { media_ids: [mediaId] },
        });
        console.log("success");
    } catch (error) {
        console.log(error);
    }
};
  
// Call any of methods and you are done 
textTweet();
mediaTweet();

Output:

output of the above program

Tweet Samples:

sample of only text tweet


Article Tags :