Basic Twitter API methods for fetching users, tweets, and searching recent tweets. Includes support for plan-aware rate-limiting.

  • package: @agentic/twitter
  • exports: class TwitterClient, namespace twitter
  • env vars: TWITTER_API_KEY, TWITTER_API_PLAN
  • source
  • twitter api docs

Install

npm install @agentic/twitter

Usage

import { TwitterClient, createTwitterV2Client } from '@agentic/twitter'

// Requires Nango connection ID and callback URL environment variables
const rawClient = await createTwitterV2Client()

const twitter = new TwitterClient({ client: rawClient })
const res = await twitter.createTweet({
  text: 'hello, world'
})

This example uses Nango for OAuth support via the createTwitterV2Client helper function, but you can pass any instance of the underlying TwitterV2Client to the TwitterClient constructor.

All TwitterClient methods are automatically throttled based on your Twitter API plan. You can set the twitter plan by setting the TWITTER_API_PLAN environment variable or by passing the twitterApiPlan parameter to the TwitterClient constructor.

This twitter client is pretty robust, taking into account my learnings from building a Twitter bot which gained over 150k followers.