Lago Client
A Rust client library for interacting with the Lago billing API.
Features
- Async/Await Support: Built with
tokioandreqwestfor modern async Rust - Automatic Retries: Configurable retry logic with exponential backoff
- Multiple Regions: Support for US, EU, and custom API endpoints
- Flexible Configuration: Environment variables or programmatic configuration
- Type Safety: Strongly typed requests and responses using
lago-types - Authentication: Secure API key-based authentication
Installation
Add this to your Cargo.toml:
[]
= "0.1.4"
Quick Start
Using Environment Variables
Set the required environment variables:
# or "eu" or custom URL
Then create a client:
use LagoClient;
async
Programmatic Configuration
use ;
use Duration;
async
Configuration
Regions
The client supports multiple regions:
Region::Us- United States (default)Region::Eu- European UnionRegion::Custom(url)- Custom API endpoint
Retry Configuration
Configure retry behavior for failed requests:
use ;
use Duration;
let retry_config = builder
.mode
.max_attempts
.initial_delay
.max_delay
.backoff_multiplier
.build;
let config = builder
.retry_config
.build;
Retry modes:
RetryMode::Off- No retriesRetryMode::Standard- Standard exponential backoffRetryMode::Adaptive- Adaptive retry behavior
API Operations
Invoices
use ;
// List invoices with optional filters
let request = new;
let invoices = client.list_invoices.await?;
// Get a specific invoice
let request = new;
let invoice = client.get_invoice.await?;
Invoice Preview
Preview an invoice before creating it:
use ;
// Preview for an existing customer with a new subscription
let preview_input = for_customer
.with_plan_code
.with_billing_time;
let request = new;
let preview = client.preview_invoice.await?;
println!;
// Preview with inline customer details
let customer = new
.with_name
.with_currency;
let preview_input = new
.with_plan_code
.with_subscription_at;
let request = new;
let preview = client.preview_invoice.await?;
// Preview with coupons
let coupon = new
.with_percentage;
let preview_input = for_customer
.with_plan_code
.with_coupons;
let request = new;
let preview = client.preview_invoice.await?;
// Preview for existing subscriptions with plan upgrade
let subscriptions = new
.with_plan_code;
let preview_input = for_customer
.with_subscriptions;
let request = new;
let preview = client.preview_invoice.await?;
Activity Logs
use ;
// List all activity logs
let activity_logs = client.list_activity_logs.await?;
// List activity logs with filters
let request = new.with_filters;
let filtered_logs = client.list_activity_logs.await?;
// Get a specific activity log by activity ID
let request = new;
let activity_log = client.get_activity_log.await?;
println!;
API Logs
use ;
// List all API logs
let api_logs = client.list_api_logs.await?;
// List API logs with filters
let request = new.with_filters;
let filtered_logs = client.list_api_logs.await?;
// Get a specific API log by request ID
let request = new;
let api_log = client.get_api_log.await?;
println!;
Billable Metrics
use ;
// Create a billable metric
let metric = new
.with_description
.with_field_name;
let request = new;
let created = client.create_billable_metric.await?;
// List billable metrics
let metrics = client.list_billable_metrics.await?;
// Get specific billable metric
let metric = client.get_billable_metric.await?;
Customers
use ;
// Create or Update a customer
let customer = new
.with_name
.with_email
.with_customer_type
.with_currency;
let request = new;
let created = client.create_customer.await?;
// List customers
let customers = client.list_customers.await?;
// Get specific customer
let customer = client.get_customer.await?;
Applied Coupons
use ;
// Apply a coupon to a customer
let apply_input = new;
let request = new;
let applied = client.apply_coupon.await?;
// Apply with a fixed amount discount
let apply_input = new
.with_fixed_amount; // $50.00 discount
let request = new;
let applied = client.apply_coupon.await?;
// Apply with a percentage discount
let apply_input = new
.with_percentage_rate; // 20% discount
let request = new;
let applied = client.apply_coupon.await?;
// List all applied coupons
let applied_coupons = client.list_applied_coupons.await?;
// List with filters
let request = new
.with_pagination
.with_filters;
let filtered = client.list_applied_coupons.await?;
Coupons
use ;
// Create a percentage-based coupon
let coupon = percentage
.with_reusable;
let request = new;
let created = client.create_coupon.await?;
// Create a fixed amount coupon
let coupon = fixed_amount
.with_frequency_duration;
let request = new;
let created = client.create_coupon.await?;
// List all coupons
let coupons = client.list_coupons.await?;
// List coupons with pagination
let request = new
.with_pagination;
let coupons = client.list_coupons.await?;
// Get a specific coupon
let request = new;
let coupon = client.get_coupon.await?;
// Update a coupon
let update_input = new
.with_name
.with_percentage_rate;
let request = new;
let updated = client.update_coupon.await?;
// Delete a coupon
let request = new;
let deleted = client.delete_coupon.await?;
Events
use ;
use json;
// Create a usage event for a customer
let event_input = for_customer
.with_properties
.with_timestamp;
let request = new;
let created = client.create_event.await?;
println!;
// Create a usage event for a subscription
let event_input = for_subscription
.with_properties
.with_precise_total_amount_cents;
let request = new;
let created = client.create_event.await?;
// Get a specific event by transaction ID
let request = new;
let event = client.get_event.await?;
println!;
Credit Notes
use ;
// List all credit notes
let credit_notes = client.list_credit_notes.await?;
// List credit notes with filters
let request = new
.with_pagination
.with_filters;
let filtered = client.list_credit_notes.await?;
// Get a specific credit note
let request = new;
let credit_note = client.get_credit_note.await?;
// Create a credit note
let items = vec!;
let input = new
.with_description;
let request = new;
let created = client.create_credit_note.await?;
// Update a credit note's refund status
let update_input = new
.with_refund_status;
let request = new;
let updated = client.update_credit_note.await?;
Error Handling
The client uses the lago-types error system:
use LagoError;
match client.list_invoices.await
Environment Variables
| Variable | Description | Default |
|---|---|---|
LAGO_API_KEY |
API key for authentication | Required |
LAGO_REGION |
API region (us, eu, or custom URL) |
us |
LAGO_API_URL |
Custom API endpoint URL | - |
Examples
See the examples/ directory for complete usage examples:
basic_usage.rs- Basic client usagecustom_configuration.rs- Advanced configuration optionsactivity_log.rs- Activity logs listing and filteringapi_log.rs- API logs listing and filteringbillable_metric.rs- Billable metrics managementcustomer.rs- Customers management operationsinvoice.rs- Invoice operations including previewapplied_coupon.rs- Applied coupons listing and filteringcoupon.rs- Coupon CRUD operationsevent.rs- Event creation and retrievalcredit_note.rs- Credit note operations
# Run the basic usage example
# Run the activity logs example
# Run the API logs example
# Run the billable metrics example
# Run the customer management example
# Run the invoice example
# Run the applied coupons example
# Run the coupons example
# Run the events example
# Run the credit notes example
Release
Before publishing a release
cargo check
cargo test
cargo doc --no-deps --open
cargo package
Run the release
cargo login API_KEY
cargo publish
License
This project is licensed under the same license as the parent Lago Rust Client.