3 releases (breaking)

0.3.0 Dec 31, 2023
0.2.0 Sep 18, 2023
0.1.0 Aug 27, 2023

#309 in Asynchronous

Download history 13/week @ 2024-02-18 38/week @ 2024-02-25 4/week @ 2024-03-10 44/week @ 2024-03-31 53/week @ 2024-04-14

97 downloads per month

MIT license

82KB
1K SLoC

Unkey for Rust

An asynchronous Rust SDK for the Unkey API.

All the API key management features you love, now with more type safety!

MSRV

The minimum supported Rust verision for the project is 1.63.0.

Documentation

Full documentation can be found at https://docs.rs/unkey.

Setup

Using cargo

$ cargo add unkey

Manually

Add the following to your Cargo.toml dependencies array:

unkey = "0.3" # I won't forget to update this™

Examples

Verifying a key

use unkey::models::{VerifyKeyRequest, Wrapped};
use unkey::Client;

async fn verify_key() {
    let c = Client::new("unkey_ABC");
    let req = VerifyKeyRequest::new("test_DEF", "api_JJJ");

    match c.verify_key(req).await {
        Wrapped::Ok(res) => println!("{res:?}"),
        Wrapped::Err(err) => eprintln!("{err:?}"),
    }
}

Creating a key

use unkey::models::{CreateKeyRequest, Wrapped};
use unkey::Client;

async fn create_key() {
    let c = Client::new("unkey_ABC");
    let req = CreateKeyRequest::new("api_123")
        .set_prefix("test")
        .set_remaining(100)
        .set_name("test_name")
        .set_owner_id("jonxslays");

    match c.create_key(req).await {
        Wrapped::Ok(res) => println!("{res:?}"),
        Wrapped::Err(err) => eprintln!("{err:?}"),
    }
}

Updating a key

use unkey::models::{Refill, RefillInterval, UpdateKeyRequest, Wrapped};
use unkey::Client;

async fn update_key() {
    let c = Client::new("unkey_ABC");
    let req = UpdateKeyRequest::new("key_XYZ")
        .set_name(Some("new_name")) // Update the keys name
        .set_ratelimit(None) // Remove any ratelimit on the key
        .set_expires(None) // Remove any expiration date
        .set_refill(Some(Refill::new(100, RefillInterval::Daily)));

    match c.update_key(req).await {
        Wrapped::Ok(res) => println!("{res:?}"),
        Wrapped::Err(err) => eprintln!("{err:?}"),
    }
}

Revoking a key

use unkey::models::{RevokeKeyRequest, Wrapped};
use unkey::Client;

async fn revoke_key() {
    let c = Client::new("unkey_ABC");
    let req = RevokeKeyRequest::new("key_XYZ");

    match c.revoke_key(req).await {
        Wrapped::Ok(res) => println!("{res:?}"),
        Wrapped::Err(err) => eprintln!("{err:?}"),
    }
}

Listing api keys

use unkey::models::{ListKeysRequest, Wrapped};
use unkey::Client;

async fn list_keys() {
    let c = Client::new("unkey_ABC");
    let req = ListKeysRequest::new("api_123");

    match c.list_keys(req).await {
        Wrapped::Ok(res) => println!("{res:?}"),
        Wrapped::Err(err) => eprintln!("{err:?}"),
    }
}

Getting api information

use unkey::models::{GetApiRequest, Wrapped};
use unkey::Client;

async fn get_api() {
    let c = Client::new("unkey_ABC");
    let req = GetApiRequest::new("api_123");

    match c.get_api(req).await {
        Wrapped::Ok(res) => println!("{res:?}"),
        Wrapped::Err(err) => eprintln!("{err:?}"),
    }
}

Getting key details

use unkey::models::{GetKeyRequest, Wrapped};
use unkey::Client;

async fn get_key() {
    let c = Client::new("unkey_ABC");
    let req = GetKeyRequest::new("key_123");

    match c.get_key(req).await {
        Wrapped::Ok(res) => println!("{res:?}"),
        Wrapped::Err(err) => eprintln!("{err:?}"),
    }
}

Update remaining verifications

use unkey::models::{UpdateOp, UpdateRemainingRequest, Wrapped};
use unkey::Client;

async fn update_remaining() {
    let c = Client::new("unkey_ABC");
    let req = UpdateRemainingRequest::new("key_123", Some(100), UpdateOp::Set);

    match c.update_remaining(req).await {
        Wrapped::Ok(res) => println!("{res:?}"),
        Wrapped::Err(err) => eprintln!("{err:?}"),
    }
}

Contributions

Unkey for Rust is open to contributions! Check out the contributing guide to get started.

License

Unkey for Rust is licensed under the MIT License.

Dependencies

~4–19MB
~252K SLoC