Skip to content

appwrite-community/rust-function-examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Rust function examples

Three production-shaped Appwrite Functions written in Rust 1.83. They accompany the 3 things you can build with the Rust runtime blog post.

Function What it does Notable crates
rust-image-optimizer POST a base64-encoded image, get a resized AVIF, WebP, or JPEG back with size metadata. image, ravif, webp
rust-pdf-invoice POST a JSON invoice, get a base64-encoded PDF (A4, header, line items, total). printpdf, serde
rust-webhook-receiver Verify an X-Appwrite-Webhook-Signature HMAC-SHA1 in constant time before processing the payload. hmac, sha1, subtle

Each function is a self-contained Cargo crate with its own Cargo.toml and lib.rs, so you can pick the one you want and deploy it on its own.

Prerequisites

Deploy

  1. Clone the repo:
    git clone https://github.com/appwrite-community/rust-function-examples.git
    cd rust-function-examples
  2. Set the project ID in appwrite.config.json (replace <PROJECT_ID>).
  3. Push whichever functions you want:
    appwrite push functions --function-id rust-image-optimizer
    appwrite push functions --function-id rust-pdf-invoice
    appwrite push functions --function-id rust-webhook-receiver

Each push builds a release binary inside the runtime container and switches the function to the new deployment when the build finishes.

Try the functions

After deployment, every function has a public preview URL (https://<FUNCTION_ID>.<REGION>.appwrite.run/). Replace $URL with that domain in the examples below.

rust-image-optimizer

IMG=$(base64 -i input.png)
curl -X POST "$URL" \
  -H 'Content-Type: application/json' \
  -d "{\"image\":\"$IMG\",\"format\":\"webp\",\"width\":600}"

Response is JSON with format, width, height, originalBytes, optimizedBytes, and image (base64). Accepts format values avif, webp, jpeg.

rust-pdf-invoice

curl -X POST "$URL" \
  -H 'Content-Type: application/json' \
  -d '{
    "invoice_number": "INV-0001",
    "customer_name": "Acme Corp",
    "customer_email": "billing@acme.io",
    "items": [
      {"description": "Rust runtime executions", "quantity": 12000, "unit_price": 0.0001},
      {"description": "Function deployments", "quantity": 5, "unit_price": 10.0}
    ]
  }'

Response is JSON with invoice_number, bytes, and pdf (base64). Decode pdf to disk and open it:

echo "$PDF_BASE64" | base64 -d > invoice.pdf

rust-webhook-receiver

Set two function variables before invoking:

  • WEBHOOK_SIGNATURE_KEY: the same secret you set on the Appwrite webhook in Settings -> Webhooks.
  • WEBHOOK_URL: the public URL the webhook posts to. Appwrite signs URL + payload, so this must match exactly.

To smoke-test the verification locally with a self-computed signature:

SIG_KEY="your-shared-secret"
URL="https://your-function.appwrite.run/"
PAYLOAD='{"event":"users.user_abc.create"}'
SIG=$(printf "%s%s" "$URL" "$PAYLOAD" | openssl dgst -sha1 -hmac "$SIG_KEY" -binary | base64)

curl -X POST "$URL" \
  -H "Content-Type: application/json" \
  -H "X-Appwrite-Webhook-Signature: $SIG" \
  -H "X-Appwrite-Webhook-Events: users.user_abc.create" \
  -d "$PAYLOAD"

A valid signature returns 200 with {"ok": true, "event": "..."}. A mismatched signature returns 401.

A note on dependency pinning

Several transitive crates (for example, avif-serialize, moxcms, time-core, deranged) ship newer versions that require the Rust 2024 edition, which was only stabilized in Rust 1.85. The Appwrite Rust runtime is on 1.83, so those crates need to be pinned to their last edition-2021 release. Each function's Cargo.toml has the necessary pins.

License

MIT.

About

Three Rust function examples for Appwrite Functions: image optimizer, PDF invoice generator, HMAC-verified webhook receiver.

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages