An asynchronous client library for the Twitter REST and Streaming V2 API's.
const Twitter = ; const client = consumer_key: '' consumer_secret: '' access_token_key: '' access_token_secret: ''; const data = await client;console;
npm install twitter-v2
You will need valid Twitter developer credentials in the form of a set of consumer keys. You can get early access V2 keys here.
User authentication requires your app's consumer keys and access tokens obtained from oauth 1.0a.
const client = consumer_key: '' consumer_secret: '' access_token_key: '' access_token_secret: '';
Alternatively, app authentication (which can only access public data but is often suitable for server applications) only needs your app's consumer keys and/or bearer token.
const client = consumer_key: '' consumer_secret: '';
or
const client = bearer_token: '';
You can make GET, POST, and DELETE requests against the REST API via the convenience methods.
client;client;client;
The REST API convenience methods return Promises.
Use the streaming convenience methods for any stream APIs.
client;
The Streaming API will return an async iterator with the convenience method close()
.
Ensure that you call close()
when done with a stream, otherwise it will
continue to download content from Twitter in the background.
const stream = client; // Close the stream after 30s.; for { console;}
Note that reconnect logic is not handled by this package, you're responsible for implementing it based on the needs of your application. The stream will close itself in two cases:
TwitterError
will be thrown.If you wish to continuously listen to a stream, you'll need to handle both of these cases. For example:
{ try for await const data of ; // The stream has been closed by Twitter. It is usually safe to reconnect. console; ; catch error // An error occurred so we reconnect to the stream. Note that we should // probably have retry logic here to prevent reconnection after a number of // closely timed failures (may indicate a problem that is not downstream). console; ; } ;
This module does not support previous versions of the Twitter API, however it works well with the following V1.1 modules