Overview
ThetwitterSearch tool searches X (formerly Twitter) for recent, relevant public tweets using the official Twitter API v2. It returns tweets with full author information and engagement metrics.
This tool powers the “Social Pulse” feature in Argument Cartographer.
Function Signature
src/ai/tools/twitter-search.ts:38
Input Schema
The search query for X/Twitter. Exclude hashtags or “from:” filters - just provide the keywords.
Input Type
Output Schema
An array of tweet objects with author information and engagement metrics.
Tweet Object Structure
Output Type
How It Works
- Query Construction: Adds filters for English language and excludes retweets
- API Request: Calls Twitter API v2 recent search endpoint
- Expansions: Requests full user objects and tweet metrics
- Hydration: Combines tweet data with author information
- Sorting: Results sorted by relevancy (Twitter’s algorithm)
- Return: Returns up to 20 most relevant tweets
Example Usage
Standalone
With AI Flow
In Blueprint Generation
The tool is automatically used in thegenerateArgumentBlueprint flow:
Search Query Filters
The tool automatically applies these filters:lang:en- English language tweets only-is:retweet- Excludes retweets, only original tweets
API Parameters
- max_results: 20 tweets
- sort_order: relevancy
- tweet.fields: created_at, author_id, public_metrics
- expansions: author_id
- user.fields: profile_image_url, username, name
Environment Variables
Your Twitter API v2 Bearer Token. Get one from the Twitter Developer Portal.
Error Handling
- Returns empty array
[]if no tweets found (not an error) - Throws error if API request fails
- Provides fallback profile images for missing data
- Logs errors to console for debugging
Rate Limits
Twitter API v2 rate limits:- App-level: 450 requests per 15 minutes
- User-level: 180 requests per 15 minutes
Performance
- Average response time: 1-2 seconds
- Returns up to 20 tweets
- Sorted by relevancy for best results
- Includes full engagement metrics
Use Cases
- Social Pulse: Gauge public sentiment on topics
- Trending Analysis: See what people are discussing
- Evidence Gathering: Find real-world examples and opinions
- Sentiment Analysis: Analyze tone and perspective
- Debate Context: Understand the broader conversation
Related
- Generate Blueprint Flow - uses Twitter Search for Social Pulse
- Ask More Flow - can search Twitter for additional context
- Web Search Tool
