Klave logo

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

On the server the @secretarium/connector package requires Node.js version 20+.

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()

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()

Where your_app.on.klave.network is the name of your application deployment as per seen in the Klave dashboard.

For a details view of the connector API, check out the Connector SDK section

On this page