This example demonstrates the fundamental workflow of using T3Router: initializing a client, configuring it, and sending a simple message.
Complete Example
use dotenv::dotenv;
use t3router::t3::client::Client;
use t3router::t3::config::Config;
use t3router::t3::message::{Message, Type};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Load environment variables from .env file
dotenv().ok();
// Get authentication credentials
let cookies = std::env::var("COOKIES").expect("COOKIES not set");
let convex_session_id = std::env::var("CONVEX_SESSION_ID")
.expect("CONVEX_SESSION_ID not set");
// Create a new client instance
let mut client = Client::new(cookies, convex_session_id);
// Initialize the client
if client.init().await? {
println!("Client initialized successfully");
}
// Create a default configuration
let config = Config::new();
// Send a message and get a response
let response = client
.send(
"gemini-2.5-flash-lite",
Some(Message::new(
Type::User,
"What is the capital of France?".to_string(),
)),
Some(config),
)
.await?;
println!("User: What is the capital of France?");
println!("Assistant: {}", response.content);
Ok(())
}
Step-by-Step Breakdown
1. Environment Setup
dotenv().ok();
let cookies = std::env::var("COOKIES").expect("COOKIES not set");
let convex_session_id = std::env::var("CONVEX_SESSION_ID")
.expect("CONVEX_SESSION_ID not set");
Load authentication credentials from environment variables. These are required to authenticate with the t3.chat API.
2. Client Initialization
let mut client = Client::new(cookies, convex_session_id);
if client.init().await? {
println!("Client initialized successfully");
}
Create a new client and initialize it. The init() method establishes the connection and prepares the client for use.
3. Configuration
let config = Config::new();
Create a configuration object with default settings. This controls features like search and reasoning effort.
4. Sending a Message
let response = client
.send(
"gemini-2.5-flash-lite",
Some(Message::new(
Type::User,
"What is the capital of France?".to_string(),
)),
Some(config),
)
.await?;
Send a message to the specified model and await the response. The response contains the assistant’s reply in the content field.
Expected Output
Client initialized successfully
User: What is the capital of France?
Assistant: The capital of France is Paris.
Always call client.init().await? before sending messages. This ensures the client is properly authenticated and ready.
The send() method automatically manages conversation state. Each call adds the message and response to the conversation history.
Available Models
You can use different models by changing the first parameter:
"gemini-2.5-flash-lite" - Fast, lightweight responses
"gemini-2.5-pro-2m" - More capable model with larger context
"gpt-4" - OpenAI GPT-4
"claude-3-7-sonnet" - Anthropic Claude
"gpt-image-1" - Image generation
"gemini-imagen-4" - Google’s image generation
Next Steps