Build

Rust SDK guide

Install the Rust SDK, compress a PDF in three lines, then run tools and pipelines, handle errors and verify webhooks.

Install

Rust package wellfriend-document-sdk, for Rust 1.80 or later, with tokio. Import it with use wellfriend::Wellfriend; What the registry serves now: 1.0.0 on crates.io. Add tokio to run it: cargo add tokio --features full.

Install the Rust SDK
cargo add wellfriend-document-sdk

Quickstart

Create a key in the dashboard (/dashboard/developer-api/keys) and set it as WELLFRIEND_API_KEY on your server; the client reads it, and https://wellfriend.dev/api/v2 is the default address.

Rust: compress a PDF to a file
let wf = Wellfriend::new(); // reads WELLFRIEND_API_KEY
let result = wf.tools().compress_pdf("input.pdf", None).await?;
result.save("output.pdf").await?;
The same job as a complete program
// cargo add wellfriend-document-sdk && cargo add tokio --features full
use wellfriend::{FileInput, Wellfriend};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Your API key: set WELLFRIEND_API_KEY, or replace YOUR_API_KEY (keys: https://wellfriend.dev/dashboard/developer-api/keys).
    let api_key = std::env::var("WELLFRIEND_API_KEY").unwrap_or_else(|_| "YOUR_API_KEY".into());
    let wf = Wellfriend::builder().api_key(api_key).build()?;
    wf.tools().compress_pdf(FileInput::from("input.pdf"), None).await?.save("input-wellfriend.pdf").await?;
    Ok(())
}

Tools

Every tool is one method, wf.tools().<tool>(input, params, options): it uploads the input (a path, bytes, a stream or a stored document; a list for merge), runs the tool, waits and returns the result. The result saves its first output with save(path), or every output into a folder; it also gives the bytes, a stream, the outputs, the operation and the usage. tools.run(toolId, ...) runs any tool by id, wait: false returns the started operation to wait on later, and quote(...) prices a run before you send it.

Settings, any tool by id, and not waiting
use wellfriend::types::CompressPdfParams;

let params: CompressPdfParams = serde_json::from_value(serde_json::json!({ "level": "strong" }))?;
let result = wf.tools().compress_pdf("input.pdf", Some(params)).await?;
println!("{:?} {:?}", result.outputs, result.usage);
wf.tools().run("merge-pdf", vec!["a.pdf", "b.pdf"], None).await?.save("merged.pdf").await?;

Pipelines

A pipeline runs up to ten tools on one file in one run; each step reads the previous step's output on the server. workflows.define(name, steps) keeps a named pipeline you run again like a tool. The SDK reuses one saved pipeline for the same steps instead of creating one per run.

Compress, then OCR
use wellfriend::Step;

let steps = vec![Step::new("compress-pdf"), Step::new("ocr-pdf")];
wf.pipeline(steps).run("input.pdf").await?.save("output.pdf").await?;

Errors

Every SDK raises the same errors. A message ends with the reference to quote to Support. Network errors, 408, 429 and 5xx answers are retried twice with backoff (honouring Retry-After) for reads and for writes the SDK sends with an idempotency key; set maxRetries and timeout on the client or per call.

The error types
WellfriendError
├─ APIError (status, code, message, referenceId, details)
│  ├─ BadRequestError (400) / ValidationError (422)
│  ├─ AuthenticationError (401) / PermissionDeniedError (403)
│  ├─ NotFoundError (404) / ConflictError (409)
│  ├─ RateLimitError (429, retryAfter)
│  └─ ServerError (5xx)
├─ ConnectionError
├─ TimeoutError
├─ OperationFailedError (code, message, operation)
└─ WaitTimeoutError (operation id, to resume)
Handle the common cases
match wf.tools().compress_pdf("input.pdf", None).await {
    Ok(result) => result.save("output.pdf").await?,
    Err(wellfriend::Error::Validation(error)) => eprintln!("{} {:?}", error.message, error.details),
    Err(wellfriend::Error::RateLimit(error)) => eprintln!("try again in {:?}", error.retry_after),
    Err(error) => return Err(error.into()),
}

Webhooks

webhooks.verify(payload, headers, secret), also available on its own as verifyWebhook, checks the signature over the raw body with a five-minute tolerance and returns the typed event; a bad signature raises WebhookVerificationError. Pass a list of secrets while you rotate one. See /docs/webhooks/signatures.

Verify a delivery
// Use the raw request body, exactly as received.
let secret = std::env::var("WELLFRIEND_WEBHOOK_SECRET")?;
let event = wellfriend::verify_webhook(&body, &headers, &secret)?;
println!("{event:?}");

Every other endpoint

Every API operation is a method named after it: document_versions.download, webhooks.endpoints.create, and so on. List methods return one page and can also go through every page. The API reference (/docs/developer-api/snippets) shows each one in this language.

Documents and webhooks
wf.document_versions().download("<documentId>", "<versionId>").await?.save("output.pdf").await?;
let endpoints = wf.webhooks().endpoints().list().await?;