Skip to content

A tiny, full-featured, flexible client / server library for the Twitter API

License

Notifications You must be signed in to change notification settings

ongspxm/twitter-lite

 
 

Repository files navigation

Twitter Lite

A tiny Nodejs library for the Twitter API

npm travis

Features

  • Promise driven via Async / Await
  • Up-to-date APIs
  • Stream support
  • Under 1kb

Installation

yarn add twitter-lite
npm install twitter-lite

Usage

  • Create an app on https://apps.twitter.com/
  • Grab the Consumer Key (API Key) and Consumer Secret (API Secret) from Keys and Access Tokens
  • Make sure you set the right access level for your app

App vs. User

Twitter has two different authentication options:

  • App: higher rate limits. Great for building your own Twitter App
  • User: lower rate limits. Great for making requests on behalf of a User.

User authentication requires:

  • consumer_key
  • consumer_secret
  • access_token_key
  • access_token_secret

App authentication requires:

  • bearer_token

App authentication is a simple header behind the scenes:

headers: {
  Authorization: `Bearer ${bearer_token}`;
}

Oauth Authentication

According to the docs this helps you get access token from your users.

const client = new Twitter({
  consumer_key: "xyz",
  consumer_secret: "xyz"
});

client.getRequestToken("http://callbackurl.com")
.then(res => console.log({
    reqTkn: res.oauth_token, 
    reqTknSecret: res.oauth_token_secret
}))
.catch(console.error);

Then you redirect your user to https://api.twitter.com/oauth/authenticate?oauth_token=xyz123abc, and once you get the verifier and the token, you pass them on to the next stage of the authentication.

const client = new Twitter({
  consumer_key: "xyz",
  consumer_secret: "xyz"
});

client.getAccessToken({
    key: requestToken,
    secret: requestTokenSecret,
    verifier: oauthVerifier
})
.then(res=>console.log({
    accTkn: res.oauth_token, 
    accTknSecret: res.oauth_token_secret,
    userId: res.user_id,
    screenName: res.screen_name
}))
.catch(console.error);

And this will return you your access_token and access_token_secret.

Verifying Credentials Example (User auth)

const client = new Twitter({
  subdomain: "api",
  consumer_key: "xyz", // from Twitter.
  consumer_secret: "xyz", // from Twitter.
  access_token_key: "abc", // from your User (oauth_token)
  access_token_secret: "abc" // from your User (oauth_token_secret)
});

client
  .get("account/verify_credentials")
  .then(results => {
    console.log("results", results);
  })
  .catch(console.error);

App authentication Example

const client = new Twitter({
  subdomain: "api",
  bearer_token: "Bearer ABC123XYZ" // generate a Bearer token
});

client
  .get("users/lookup")
  .then(results => {
    console.log("results", results);
  })
  .catch(console.error);

Streams

To learn more about the streaming API visit the Twitter Docs.

const client = new Twitter({
  consumer_key: "xyz" // from Twitter.
  consumer_secret: "xyz" // from Twitter.
  access_token_key: "abc" // from your User (oauth_token)
  access_token_secret: "abc" // from your User (oauth_token_secret)
});

const parameters = {
  track: "#bitcoin,#litecoin,#monero", // #bitcoin, #litecoin, #monero
  follow: "422297024,873788249839370240", // @OrchardAI, @tylerbuchea
  locations: "-122.75,36.8,-121.75,37.8", // Bounding box -	San Francisco
};

client.stream("statuses/filter", parameters)
  .on("start", response => console.log("start"))
  .on("data", data => console.log("data", data.text))
  .on("ping", () => console.log("ping"))
  .on("error", error => console.log("error", error))
  .on("end", response => console.log("end"));

// to stop the stream:
client.stream.destroy(); // emits "end" and "error" event

Troubleshooting

API Errors

Api errors are returned (with "catch" in the Promise api or with "err" param in the callback api) as an array of errors. Thus errors described in twitter docs for example as:

 { "errors": [ { "code": 88, "message": "Rate limit exceeded" } ] }

Would return as :

 [ { "code": 88, "message": "Rate limit exceeded" } ]

Credit

Over the years, thanks to:

About

A tiny, full-featured, flexible client / server library for the Twitter API

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%