Twitter Internal API Client

0.0.55 · active · verified Sun Apr 19

twitter-openapi-typescript is a TypeScript library that provides an implementation of Twitter's (now X's) *internal* API, allowing programmatic interaction with the platform without relying on the official, public API. It is currently in a pre-1.0 development phase, indicated by its `0.0.55` version, suggesting frequent updates and potential breaking changes without prior notice due to the nature of interacting with internal, undocumented endpoints. The library emphasizes ease of use with guest clients for public data access and supports authenticated clients via cookie-based login (`ct0`, `auth_token`). A key differentiator is its focus on the internal API, which offers capabilities beyond the public API, though at a higher risk of instability and account suspension. The project ships with full TypeScript types, enhancing developer experience.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates fetching a user's details (followers, friends count) using a guest client.

import { TwitterOpenApi } from 'twitter-openapi-typescript';

async function getUserDetails() {
  const api = new TwitterOpenApi();
  // To avoid rate limits or access private data, you might need to log in:
  // const client = await api.getClientFromCookies({
  //   ct0: process.env.TWITTER_CT0 ?? '',
  //   auth_token: process.env.TWITTER_AUTH_TOKEN ?? ''
  // });
  const client = await api.getGuestClient();
  const response = await client.getUserApi().getUserByScreenName({ screenName: 'elonmusk' });
  const userLegacy = response.data?.user?.legacy;
  if (userLegacy) {
    console.log(`@${userLegacy.screenName}`);
    console.log(`Friends Count: ${userLegacy.friendsCount}, Followers Count: ${userLegacy.followersCount}`);
  } else {
    console.log('User not found or API response format changed.');
  }
}

getUserDetails();

view raw JSON →