Rust library
An async server-side client for the Voidhash API, built on Tokio and reqwest.
The voidhash crate is an async client for Rust backends, built on Tokio and reqwest. It needs a
secret key, so run it on your server only.
Install
The voidhash crate installs from a clone of the
Voidhash repository, as a path dependency:
git clone https://github.com/voidhashcom/voidhash.git third_party/voidhashcargo add --path third_party/voidhash/libraries/rustTo pin a build, check out a commit in the clone (git -C third_party/voidhash checkout <commit>), or
add the repository as a Git submodule so your repository records the commit.
The library is an alpha release and isn't on crates.io yet. Pin a commit and update regularly. See Versions and releases.
Requirements: a Tokio runtime. The client sends requests with reqwest. The optional axum
feature adds an axum route for provider endpoints.
Create a client
Create one client with your secret key and share it across your server, for example in an Arc:
use std::sync::Arc;
use voidhash::VoidhashClient;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let voidhash = Arc::new(
VoidhashClient::builder()
.secret_key(std::env::var("VOIDHASH_SECRET_KEY")?)
// Event capture is served from the API host for now.
.ingest_url("https://api.voidhash.com")
.build()?,
);
// Pass `voidhash` to your handlers, for example as axum state.
Ok(())
}VoidhashClient::builder() sets these options before build();
VoidhashClient::new(secret_key) uses the defaults:
| Builder method | Default | Description |
|---|---|---|
secret_key | — | Required. Sent as Authorization: Bearer <secret_key>. |
project_id | The key's project | Skips the project lookup. Concurrent first calls share one lookup. |
base_url | https://api.voidhash.com | The API origin. |
ingest_url | https://ingest.voidhash.com | The event capture origin. Use https://api.voidhash.com; the default host doesn't answer yet. |
publishable_key | — | Sent as the body token on event capture. Optional. |
header(name, value) | — | An extra header for every request. Call it again for more. |
build() makes no request. It returns voidhash::Error::Request when the secret key or
project_id is blank, or a header is invalid or sets authorization or x-secret-key.
Call the API
Each resource is a method on the client that returns a borrowed handle, such as
voidhash.persons() or voidhash.webhooks().endpoints(). Methods are async, take IDs as &str
and request bodies as generated types from voidhash::generated::types. They return
Result<T, voidhash::Error>.
let perks = voidhash.perks().list().await?;
for perk in &perks {
println!("{}", perk.slug);
}
let person = voidhash.persons().get("person_k2v9x4m8q1r3").await?;List methods return the first page only, newest first where the API orders that way. Person entitlements are the exception: they follow every page.
| Method | Covers |
|---|---|
persons() | Persons, attributes and access checks. |
perks(), products() | The catalog, and the perks each product grants. |
paywalls() | Paywall locations. |
webhooks() | Webhook endpoints and deliveries. |
event_capture() | Events. |
notifications() | Push notifications. |
payment_source(id) | Payment sources. |
api_keys(), schema() | The project's API keys and runtime schema. |
For an endpoint without a method here, call the REST API directly.
Handle errors
A failed API call returns voidhash::Error::Api, holding the API error.
Match on its type_ and code:
match voidhash.persons().get("person_k2v9x4m8q1r3").await {
Ok(person) => Ok(Some(person)),
Err(voidhash::Error::Api { type_, .. }) if type_ == "not_found" => Ok(None),
Err(voidhash::Error::Api { type_, .. })
if type_ == "unauthenticated" || type_ == "permission_denied" =>
{
Err("the Voidhash secret key is invalid or lacks access".into())
}
Err(error) => Err(error.into()),
}Error::Api also carries status, param and request_id. error.is_not_found() covers the common branch. Network failures are
Error::Transport, and configuration or decoding problems are Error::Request. The library
doesn't retry or cache.
Cargo features
| Feature | Adds |
|---|---|
axum | ProviderHandler::into_axum, to mount a provider endpoint as an axum route. |
Everything else, webhook verification included, needs no feature. Enable it with
cargo add voidhash --features axum.
Next steps
Something wrong or missing on this page? Tell us by email or on Discord.