UNPKG

7.24 kBMarkdownView Raw
1# Twitter API v2
2
3[![Twitter API v2 badge](https://img.shields.io/endpoint?url=https%3A%2F%2Ftwbadges.glitch.me%2Fbadges%2Fv2)](https://developer.twitter.com/en/docs/twitter-api/early-access)
4[![Twitter API v1.1 badge](https://img.shields.io/endpoint?url=https%3A%2F%2Ftwbadges.glitch.me%2Fbadges%2Fstandard)](https://developer.twitter.com/en/docs/twitter-api/v1)
5[![Version badge](https://badgen.net/github/release/PLhery/node-twitter-api-v2)](https://github.com/PLhery/node-twitter-api-v2)
6[![Checks badge](https://github.com/PLhery/node-twitter-api-v2/actions/workflows/CI.yml/badge.svg)](https://github.com/PLhery/node-twitter-api-v2/actions/workflows/CI.yml)
7[![Package size badge](https://badgen.net/bundlephobia/minzip/twitter-api-v2)](https://bundlephobia.com/package/twitter-api-v2)
8
9Strongly typed, full-featured, light, versatile yet powerful Twitter API v1.1 and v2 client for Node.js.
10
11Main maintainer: [@alkihis](https://github.com/alkihis) - <a href="https://www.buymeacoffee.com/alkihis" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" height="20px" marginTop="10px"></a>
12
13
14## Highlights
15
16**Ready for v2 and good ol' v1.1 Twitter API**
17
18**Light: No dependencies, 23kb minified+gzipped**
19
20**Bundled types for request parameters and responses**
21
22**Streaming support**
23
24**Pagination utils**
25
26**User-context authentication with OAuth2**
27
28**Media upload helpers**
29
30## How to use
31
32Install it through your favorite package manager:
33```bash
34yarn add twitter-api-v2
35# or
36npm i twitter-api-v2
37```
38
39Here's a quick example of usage:
40
41```ts
42import { TwitterApi } from 'twitter-api-v2';
43
44// Instantiate with desired auth type (here's Bearer v2 auth)
45const twitterClient = new TwitterApi('<YOUR_APP_USER_TOKEN>');
46
47// Tell typescript it's a readonly app
48const readOnlyClient = twitterClient.readOnly;
49
50// Play with the built in methods
51const user = await readOnlyClient.v2.userByUsername('plhery');
52await twitterClient.v1.tweet('Hello, this is a test.');
53// You can upload media easily!
54await twitterClient.v1.uploadMedia('./big-buck-bunny.mp4');
55```
56
57## Why?
58
59Sometimes, you just want to quickly bootstrap an application using the Twitter API.
60Even though there are a lot of libraries available on the JavaScript ecosystem, they usually just
61provide wrappers around HTTP methods, and some of them are bloated with many dependencies.
62
63`twitter-api-v2` is meant to provide full endpoint wrapping, from method name to response data,
64using descriptive typings for read/write/DMs rights, request parameters and response payload.
65
66A small feature comparaison with other libs:
67
68| Package | API version(s) | Response typings | Media helpers | Pagination | Subdeps | Size (gzip) | Install size |
69| -------------- | -------------- | ---------------- | ------------- | ---------- | --------------- | -------------:| -------------:|
70| twitter-api-v2 | v1.1, v2, labs | ✅ | ✅ | ✅ | 0 | ~23 kB | [![twitter-api-v2 install size badge](https://badgen.net/packagephobia/install/twitter-api-v2)](https://packagephobia.com/result?p=twitter-api-v2) |
71| twit | v1.1 | ❌ | ✅ | ❌ | 51 | ~214.5 kB | [![twit install size badge](https://badgen.net/packagephobia/install/twit)](https://packagephobia.com/result?p=twit) |
72| twitter | v1.1 | ❌ | ❌ | ❌ | 50 | ~182.1 kB | [![twitter install size badge](https://badgen.net/packagephobia/install/twitter)](https://packagephobia.com/result?p=twitter) |
73| twitter-lite | v1.1, v2 | ❌ | ❌ | ❌ | 4 | ~5.3 kB | [![twitter-lite install size badge](https://badgen.net/packagephobia/install/twitter-lite)](https://packagephobia.com/result?p=twitter-lite) |
74| twitter-v2 | v2 | ❌ | ❌ | ❌ | 7 | ~4.5 kB | [![twitter-v2 install size badge](https://badgen.net/packagephobia/install/twitter-v2)](https://packagephobia.com/result?p=twitter-v2) |
75
76## Features
77
78Here's everything `twitter-api-v2` can do:
79
80### Basics:
81- Support for v1.1 and **v2 of Twitter API**
82- Make signed HTTP requests to Twitter with every auth type: **OAuth 1.0a**, **OAuth2** (even brand new user context OAuth2!) and **Basic** HTTP Authorization
83- Helpers for numerous HTTP request methods (`GET`, `POST`, `PUT`, `DELETE` and `PATCH`),
84 that handle query string parse & format, automatic body formatting and more
85- High-class support for stream endpoints, with easy data consumption and auto-reconnect on stream errors
86
87### Request helpers:
88- Automatic paginator for endpoints like user and tweet timelines,
89 allowing payload consumption with modern asynchronous iterators until your rate-limit is hit
90- Convenient methods for authentication - generate auth links and ask for tokens to your users
91- Media upload with API v1.1, including **long video & subtitles support**, automatic media type detection,
92 **chunked upload** and support for **concurrent uploads**
93- Dedicated methods that wraps API v1.1 & v2 endpoints, with **typed arguments** and fully **typed responses**
94- Typed errors, meaningful error messages, error enumerations for both v1.1 and v2
95
96### Type-safe first:
97- **Typings for tweet, user, media entities (and more) are bundled!**
98- Type-safe wrapping of dedicated methods in 3 right level: *DM*/*Read-write*/*Read-only* (just like Twitter API do!) -
99 you can declare a read-only client - you will only see the methods associated with read-only endpoints
100
101And last but not least, fully powered by native `Promise`s.
102
103## Documentation
104
105Learn how to use the full potential of `twitter-api-v2`.
106
107- Get started
108 - [Create a client and make your first request](./doc/basics.md)
109 - [Handle Twitter authentication flows](./doc/auth.md)
110 - [Explore some examples](./doc/examples.md)
111 - [Use and create plugins](./doc/plugins.md)
112- Use endpoints wrappers — ensure typings of request & response
113 - [Available endpoint wrappers for v1.1 API](./doc/v1.md)
114 - [Available endpoint wrappers for v2 API](./doc/v2.md)
115 - [Use Twitter streaming endpoints (v1.1 & v2)](./doc/streaming.md)
116- Deep diving into requests
117 - [Use direct HTTP-method wrappers](./doc/http-wrappers.md)
118 - [Use rate limit helpers](./doc/rate-limiting.md)
119 - [Handle errors](./doc/errors.md)
120 - [Master `twitter-api-v2` paginators](./doc/paginators.md)
121 - [Discover available helpers](./doc/helpers.md)
122
123## Plugins
124
125Official plugins for `twitter-api-v2`:
126- [`@twitter-api-v2/plugin-token-refresher`](https://www.npmjs.com/package/@twitter-api-v2/plugin-token-refresher): Handle OAuth 2.0 (user-context) token refreshing for you
127- [`@twitter-api-v2/plugin-rate-limit`](https://www.npmjs.com/package/@twitter-api-v2/plugin-rate-limit): Access and store automatically rate limit data
128- [`@twitter-api-v2/plugin-cache-redis`](https://www.npmjs.com/package/@twitter-api-v2/plugin-cache-redis): Store responses in a Redis store and serve cached responses
129
130See [how to use plugins here](./doc/plugins.md).