CalmLens Rust SDK
The Rust SDK for CalmLens moderation, storage and CDN delivery: an async client (Tokio and reqwest), an optional blocking client, typed models, streamed uploads and downloads, retries with idempotency keys, webhook verification and an offline mock. It requires Rust 1.88 or later.
Installation
| Feature | Default | Adds |
|---|---|---|
async |
yes | The async calmlens::ServerClient |
rustls |
yes | TLS through rustls, verifying certificates with the operating system's store |
native-tls |
TLS through the platform library | |
blocking |
calmlens::blocking::ServerClient, the same API without async |
|
tracing |
A tracing event, with the target calmlens, for every HTTP attempt |
|
testing |
calmlens::testing: the offline mock and test input generators, for [dev-dependencies] or a smoke-test binary |
|
strict-contract |
For the SDK's own contract tests only; don't enable it in applications |
With default-features = false the crate contains only the models and the webhook helpers, with no HTTP stack, for services that only receive webhooks.
Versions follow semantic versioning: see Versioning and support. The minimum supported Rust version is 1.88; a minor release may raise it to a version released at least six months earlier, and a patch release never does.
Clients
| Client | Use it for |
|---|---|
calmlens::ServerClient |
Async servers. Create one at startup and share it: clones share connections |
calmlens::blocking::ServerClient |
Synchronous code; never call it directly from async code |
calmlens::testing::MockServerClient |
Offline tests, with queued responses or an in-memory CalmLens |
Quick start
Create an API key and copy your project ID as described in Authentication, and keep them on your server.
use ;
async
assets().classify and text().classify wait for the decision; assets().create queues processing, and assets().wait_for_decision or a webhook reports the result. wait_for_decision returns once the asset is settled: its classification.status is completed, with the decision in classification.moderation.decision, or failed, with the reason in classification.failure_reason. While it is queued or processing it keeps waiting. The decision decides delivery:
| Decision | Meaning |
|---|---|
None |
Not decided yet; don't show the content |
allowed |
Show it |
limited |
Delivered like allowed, but reduce its reach: keep it out of recommendations and rank it lower |
age-restricted |
Show it only behind your own age check; the CDN requires ViewerClaims::new().age_confirmed(true) |
blocked |
Not delivered; the content is kept |
purged |
Not delivered, and the stored content is deleted |
Treat a missing or unknown decision as not allowed:
let publishable = matches!;
Options types are #[non_exhaustive]: build them with new, which takes the required fields, and the chained setters (clear_* sends null), so a minor release can add an option. Enums keep values this SDK version doesn't list as Unknown; to act on one, compare as_str() in a _ arm rather than matching Unknown, which stops matching once a later version lists the value.
What you can do
- Classify text and files, and read the decision: Rust SDK, Reading classification results
- Wait for queued decisions: Waiting for a decision
- Stream uploads from bytes, readers, streams and files, with content over 8 MiB sent in resumable parts automatically: Uploads, Multipart uploads
- Turn on OCR and other optional analyses: Processing features
- Moderate replies with their conversation: Conversations
- Declare a project's configuration in code, sending each
revisionyou read to refuse a change made since:upload_presets()(list,iterate,ensureby key,delete),project_settings()(get,update, which replaces the settings) andmetadata_fields()(list,ensureby ID,delete): Upload presets - Sign upload tokens for browser uploads through a preset: Upload tokens, Upload presets
- Manage assets, folders and batches, and record reviewer decisions: Methods, Reviewer decisions, Batch operations
- Build asset URLs with lenses and transforms, sign private assets' URLs, read their display info and stream content: CDN URLs, Delivering assets
- Block known images with hash lists: Hash lists
- Page through assets and poll the events feed: Pagination, Events
- Verify webhooks with axum or actix-web: Webhooks, Webhook helpers
- Configure timeouts, retries, a proxy, extra root certificates and your app's name, and override them per call: Client configuration, Retries and idempotency
- Observe every HTTP attempt with a hook,
tracingorCALMLENS_LOG, read each result'slast_response(), and call endpoints without a method throughrequest: Client configuration - Handle errors by kind or code: Errors and retries, Error details
Testing
calmlens::testing::MockServerClient (feature testing) runs your code offline against queued responses or an in-memory CalmLens that simulates moderation outcomes, events and CDN delivery. Against a real project, generate_test_text_code, paint_test_image and generate_media_test_code build signed test inputs, with your project's test input key, that CalmLens decides as a chosen scenario. See Rust testing.