The twitterSearch tool searches X (formerly Twitter) for recent, relevant public tweets using the Twitter API v2. It returns detailed tweet information including author details and engagement metrics.
export const twitterSearch = ai.defineTool( { name: 'twitterSearch', description: 'Searches X (formerly Twitter) for recent, relevant public tweets using a keyword query. Returns a list of tweets with author and metric details.', inputSchema: TwitterSearchInputSchema, outputSchema: TwitterSearchOutputSchema, }, async (input) => { ... });
Your Twitter API v2 Bearer Token. Obtain one from the Twitter Developer Portal.Error thrown if missing: TWITTER_BEARER_TOKEN is not configured. Please add it to your .env file.
The tool accepts input conforming to TwitterSearchInputSchema:
const TwitterSearchInputSchema = z.object({ query: z.string().describe( 'The search query for X/Twitter. Exclude hashtags or "from:" filters, just provide the keywords.' ),});
The search keywords for X/Twitter. Provide plain keywords without hashtags or advanced filters.Note: The tool automatically adds lang:en -is:retweet filters to the query.
Returns an array of tweets conforming to TwitterSearchOutputSchema:
const TweetAuthorSchema = z.object({ name: z.string().describe("The author's display name."), username: z.string().describe("The author's unique username/handle."), profile_image_url: z.string().url().describe("URL to the author's profile picture."),});const PublicMetricsSchema = z.object({ retweet_count: z.number(), reply_count: z.number(), like_count: z.number(), impression_count: z.number(),});const TweetResultSchema = z.object({ id: z.string().describe('The unique ID of the tweet.'), text: z.string().describe('The full text content of the tweet.'), author: TweetAuthorSchema, created_at: z.string().describe('The date and time the tweet was created.'), public_metrics: PublicMetricsSchema.describe('Engagement metrics for the tweet.'),});const TwitterSearchOutputSchema = z.array(TweetResultSchema);
Thrown when: TWITTER_BEARER_TOKEN is not set or equals 'YOUR_BEARER_TOKEN_HERE'Message: TWITTER_BEARER_TOKEN is not configured. Please add it to your .env file.