A high-performance Go client for Twitter's API with 95-100% success rates, intelligent caching, and production-grade reliability.
- 95-100% Success Rate - Breakthrough matrix algorithm fix achieving production reliability
- Real Algorithm Implementation - Authentic Twitter X-Client-Transaction-ID generation
- Production Caching - 6-hour HTML data cache, 3-hour animation keys
- Automatic Retry - Exponential backoff with intelligent error recovery
- Thread-Safe - Concurrent request handling with proper locking
- Zero Configuration - Works out of the box
- 12 API Endpoints - Complete Twitter functionality
- Comprehensive Metrics - Built-in monitoring and performance tracking
package main
import (
"context"
"fmt"
"log"
"github.com/your-org/xapi"
)
func main() {
// Create production-ready client
client, err := xapi.New()
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
// Get user info - 95-100% success rate
user, err := client.User(ctx, "nasa")
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s has %d followers\n", user.Name, user.FollowersCount)
// Get recent tweets with options
tweets, err := client.Tweets(ctx, "nasa", xapi.WithCount(5))
if err != nil {
log.Fatal(err)
}
fmt.Printf("Retrieved %d tweets\n", len(tweets))
// Get full profile with engagement stats
profile, err := client.Profile(ctx, "nasa", 10)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Average engagement: %.1f\n", profile.Stats.AvgEngagement)
}client, err := xapi.New() // Uses optimized production configclient, err := xapi.NewDevelopmentClient() // Faster refresh, more loggingconfig := &xapi.ProductionConfig{
HTMLDataCacheLifetime: 6 * time.Hour, // Cache HTML for 6 hours
TransactionIDLifetime: 1 * time.Hour, // Reuse IDs for 1 hour
EnableAutoRetry: true, // Auto-retry on failures
MaxRetryAttempts: 3, // Up to 3 retries
EnableDebugLogging: false, // Production logging
}
client, err := xapi.NewClient(config)// Enable debug logging
client.Debug(true)
// Get real-time metrics
metrics := client.GetMetrics()
fmt.Printf("Success rate: %.1f%%\n", client.GetSuccessRate())
fmt.Printf("Total requests: %d\n", metrics.TotalRequests)
fmt.Printf("Average latency: %.0fms\n", metrics.AverageLatency)
fmt.Printf("Cache hits: %d\n", metrics.CacheHits)
fmt.Printf("Uptime: %v\n", client.GetUptime())
// Force refresh if needed
err = client.ForceRefresh()Fetches user profile with high reliability.
user, err := client.User(ctx, "nasa")
user, err := client.User(ctx, "@nasa") // @ optionalFetches user tweets with functional options.
// Default: 20 tweets
tweets, err := client.Tweets(ctx, "nasa")
// Custom count
tweets, err := client.Tweets(ctx, "nasa", xapi.WithCount(10))
// With pagination cursor
tweets, err := client.Tweets(ctx, "nasa",
xapi.WithCount(10),
xapi.WithCursor("cursor123"))Fetches tweets with full pagination support.
page, err := client.TweetsPage(ctx, "nasa", xapi.WithCount(20))
if page.HasMore && page.NextCursor != nil {
nextTweets, err := client.Tweets(ctx, "nasa",
xapi.WithCursor(page.NextCursor.Value))
}Complete user profile with tweets and engagement statistics.
profile, err := client.Profile(ctx, "nasa", 10)
fmt.Printf("Top tweet engagement: %d\n", profile.Stats.TopTweet.FavoriteCount)Users that the specified user follows.
The specified user's followers.
Blue verified followers only.
Single tweet by ID.
tweet, err := client.Tweet(ctx, "1953893398995243332")User's highlighted/pinned tweets.
Live broadcast/stream information.
Business profile team timeline.
Bulk user lookup in one API call.
users, err := client.UsersByIDs(ctx, []string{"11348282", "783214"})// Production config (default)
config := xapi.DefaultProductionConfig()
// Development config (faster refresh, more logging)
config := xapi.DevelopmentConfig()
// Ultra-fresh config (no caching, for testing)
config := xapi.UltraFreshConfig()client.Debug(true) // Enable debug logging
client.SetDebugMode(false) // Disable debug loggingClean, composable options for API methods:
// Available options
func WithCount(count int) TweetOption
func WithCursor(cursor string) TweetOption
func WithPagination() TweetOption
// Usage examples
tweets, err := client.Tweets(ctx, "nasa") // Default
tweets, err := client.Tweets(ctx, "nasa", xapi.WithCount(5)) // Custom count
tweets, err := client.Tweets(ctx, "nasa", // Multiple options
xapi.WithCount(10),
xapi.WithCursor("cursor123"))- HTML Data: 6-hour cache lifetime (production)
- Animation Keys: 3-hour cache lifetime
- Transaction IDs: 1-hour reuse for efficiency
- Automatic Refresh: Background refresh on expiration
- Exponential Backoff: 500ms β 1s β 2s delays
- Smart Error Handling: Auth errors trigger immediate refresh
- Max Attempts: 3 retries per request (configurable)
- Context Support: Proper cancellation handling
- Built-in Limits: Respects Twitter's 50 requests/minute
- Development Mode: Higher limits (100/minute) for testing
- Automatic Throttling: No manual rate limiting needed
The client handles errors intelligently:
tweets, err := client.Tweets(ctx, "nasa")
if err != nil {
switch {
case strings.Contains(err.Error(), "rate limited"):
// Rate limit - automatic backoff applied
case strings.Contains(err.Error(), "authentication"):
// Auth error - transaction ID refreshed automatically
case strings.Contains(err.Error(), "failed after"):
// Multiple retries exhausted
}
}- 401/403 Auth Errors: Auto-refresh transaction ID and retry
- 500/502/503/504 Server Errors: Exponential backoff retry
- 429 Rate Limits: Respect limits without transaction refresh
- Network Errors: Basic retry with fresh transaction ID
client.go- Main client with unified implementationendpoints.go- All 12 Twitter API endpointstransaction.go- Production transaction ID generatorconfig.go- Production configuration managementtypes.go- Complete type definitionsxpff_generator.go- XPFF header generation
- Real Algorithm: Authentic Twitter transaction ID generation
- Corrected Matrix: Critical fix achieving 95-100% success rates
- Production Caching: Intelligent cache layers with different lifetimes
- Thread Safety: Proper locking for concurrent operations
- Comprehensive Metrics: Built-in monitoring and performance tracking
type User struct {
ID string `json:"id"`
RestID string `json:"rest_id"`
Name string `json:"name"`
ScreenName string `json:"screen_name"`
Description string `json:"description"`
Location string `json:"location"`
FollowersCount int `json:"followers_count"`
FriendsCount int `json:"friends_count"`
StatusesCount int `json:"statuses_count"`
CreatedAt time.Time `json:"created_at"`
Verified bool `json:"verified"`
IsBlueVerified bool `json:"is_blue_verified"`
ProfileImageURL string `json:"profile_image_url"`
ProfileBannerURL string `json:"profile_banner_url"`
// ... additional fields
}type Tweet struct {
ID string `json:"id"`
RestID string `json:"rest_id"`
FullText string `json:"full_text"`
CreatedAt time.Time `json:"created_at"`
ConversationID string `json:"conversation_id_str"`
Author *User `json:"author,omitempty"`
// Engagement metrics
BookmarkCount int `json:"bookmark_count"`
FavoriteCount int `json:"favorite_count"`
QuoteCount int `json:"quote_count"`
ReplyCount int `json:"reply_count"`
RetweetCount int `json:"retweet_count"`
ViewCount int `json:"view_count"`
// Rich content
Entities *TweetEntities `json:"entities,omitempty"`
ExtendedEntities *ExtendedEntities `json:"extended_entities,omitempty"`
// ... additional fields
}type TweetPage struct {
Tweets []*Tweet `json:"tweets"`
NextCursor *Cursor `json:"next_cursor,omitempty"`
PrevCursor *Cursor `json:"prev_cursor,omitempty"`
HasMore bool `json:"has_more"`
}type Profile struct {
User *User `json:"user"`
Tweets []*Tweet `json:"tweets"`
Stats *Stats `json:"stats"`
}
type Stats struct {
TotalEngagement int `json:"total_engagement"`
AvgEngagement float64 `json:"avg_engagement"`
TopTweet *Tweet `json:"top_tweet"`
}go mod init your-project
# Copy the xapi/ directory to your projectOr if published:
go get github.com/your-org/xapigolang.org/x/time- Rate limitinggolang.org/x/net- HTML parsing for transaction generation
This client achieves 95-100% success rates through:
- Corrected Matrix Algorithm - Fixed critical browser animation simulation
- Real Twitter Data - Live homepage and ondemand.s file fetching
- Production Caching - Intelligent cache layers reducing API calls
- Robust Error Handling - Automatic retry with fresh transaction IDs
- Thread-Safe Operations - Proper concurrent request handling
- Before: ~40% success rate with matrix bugs
- After: 95-100% success rate with corrected algorithm
- Production Ready: Reliable enough for commercial applications
MIT