Using koban as a library
The koban crate is a standalone Invoice Ninja API client that other Rust applications can depend on, independent of the CLI:
sh
cargo add kobanQuickstart
rust
use koban::{ApiClient, Config};
#[tokio::main]
async fn main() -> koban::Result<()> {
// Reads INVOICE_NINJA_API_TOKEN and optional INVOICE_NINJA_BASE_URL.
let client = ApiClient::new(Config::from_env()?);
// Typed resource accessors return the built-in models.
for invoice in client.invoices().list().await? {
println!("{} -> {}", invoice.number, invoice.balance);
}
// Or work with any resource and your own type / serde_json::Value.
let client_record = client.clients().get("client_id").await?;
println!("{}", client_record.display_name);
Ok(())
}Configuration
Config reads from the environment with Config::from_env(), or you can build one directly. The relevant env constants are re-exported: API_TOKEN_ENV, BASE_URL_ENV, and DEFAULT_BASE_URL. See the Environment reference.
Three layers of API
koban gives you three ways to talk to Invoice Ninja, from highest to lowest level:
- Typed resource accessors —
client.invoices(),client.clients(), … — return typed models. See Resource accessors. - Generic typed methods —
client.resource::<T>(Resource::TaxRates)works for anyT: DeserializeOwned, including your own types orserde_json::Value. - Raw JSON escape hatch —
client.get_json(...),post_json,put_json, anddelete_jsonreturnserde_json::Valuefor non-resource endpoints or routes outside the generic CRUD shape.
What gets re-exported
lib.rs re-exports the full public surface:
ApiClientandConfig(+API_TOKEN_ENV,BASE_URL_ENV,DEFAULT_BASE_URL)Resource(the resource enum) andResources(the accessor handle)KobanErrorandResultredact(token redaction helper)- Models:
Invoice,InvoiceItem,Client,Contact,Payment,Quote,Credit,Product,Expense,Vendor,Project,Task, and the envelopesData,Paginated,Meta,Pagination.
By default the library depends only on thiserror for its error type; enable the miette feature for diagnostic help text on KobanError (see Errors & features).