Crates.io | get402 |
lib.rs | get402 |
version | 0.1.2 |
source | src |
created_at | 2022-02-17 16:21:17.905533 |
updated_at | 2022-02-17 22:30:19.834133 |
description | Client For Building Paid APIs Using Get402.com |
homepage | https://get402.com |
repository | https://github.com/ProofOfWorkCompany/get402-rust |
max_upload_size | |
id | 534217 |
size | 17,608 |
Build Paid APIs with Ease. This library simplifies offering of APIs that can be accessed only via micropayments
cargo install get402
See https://get402.com/docs for complete documentation
You may import the entire library or load only specific objects as needed
use Get402;
Your Get402 API is identified by a public/private key pair where the public address is used to identify your API and the private key is used to sign requests to get402.com.
let private_key = env::var("GET402_API_PRIVATE_KEY").unwrap();
let app: Get402::App = Get402::App::load(private_key);
let app: Get402::App = Get402::App::generate();
One you load your app using its private key there is no more work to do, all signing of requests is handled automatically by the library.
All clients start with a balance of zero credits available, which can be queried any time
let client: Get402::Client = app.create_client();
let client_identifier = env::var("GET402_API_CLIENT_IDENTIFIER").unwrap();
let client = app.get_client_from_identifier(&client_identifier);
let balance: u64 = client.get_balance().await.unwrap();
When a client uses your API you should charge their API key which reduces their available balance of credits.
let client = app.get_client_from_identifier();
let mut params = HashMap::new();
params.insert("credits", 1);
let response = client.charge_credit(¶ms);
If their balance of credits goes to zero you will receive an error including a PaymentRequired request with details on purchasing additional credits. If you do not want to receive an error here always check the balance first.
match client.charge_credit(¶ms).await {
Err(Get402::APIError::InsufficientFunds(payment_request)) => {
println!("Insufficient Funds!");
// payment_request includes outputs array, paymentUrl, and memo
}
Ok() => {
println!("Sufficient Credit For Call");
}
other => {
println!("Unexpected Error Occurred!");
}
}
To purchase additional credits simply request a new payment template for any number of credits. You will receive a standard payment request which wallets know how to fulfill.
let payment_request: PaymentRequired = client.request_buy_credits(10).await.unwrap();
Since client API keys are actually public/private key pairs capable of holding funds directly, this library provides a utility for purchasing new credits using the client private key directly. First you must load your client funds by sending satoshis to the client identifier address. Once funds arrive they will be available for purchasing credits.
Once payment is sent your client API key will immediately be credited with additional credits
cargo build
To run the tests you must set GET402_PRIVATE_KEY
environment variable either in the shell or a .env
file
cargo test