Rust SDK
Rust Quick Start
WoWSQL Rust SDK — Get started in minutes
Get your API key: Use the WoWSQL project dashboard to create or copy your API key. The same key works for database, auth, and storage (unified API key).
Installation
Official package: View on crates.io →
cargo add wowsql
cargo add tokio --features full
Or add to Cargo.toml:
[dependencies]
wowsql = "1.0"
tokio = { version = "1.0", features = ["full"] }
Initialize Client
use wowsql::{Client, Config};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(Config {
project_url: "https://myproject.wowsql.com".to_string(),
api_key: "your_api_key_here".to_string(),
});
// Always use select() before get()/execute()
let users = client.table("users").select("*").limit(10).execute().await?;
for user in users.data {
println!("{}", user["name"]);
}
Ok(())
}
Query Data
Basic Queries
let users = client.table("users").select("*").get().await?;
let adults = client.table("users").select("*").gte("age", 18).get().await?;
let names = client.table("users").select("id", "name").get().await?;
Advanced Queries
let active_users = client.table("users")
.select("*")
.eq("status", "active")
.gt("age", 18)
.order("created_at", "desc")
.limit(20)
.offset(0)
.execute()
.await?;
println!("Found {} users", active_users.count);
Filter Helper Methods
let users = client.table("users")
.eq("status", "active")
.gt("age", 18)
.like("email", "%@gmail.com")
.in_("role", serde_json::json!(["admin", "moderator"]))
.between("created_at", "2024-01-01", "2024-12-31")
.get()
.await?;
GROUP BY and Aggregates
Basic GROUP BY
let result = client.table("products")
.select("category", "COUNT(*) as count", "AVG(price) as avg_price")
.group_by("category")
.get()
.await?;
HAVING Clause
let result = client.table("products")
.select("category", "COUNT(*) as count")
.group_by("category")
.having("COUNT(*)", "gt", 10)
.get()
.await?;
CRUD Operations
Create Record
let result = client.table("users").create(serde_json::json!({
"name": "John Doe",
"email": "john@example.com",
"age": 30
})).await?;
println!("Created user with ID: {}", result["id"]);
Update Record
client.table("users").update(123, serde_json::json!({
"email": "newemail@example.com"
})).await?;
Delete Record
client.table("users").delete(123).await?;
Get Single Record by ID
let user = client.table("users").get_by_id(123).await?;
println!("User: {}", user["name"]);
Get First Record
let user = client.table("users").eq("status", "active").first().await?;
if let Some(u) = user { println!("First active user: {}", u["name"]); }
Filter Operators
Available operators: eq, neq, gt, gte, lt, lte, like, is, in, not_in, between, not_between. Use .eq("col", value), .filter("col", "op", value), etc.
Error Handling
match client.table("users").select("*").get().await {
Ok(users) => println!("Success: {} users", users.data.len()),
Err(e) => {
if e.to_string().contains("401") { println!("Authentication failed — check your API key"); }
else if e.to_string().contains("404") { println!("Table or resource not found"); }
else { println!("Error: {}", e); }
}
}
Authentication
Use the same API key for authentication. One key works for database and auth.
Sign Up & Sign In
let auth = wowsql::Auth::new(Config {
project_url: "myproject".to_string(),
api_key: "your_api_key".to_string(),
});
let result = auth.sign_up("user@example.com", "SecurePassword123", Some("John Doe")).await?;
let login = auth.sign_in("user@example.com", "SecurePassword123").await?;
Password Reset
auth.forgot_password("user@example.com").await?;
auth.reset_password("reset_token", "NewSecurePassword123").await?;
OTP Authentication
auth.send_otp("user@example.com", "login").await?;
let result = auth.verify_otp("user@example.com", "123456", "login", None).await?;
println!("Logged in: {}", result.user.id);
Magic Link & Email Verification
auth.send_magic_link("user@example.com", "login").await?;
OAuth Authentication
let oauth_url = auth.get_oauth_authorization_url(
"github",
"http://localhost:5000/auth/callback"
).await?;
// Redirect user to oauth_url.authorization_url
let result = auth.exchange_oauth_callback(
"github",
"authorization_code_from_callback",
None
).await?;
println!("OAuth login successful: {}", result.user.id);
Storage Operations
Initialize Storage
let storage = wowsql::Storage::new(Config {
project_url: "myproject".to_string(),
api_key: "your_api_key".to_string(),
});
File Operations
// Upload file
let file_data = std::fs::read("path/to/document.pdf")?;
let result = storage.upload_bytes(&file_data, "uploads/document.pdf", Some("application/pdf")).await?;
// Get presigned URL
let url = storage.get_presigned_url("uploads/document.pdf", 3600).await?;
// List files
let files = storage.list_files("uploads/").await?;
// Delete file
storage.delete_file("uploads/document.pdf").await?;
// Check quota
let quota = storage.get_quota().await?;
Download or View File
Via REST: use GET .../files/{file_key}/download for attachment download, or GET .../files/{file_key}/view to view inline in the browser.
Schema Management (Creating Tables)
Use the schema client with a service role key to create tables programmatically:
use wowsql::{WowSQLSchema, ColumnDefinition, CreateTableRequest};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let schema = WowSQLSchema::new(
"https://your-project.wowsql.com",
"service_role_key",
)?;
schema.create_table(CreateTableRequest {
table_name: "products".to_string(),
columns: vec![
ColumnDefinition::new("id", "INT")
.auto_increment(true)
.unique(true)
.not_null(true),
ColumnDefinition::new("name", "VARCHAR(255)")
.not_null(true),
ColumnDefinition::new("price", "DECIMAL(10,2)")
.not_null(true),
],
primary_key: Some("id".into()),
indexes: None,
}).await?;
Ok(())
}