TS 中文文档 TS 中文文档
指南
GitHub (opens new window)
指南
GitHub (opens new window)
  • 入门教程

    • TypeScript 手册
    • 基础知识
    • 日常类型
    • 类型缩小
    • 更多关于函数
    • 对象类型
    • 从类型创建类型
    • 泛型
    • Keyof 类型运算符
    • Typeof 类型运算符
    • 索引访问类型
    • 条件类型
    • 映射类型
    • 模板字面类型
    • 类
    • 模块
  • 参考手册

  • 项目配置

Twitter API SDK for TypeScript


Introduction


A TypeScript SDK for the Twitter API. This library is built with TypeScript developers in mind, but it also works with JavaScript.

Note: This SDK is in beta and is not ready for production

You can find examples of using the client in the examples/ directory

Note: Only Twitter API V2 is supported

Features


Full type information for requests and responses
OAuth2 support
Supports Node.js 14+. Doesn't work in browser environments due to the Twitter API not supporting CORS

Installing


  1. ``` sh
  2. npm install twitter-api-sdk

  3. ```

Client


To setup the client we will authenticate with a bearer-token as follows

  1. ``` ts
  2. import { Client } from "twitter-api-sdk";

  3. const client = new Client("MY-BEARER-TOKEN");
  4. ```

For more information about authentication go here

Examples


Consuming a Stream


  1. ``` ts
  2. import { Client } from "twitter-api-sdk";

  3. const client = new Client(process.env.BEARER_TOKEN);

  4. async function main() {
  5.   const stream = client.tweets.sampleStream({
  6.     "tweet.fields": ["author_id"],
  7.   });
  8.   for await (const tweet of stream) {
  9.     console.log(tweet.data?.author_id);
  10.   }
  11. }

  12. main();
  13. ```

Getting a Tweet


  1. ``` ts
  2. import { Client } from "twitter-api-sdk";

  3. const client = new Client(process.env.BEARER_TOKEN);

  4. async function main() {
  5.   const tweet = await client.tweets.findTweetById("20");
  6.   console.log(tweet.data.text);
  7. }

  8. main();
  9. ```

Streaming


For endpoints that return a stream you get sent back an Async Generator which you can iterate over:

  1. ``` ts
  2. const stream = client.tweets.sampleStream();

  3. for await (const tweet of stream) {
  4.   console.log(tweet.data.text);
  5. }
  6. ```

Pagination


For endpoints that have pagination you can

  1. ``` ts
  2. const followers = client.users.usersIdFollowers("20");

  3. for await (const page of followers) {
  4.   console.log(page.data);
  5. }

  6. // This also works
  7. const followers = await client.users.usersIdFollowers("20");
  8. console.log(followers.data);
  9. ```

Authentication


This library supports App-only Bearer Token and OAuth 2.0

You can see various examples on how to use the authentication in examples/

Getting Started


Make sure you turn on OAuth2 in your apps user authentication settings, and set the type of app to be either a confidential client or a public client.

Creating a Public Auth Client


  1. ``` ts
  2. const authClient = new auth.OAuth2User({
  3.   client_id: process.env.CLIENT_ID,
  4.   callback: "http://127.0.0.1:3000/callback",
  5.   scopes: ["tweet.read", "users.read", "offline.access"],
  6. });

  7. const client = new Client(authClient);
  8. ```

Creating a Confidential Auth Client


  1. ``` ts
  2. const authClient = new auth.OAuth2User({
  3.   client_id: process.env.CLIENT_ID,
  4.   client_secret: process.env.CLIENT_SECRET,
  5.   callback: "http://127.0.0.1:3000/callback",
  6.   scopes: ["tweet.read", "users.read", "offline.access"],
  7. });

  8. const client = new Client(authClient);
  9. ```

Generating an Authentication URL


  1. ``` ts
  2. const authUrl = authClient.generateAuthURL({
  3.   code_challenge_method: "s256",
  4. });
  5. ```

Requesting an Access Token


Once the user has approved the OAuth flow, you will receive a code query parameter at the callback URL you specified.

  1. ``` ts
  2. await authClient.requestAccessToken(code);
  3. ```

Revoking an Access Token


  1. ``` ts
  2. const response = await authClient.revokeAccessToken();
  3. ```

Contributing


Note this is only for developers who want to contribute code to the SDK

Clone the Repository


  1. ``` sh
  2. git clone https://github.com/twitterdev/twitter-api-typescript-sdk

  3. ```

Running the Generation Script


Generating the SDK with the latest OpenAPI spec. The version is any valid SemVer version

  1. ``` sh
  2. yarn generate 1.0.0

  3. ```

Generating the SDK with a local OpenAPI specification file.

  1. ``` sh
  2. yarn generate 1.0.0 --specFile ~/path/to/file/openapi.json

  3. ```

The files generated are put in the src/gen directory, these files are not edited manually.

Building


  1. ``` sh
  2. yarn build

  3. ```

Testing


  1. ``` sh
  2. yarn test

  3. ```
Last Updated: 2023-09-03 17:10:52