- OAuth 2.0 Authentication: Secure authentication with automatic token refresh and caching
- Tweet Posting: Create and publish tweets via X/Twitter V2 API
- Rate Limiting: Built-in rate limit handling and error management
- Async/Await Support: Fully asynchronous API built with Tokio
- Environment Configuration: Easy setup using environment variables
- Token Management: Automatic bearer token refresh with optional refresh token persistence
- Automatic Token Refresh: Seamlessly handles token expiration without manual intervention
- Error Handling: Comprehensive error types for different API scenarios
- PKCE Flow Support: Enhanced security with Proof Key for Code Exchange
- TLS Security: All requests use secure HTTPS connections with rustls
- Simple API: Clean, intuitive interface for common X/Twitter operations
- Type Safety: Full Rust type safety with serde serialization/deserialization
- Comprehensive Documentation: Well-documented code with examples
- Environment-based Configuration: Secure credential management
Set up the required environment variables for X/Twitter API access:
# Required: Your X/Twitter App Client ID and Secret
export X_CLIENT_ID="your_client_id_here"
export X_CLIENT_SECRET="your_client_secret_here"
# Optional: Refresh token to avoid re-authentication
export X_REFRESH_TOKEN="your_refresh_token_here"
use xv2api::{Api, tweets::ApiTweet};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize API client from environment variables.
let api = Api::from_env();
// Post a tweet.
let resp = api.tweet("Hello from XV2API! 🦀".to_string()).await?;
println!("Response: {resp:?}");
println!("Tweet posted successfully!");
Ok(())
}
XV2API follows a modular architecture designed for extensibility and maintainability:
xv2api/
├── src/
│ ├── lib.rs # Main API client and core functionality
│ ├── auth.rs # OAuth 2.0 authentication module
│ ├── tweets.rs # Tweet-related API endpoints
│ └── error.rs # Error types and handling
├── Cargo.toml # Project configuration and dependencies
└── README.md # This documentation
Api
: Main client struct handling HTTP requests and authenticationAuthenticator
: OAuth 2.0 flow management with token cachingApiTweet
: Trait defining tweet-related operationsError
: Comprehensive error handling for various failure scenarios
- Async-First: Built with Tokio for high-performance async operations
- Type Safety: Leverages Rust's type system for compile-time guarantees
- Error Transparency: Clear error types for different failure modes
- Token Management: Automatic token refresh with minimal user intervention
- Modular Design: Easy to extend with new API endpoints
If you find this project helpful and would like to support its development, you can buy me a coffee!
Your support is greatly appreciated and motivates me to keep improving this project.
- Fiat
- Crypto
- Bitcoin
bc1pedlrf67ss52md29qqkzr2avma6ghyrt4jx9ecp9457qsl75x247sqcp43c
- Ethereum
0x3e25247CfF03F99a7D83b28F207112234feE73a6
- Polkadot
156HGo9setPcU2qhFMVWLkcmtCEGySLwNqa3DaEiYSWtte4Y
- Bitcoin
Thank you for your support!
We would like to extend our heartfelt gratitude to the following projects and contributors:
- The Rust community for their continuous support and development of the Rust ecosystem.
- OAuth2 crate for providing excellent OAuth 2.0 implementation
- X/Twitter Developer Community for API documentation and support
Licensed under GPL-3.0.