Connect
Connect to an app on Klave
Once your Klave app is ready and deployed, you will want to leverage it in your applications. Klave implements a Secure Connection Protocol (SCP) which provides validatable direct access to your applications.
You can refer to the protocol above to implement your own custom connectors. However, Klave provides ready-to-use TypeScript and Python connectors that can be utilized on both the server and client sides (including web browsers, IoT devices, etc...).
The basics
To start using Klave's TypeScript connector you will first need to install it from NPM
yarn add @secretarium/connector
npm install @secretarium/connector
pnpm add @secretarium/connector
On the server the @secretarium/connector package requires Node.js version 20+.
pip install secretarium-connector
On the server the secretarium_connector package requires Python version 3.11.0+.
cargo add klave-connector
On the server the klave-connector component requires Rust version 1.86.0+.
Once done, you will need to setup a few things to get running. You can see the example code right here:
import { SCP } from '@secretarium/connector';
async function main() {
// We create the new connector context
const context = new SCP();
// We create a new connection key or provide an existing one
const myKey = await Key.createKey()
// We start the connection to Klave
const connection = await context.connect('wss://on.klave.network', myKey)
}
main()
import asyncio
from secretarium_connector import SCP, Key
async def main():
#We create the new connector context
context = SCP()
#We create a new connection key or provide an existing one
myKey = await Key.createKey()
#We start the connection to Klave
connection = await context.connect('wss://on.klave.network', myKey)
asyncio.run(main())
use klave_connector::{SCP};
#[tokio::main]
async fn main() {
let mut client = SCP::new("wss://on.klave.network", None, None);
match client.connect().await {
Ok(_) => println!("Connected to Klave successfully."),
Err(e) => {
eprintln!("Failed to connect to Klave: {}", e);
return;
}
};
match client.close().await {
Ok(_) => println!("Connection closed successfully."),
Err(e) => eprintln!("Failed to close connection: {}", e),
};
}
From there calling your functions is very easy. Take a look at the example below using the storeValue
and fetchValue
methods defined in the hello_world app in the Create section:
import { SCP, Key } from '@secretarium/connector';
type MyValue = {
success: boolean;
value: string;
}
async function main() {
const context = new SCP();
const myKey = await Key.createKey()
const connection = await context.connect('wss://on.klave.network', myKey)
// We reference the name of our application deployment
const myAppId = 'your_app.on.klave.network';
// We load data in our application ledger with a transaction
await connection.newTx(myAppId, 'storeValue', { key: 'myKey', value: 'myValue' }).send()
// We retrieve the data with a query
const result = await connection.newQuery<MyValue>(myAppId, 'fetchValue', { key: 'myKey' }).send()
// We display the data we retreive and see it matches
console.log(result); // { "success": true, "value": "myValue" }
}
main()
import asyncio
from secretarium_connector import SCP, Key
async def main():
context = SCP()
myKey = await Key.createKey()
connection = await context.connect('wss://on.klave.network', myKey)
#We reference the name of our application deployment
myAppId = 'your_app.on.klave.network'
#Create object to store
keyValue1 = {'key':'key1', 'value':'myValue1'}
#We store the data in our application ledger with the transaction storeValue defined in the application
transactionResults = connection.newTx(myAppId, 'storeValue', 'requestId1', keyValue1)
await transactionResults.send()
#We retrieve the data with the query fetchValue defined in the application
queryResults = connection.newTx(myAppId, 'fetchValue', 'requestId2', {'key':'key1'})
#We define a callback function to print the result of the query
queryResults.onResult(lambda message: print(f"Query result: {message}"))
await queryResults.send()
asyncio.run(main())
#[tokio::main]
async fn main() {
let mut client = SCP::new("wss://on.klave.network", None, None);
match client.connect().await {
Ok(_) => println!("Connected to Klave successfully."),
Err(e) => {
eprintln!("Failed to connect to Klave: {}", e);
return;
}
};
// Send a transaction
let mut tx = client.new_tx("<your-app>", "version", None, None).await;
tx.on_error(|request_id, error_message| {
eprintln!("Transaction error occurred. RequestId: {:?}, Error: {:?}", request_id, error_message);
});
tx.on_result(|request_id, result| {
println!("Result received. RequestId: {:?}, Result: {:?}", request_id, result);
});
let _ = tx.send().await;
match client.close().await {
Ok(_) => println!("Connection closed successfully."),
Err(e) => eprintln!("Failed to close connection: {}", e),
};
}
Where your_app.on.klave.network
is the name of your application deployment as per seen in the Klave dashboard.
Last updated on