Twitter/X API Integration with Website
Twitter/X API v2 became paid for most use cases after the 2023 pricing policy change. The free Basic tier is very limited. Main scenarios: displaying account tweets, auto-posting content, Twitter authorization.
X API Pricing Plans (2025)
| Tier | Cost | Features |
|---|---|---|
| Free | $0 | 500 posts/month, authors only |
| Basic | $100/month | 10K posts read, 100 posts write |
| Pro | $5000/month | 1M posts read, 300K posts write |
For displaying tweets on your website, Basic tier or oEmbed embedding (without API) is sufficient.
Embedding without API (oEmbed)
public function embedTweet(string $tweetUrl): string
{
$resp = Http::get('https://publish.twitter.com/oembed', [
'url' => $tweetUrl,
'theme' => 'light',
'hide_thread' => 'true',
'omit_script' => 'true',
]);
return $resp->json('html');
}
Twitter API v2: Publishing Posts
import tweepy
client = tweepy.Client(
consumer_key=CONSUMER_KEY,
consumer_secret=CONSUMER_SECRET,
access_token=ACCESS_TOKEN,
access_token_secret=ACCESS_TOKEN_SECRET,
)
def post_tweet(text: str, media_ids: list = None) -> str:
resp = client.create_tweet(text=text, media_ids=media_ids)
return resp.data['id']
OAuth 2.0 Authorization via Twitter
Route::get('/auth/twitter/redirect', function () {
$codeVerifier = Str::random(64);
$codeChallenge = base64url_encode(hash('sha256', $codeVerifier, true));
session(['twitter_code_verifier' => $codeVerifier]);
return redirect('https://twitter.com/i/oauth2/authorize?' . http_build_query([
'response_type' => 'code',
'client_id' => config('services.twitter.client_id'),
'redirect_uri' => route('auth.twitter.callback'),
'scope' => 'tweet.read users.read offline.access',
'state' => Str::random(16),
'code_challenge' => $codeChallenge,
'code_challenge_method' => 'S256',
]));
});
Timeline
Auto-posting via API: 2–3 days. OAuth authorization: 2 days.







