Twitter OpenAPI TypeScript Generated Client

0.0.38 · active · verified Sun Apr 19

This package, `twitter-openapi-typescript-generated`, provides an automatically generated TypeScript/JavaScript client for the Twitter API, based on its OpenAPI specification. It leverages the native Fetch API for all network requests and is designed for use across diverse JavaScript environments, including Node.js, Webpack-bundled applications, and modern browsers. It supports both CommonJS and ES module systems, with full TypeScript type definitions automatically resolved. While the prompt specifies version 0.0.38, the related `twitter-openapi-typescript` package on npm is currently at 0.0.55, indicating a rapid release cadence in a pre-1.0 state. This early `0.0.x` versioning suggests that the API and its implementation may undergo frequent, potentially breaking, changes without strict adherence to semantic versioning. It serves as a direct, machine-generated interface to the Twitter API, differentiating itself from more 'human-friendly' wrappers by offering a direct reflection of the OpenAPI spec.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart initializes the Twitter API client with a bearer token and fetches a user's public profile information by screen name, demonstrating basic client setup and API interaction.

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

const main = async () => {
  const bearerToken = process.env.TWITTER_BEARER_TOKEN ?? '';

  if (!bearerToken) {
    console.error('Please set the TWITTER_BEARER_TOKEN environment variable.');
    process.exit(1);
  }

  try {
    // Initialize the API client with a bearer token
    const api = new TwitterOpenApi();
    api.bearer = bearerToken; // Direct bearer token assignment, as shown in similar usage examples.

    // Get a guest client if bearer token is for guest access or specific API requires it
    const client = await api.getGuestClient(); 
    
    // Example: Fetching user details by screen name (e.g., Elon Musk)
    const screenName = 'elonmusk';
    console.log(`Attempting to fetch user: @${screenName}`);

    const response = await client.getUserApi().getUserByScreenName({ screenName });

    const userLegacy = response.data?.user?.legacy;

    if (userLegacy) {
      console.log(`Found user: ${userLegacy.name} (@${userLegacy.screenName})`);
      console.log(`Followers: ${userLegacy.followersCount}, Following: ${userLegacy.friendsCount}`);
    } else {
      console.log(`User @${screenName} not found or data unavailable.`);
      console.log('Full response:', JSON.stringify(response, null, 2));
    }
  } catch (error) {
    console.error('An error occurred:', error);
    if (error instanceof Error) {
        console.error('Error message:', error.message);
    }
  }
};

main();

view raw JSON →