# twitter-api-sdk-fork

> A TypeScript SDK for the Twitter API

Latest version **1.0.9** (published 2022-07-08) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install twitter-api-sdk-fork
pnpm add twitter-api-sdk-fork
yarn add twitter-api-sdk-fork
bun add twitter-api-sdk-fork
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.9 |
| Published | 2022-07-08 |
| First published | 2022-06-30 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=14 |
| Dependencies | 2 |
| Unpacked size | 327.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 997 |
| Maintainers | lightyagami200 |

## Links

- npm: https://www.npmjs.com/package/twitter-api-sdk-fork
- Repository: https://github.com/twitterdev/twitter-api-typescript-sdk
- Homepage: https://github.com/twitterdev/twitter-api-typescript-sdk#readme
- Issues: https://github.com/twitterdev/twitter-api-typescript-sdk/issues
- npm.io page: https://npm.io/package/twitter-api-sdk-fork

## Dependencies (2)

- [node-fetch](https://npm.io/package/node-fetch.md) ^2.6.1
- [abort-controller](https://npm.io/package/abort-controller.md) ^3.0.0

## Recent versions

- 1.0.9 (latest) — 2022-07-08
- 1.0.8 — 2022-06-30

## README

# 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/](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

```
npm install twitter-api-sdk
```

## Client

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

```typescript
import { Client } from "twitter-api-sdk";

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

For more information about authentication [go here](#authentication)

## Examples

### Consuming a stream

```typescript
import { Client } from "twitter-api-sdk";

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

async function main() {
  const stream = client.tweets.sampleStream({
    "tweet.fields": ["author_id"],
  });
  for await (const tweet of stream) {
    console.log(tweet.data?.author_id);
  }
}

main();
```

### Getting a tweet

```typescript
import { Client } from "twitter-api-sdk";

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

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

main();
```

## Streaming

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

```typescript
const stream = client.tweets.sampleStream();

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

## Pagination

For endpoints that have pagination you can

```typescript
const followers = client.users.usersIdFollowers("20");

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

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

## 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/](examples/)

## Getting Started

Make sure you to turn on OAuth2 in your apps user authentication settings, and set the type of app to be a confidential client (Web App or Automated App or Bot).

### Creating an Auth Client

```typescript
const authClient = new auth.OAuth2User({
  client_id: process.env.CLIENT_ID,
  client_secret: process.env.CLIENT_SECRET,
  callback: "http://127.0.0.1:3000/callback",
  scopes: ["tweet.read", "users.read", "offline.access"],
});

const client = new Client(authClient);
```

### Generating an Authentication URL

```typescript
const authUrl = authClient.generateAuthURL({
  code_challenge_method: "s256",
});
```

### Getting an Access Token

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

```typescript
await authClient.requestAccessToken(code);
```

### Revoking an Access Token

```typescript
const response = await authClient.revokeAccessToken();
```

---
_Source: https://npm.io/package/twitter-api-sdk-fork · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
