3.6 KiB
X (Twitter) API Setup Guide
This guide provides step-by-step instructions for setting up X (formerly Twitter) API integration for managing tweets and replies.
Table of Contents
- Overview
- Prerequisites
- X Developer Portal Setup
- Environment Configuration
- OAuth Redirect URI Configuration
- Permissions & Scopes
- API Tiers & Limitations
- Development vs Production
- Troubleshooting
Overview
API Version: X API v2
Base URL: https://api.twitter.com/2
Auth URL: https://twitter.com/i/oauth2/authorize
Token URL: https://api.twitter.com/2/oauth2/token
Auth Method: OAuth 2.0 with PKCE (Proof Key for Code Exchange)
Features Supported
- Fetch user tweets
- Read tweet replies (conversation threads)
- Reply to tweets
- Automatic token refresh
⚠️ Important Limitations
- Search API (for fetching replies) requires Basic tier or higher
- Free tier cannot search for replies - only direct mentions accessible
- The system supports both tiers with graceful degradation
Prerequisites
- An X (Twitter) account
- Access to X Developer Portal
- HTTPS-enabled server for production (required for OAuth redirect URIs)
X Developer Portal Setup
Step 1: Apply for Developer Access
- Navigate to X Developer Portal
- Sign in with your X account
- Click "Sign up" for free access (or choose a paid tier)
- Fill out the application form:
- Country: Select your country
- Use Case: Select "Building tools for my own use" or appropriate option
- Description: Describe your use case in detail:
"We are building a social media management dashboard that allows organizations to manage and respond to comments and replies on their X/Twitter posts from a centralized interface. This helps community managers respond faster to audience engagement."
- Review and accept the developer agreement
- Click "Submit"
Step 2: Create a Project & App
- Once approved, go to the Developer Portal
- Create a Project:
- Project Name: e.g., "PX360 Social"
- Use Case: Select "Building tools for my own use"
- Project Description: Brief description of your application
- Create an App within the project:
- App Name: Unique name for your app (must be globally unique)
- Environment: Select "Production"
Step 3: Configure OAuth 2.0
-
In your app settings, go to "Settings" tab
-
Scroll to "User authentication settings"
-
Click "Set up"
-
Select "Web App, Automated App or Bot"
-
Configure OAuth 2.0:
General Settings:
- Callback URI / Redirect URL:
- Development:
http://127.0.0.1:8000/social/callback/X/ - Production:
https://yourdomain.com/social/callback/X/
- Development:
- Website URL: Your application URL
- Terms of Service URL: (Optional) Your ToS URL
- Privacy Policy URL: (Optional) Your privacy policy URL
- Callback URI / Redirect URL:
-
Click "Save"
Step 4: Get API Credentials
- Go to "Keys and Tokens" tab in your app
- Under "OAuth 2.0 Client ID and Client Secret":
- Click "Regenerate" if needed
- Copy the Client ID → This is your
X_CLIENT_ID - Copy the Client Secret → This is your
X_CLIENT_SECRET
⚠️ Important: The Client Secret is only shown once. Store it securely!