agentgen · Rust SDK + CLI
Async Rust client and command-line tool for the AgentGen API — HTML → PDF and HTML → Image generation.
This directory is a Cargo workspace with two crates:
| Crate | Type | Description |
|---|---|---|
agentgen |
Library | Async Rust client — use in your own Rust projects |
agentgen-cli |
Binary | agentgen CLI — use from any shell or CI pipeline |
Library — agentgen
Adding to your project
# Cargo.toml
[]
= { = "./agentgen" } # local
# — or once published to crates.io —
= "0.1"
= { = "1", = ["full"] }
Quick start
use ;
async
Creating the client
// Production (default base URL: https://www.agent-gen.com/api)
let client = new;
// Custom base URL — useful for local development or testing
let client = with_base_url;
generate_image — Costs 1 token
Build a GenerateImageRequest with the provided builder methods and await the call.
use ;
let result = client
.generate_image
.await?;
println!;
println!;
println!;
println!;
println!;
GenerateImageRequest::new(html) is the only required call; all builder methods are optional.
generate_pdf — Costs 2 tokens per page
Pass a GeneratePdfRequest — either SinglePage(PdfPage) for one page, or MultiPage { pages: Vec<PdfPage> } for multiple pages.
Use generate_pdf_with_optimize(..., Some(false)) if you need the unoptimized Chromium PDF.
Single-page PDF
use ;
let result = client
.generate_pdf_with_optimize
.await?;
println!;
println!;
println!;
Shorthand for uniform margins
use PdfMargin;
// Sets top/bottom/left/right to the same value
let margin = all;
Multi-page PDF
let result = client
.generate_pdf_with_optimize
.await?;
println!;
Up to 100 pages per request.
upload_temp — Free
Upload a local file so you can reference it by URL inside your HTML. Auto-deleted after 24 hours.
use Path;
let upload = client.upload_temp.await?;
println!;
println!;
println!;
println!;
// Embed the URL in HTML and then generate
let image = client
.generate_image
.await?;
get_balance — Free
let balance = client.get_balance.await?;
println!;
Error handling
All methods return Result<T, AgentGenError>. The error type is defined in agentgen::AgentGenError:
use AgentGenError;
match client.generate_image.await
AgentGenError variants
| Variant | When | Fields |
|---|---|---|
Api { status, message, detail } |
Non-2xx API response (except 402) | HTTP status, error message, optional detail |
InsufficientTokens { balance, required, buy_more_url } |
HTTP 402 | Current balance, required tokens, buy URL |
Http(reqwest::Error) |
Network / transport failure | Wrapped reqwest error |
Io(std::io::Error) |
File read failure (upload_temp) | Wrapped I/O error |
Type reference
use ;
CLI — agentgen
The agentgen binary lets you use the API directly from a terminal, shell script, or CI pipeline without writing any code.
Building
# From the rust/ workspace root
# The binary is at:
Or install it system-wide:
Authentication
Provide your API key via the AGENTGEN_API_KEY environment variable (recommended) or the --api-key flag:
# or pass it inline
agentgen balance
Check your current token balance.
agentgen image
Render HTML to an image (PNG / JPEG / WebP). Costs 1 token.
agentgen image (--html <string> | --file <path>)
[--viewport-width <px>]
[--viewport-height <px>]
[--selector <css>]
[--format png|jpeg|webp]
[--scale <1-3>]
[--output <path>]
Inline HTML:
From a file:
Download the result locally:
Example output:
✓ Image generated
URL: https://…/output.png
Dimensions: 1200 × 630 px
Format: PNG
Tokens used: 1
Request ID: req_abc123
✓ Saved → og-image.png
Options:
| Flag | Default | Description |
|---|---|---|
--html <string> |
— | Inline HTML string to render |
--file <path> |
— | Read HTML from a file (mutually exclusive with --html) |
--viewport-width <px> |
1200 | Viewport width in pixels used for layout before capture |
--viewport-height <px> |
800 | Viewport height in pixels used for layout before capture |
--selector <css> |
— | Capture a specific element instead of the full rendered document |
--format |
png |
Output format: png, jpeg, or webp |
--scale <n> |
2 | Device pixel ratio (1–3) |
--output / -o <path> |
— | Download the generated image to this path |
agentgen pdf
Render HTML to a PDF. Costs 2 tokens per page.
agentgen pdf (--html <string> | --file <path> | --pages <file1> <file2> …)
[--page-size-source css|format]
[--format A4|Letter|A3|Legal]
[--landscape]
[--print-background]
[--no-optimize]
[--margin-top <value>] [--margin-bottom <value>]
[--margin-left <value>] [--margin-right <value>]
[--output <path>]
Single-page — inline HTML:
Single-page — from a file:
Multi-page — one HTML file per page:
Each file in --pages becomes one independent page. All pages share the same --format, --landscape, and margin settings.
Example output:
✓ PDF generated
URL: https://…/output.pdf
Pages: 3 page(s)
Tokens used: 6 token(s)
Request ID: req_def456
✓ Saved → report.pdf
Options:
| Flag | Default | Description |
|---|---|---|
--html <string> |
— | Inline HTML for a single-page PDF |
--file <path> |
— | Read HTML from a file (single-page) |
--pages <files…> |
— | Multiple HTML files — one per page |
--page-size-source |
css |
Prefer CSS @page size or fallback format |
--format |
A4 |
Fallback paper size: A4, Letter, A3, Legal |
--landscape |
off | Landscape orientation |
--print-background |
off | Render CSS backgrounds |
--no-optimize |
off | Disable PDF shrinking post-processing |
--margin-top <value> |
— | Top margin (CSS length, e.g. 20mm) |
--margin-bottom <value> |
— | Bottom margin |
--margin-left <value> |
— | Left margin |
--margin-right <value> |
— | Right margin |
--output / -o <path> |
— | Download the generated PDF to this path |
agentgen upload
Upload a local file to temporary storage. Free — no tokens consumed. Auto-deleted after 24 hours.
Example output:
✓ File uploaded
URL: https://…/logo.png
Key: tmp/…/logo.png
Size: 14832 bytes
Expires at: 2026-03-03T10:00:00.000Z
Use the printed URL inside your HTML before calling image or pdf.
Global flags
| Flag | Description |
|---|---|
--api-key <key> |
API key (or set AGENTGEN_API_KEY env var) |
--version |
Print version and exit |
--help |
Print help and exit |
Token pricing
| Operation | Cost |
|---|---|
| Generate image | 1 token |
| Generate PDF (per page) | 2 tokens |
| Upload temp file | Free |
| Check balance | Free |
Purchase tokens at agent-gen.com.