CLI Commands

K2 exposes 36 Tauri commands that bridge the React frontend to the Rust P2P core. These commands are organized into logical groups based on their functionality.
All commands are defined in k2-app-tauri/src-tauri/src/lib.rs and invoked from the frontend via @tauri-apps/api.

Node Management

Commands for initializing and managing the local P2P node.

init_node

Initialize the K2Node with persistent storage. This command is idempotent - if a node already exists, it returns the existing node ID. Parameters:
NameTypeDescription
appAppHandleTauri application handle (injected by runtime)
Returns: String - Short node ID (first 8 characters of the public key) Example:
import { invoke } from '@tauri-apps/api/core';

const nodeId = await invoke('init_node');
console.log('Node initialized:', nodeId);

get_my_node_id

Retrieve the full node public key as a hex string for sharing with other peers. Parameters: None Returns: String - Full NodeId in hexadecimal format Example:
const fullNodeId = await invoke('get_my_node_id');
// e.g., "71853750efc1219d7976639087c5fb25cf8d4b49f6d509366f2e094a3f781623"

Profile Management

Commands for managing the user’s public profile.

get_profile

Retrieve the current user’s profile information. Returns: Profile object containing:
  • name: Display name
  • intro: Short introduction
  • description: Detailed description
  • avatar_hash: BLAKE3 hash of avatar image
  • logo_dark_hash: Hash of dark mode logo
  • logo_light_hash: Hash of light mode logo
Example:
const profile = await invoke('get_profile');
console.log(profile.name);

get_profile_image

Fetch a profile image by its BLAKE3 hash. Parameters:
NameTypeDescription
hashStringBLAKE3 hash of the image
Returns: String - Base64-encoded PNG data URL Example:
const imageDataUrl = await invoke('get_profile_image', { hash: profile.avatar_hash });
// "data:image/png;base64,iVBORw0KGgo..."

update_profile_text

Update the text fields of the user profile. Supports partial updates. Parameters:
NameTypeRequiredDescription
nameStringNoNew display name
introStringNoNew short introduction
descriptionStringNoNew detailed description
Returns: () - Success indicator Example:
await invoke('update_profile_text', {
  name: 'Alice',
  intro: 'Software engineer & seller'
});

update_profile_image

Upload and store a profile image (avatar or logo). Parameters:
NameTypeDescription
fieldStringOne of: "avatar", "logo_dark", "logo_light"
bytesVec<u8>Raw image bytes (PNG or JPEG)
Returns: String - BLAKE3 hash of the stored image Example:
const imageBytes = await fetch(file).then(r => r.arrayBuffer());
const hash = await invoke('update_profile_image', {
  field: 'avatar',
  bytes: Array.from(new Uint8Array(imageBytes))
});

Contact Book

Commands for managing P2P contacts via iroh-docs synchronization.

add_contact

Add a new contact to the synchronized contact book. Parameters:
NameTypeRequiredDescription
node_idStringYesFull NodeId in hex format
nicknameStringYesDisplay name for the contact
notesStringNoOptional notes
Returns: () - Success indicator Example:
await invoke('add_contact', {
  node_id: '71853750efc1219d...',
  nickname: 'Bob',
  notes: 'Laptop seller from #laptops topic'
});

remove_contact

Remove a contact by their NodeId. Parameters:
NameTypeDescription
node_idStringFull NodeId in hex format
Example:
await invoke('remove_contact', { node_id: '71853750efc1219d...' });

update_contact_nickname

Update a contact’s nickname. Parameters:
NameTypeDescription
node_idStringFull NodeId in hex format
nicknameStringNew nickname
Example:
await invoke('update_contact_nickname', {
  node_id: '71853750efc1219d...',
  nickname: 'Bob the Builder'
});

list_contacts

List all contacts from the iroh-docs contact book. Returns: Vec<Contact> - Array of contact objects Example:
const contacts = await invoke('list_contacts');
contacts.forEach(c => console.log(c.nickname, c.node_id));

ping_contact

Check if a contact is online with a 10-second timeout. Parameters:
NameTypeDescription
node_idStringFull NodeId in hex format
Returns: bool - true if contact responds within timeout Example:
const isOnline = await invoke('ping_contact', { node_id: '71853750efc1219d...' });
console.log('Contact is', isOnline ? 'online' : 'offline');

Chat / Direct Messages

Commands for P2P direct messaging via iroh-gossip.

send_chat_message

Send a direct message to a contact via a gossip topic derived from both NodeIds. Parameters:
NameTypeDescription
recipient_node_idStringFull NodeId of the recipient
contentStringMessage text
Returns: () - Success indicator Example:
await invoke('send_chat_message', {
  recipient_node_id: '71853750efc1219d...',
  content: 'Hey, is the laptop still available?'
});

start_dm_listener

Subscribe to direct message topics for a contact and emit frontend events when messages arrive. Parameters:
NameTypeDescription
contact_node_idStringFull NodeId of the contact to listen for
Events Emitted:
  • k2://chat-message - { sender, content, timestamp }
Example:
import { listen } from '@tauri-apps/api/event';

// Start listening
await invoke('start_dm_listener', { contact_node_id: '71853750efc1219d...' });

// Handle incoming messages
listen('k2://chat-message', (event) => {
  console.log('New message:', event.payload);
});

File Sharing

Commands for sharing and downloading files via iroh-blobs.

share_file

Add a file to the blob store and generate a shareable ticket. Parameters:
NameTypeDescription
pathStringAbsolute file path
Returns: String - Ticket in format filename|ticket_string Example:
const ticket = await invoke('share_file', { path: '/home/user/photo.jpg' });
// "photo.jpg|blobticket..."

share_bytes

Share raw bytes directly (useful for Android content:// URIs). Parameters:
NameTypeDescription
bytesVec<u8>Raw file bytes
filenameStringOriginal filename
Returns: String - Ticket string Example:
const ticket = await invoke('share_bytes', {
  bytes: Array.from(imageBytes),
  filename: 'screenshot.png'
});

download_file

Download a file from a blob ticket and save it to the Downloads folder. Parameters:
NameTypeDescription
ticketStringFull blob ticket string
Returns: String - Full path to the downloaded file Example:
const savePath = await invoke('download_file', { ticket: 'blobticket...' });
console.log('File saved to:', savePath);

generate_qr_svg

Generate a QR code as an SVG string for sharing tickets or NodeIds. Parameters:
NameTypeDescription
dataStringData to encode in the QR code
Returns: String - SVG XML string Example:
const svg = await invoke('generate_qr_svg', { data: ticket });
// Insert into DOM: <div dangerouslySetInnerHTML={{ __html: svg }} />

Marketplace

Commands for the AI-powered decentralized marketplace.

get_broadcast_delay

Get a random delay between 1-4 seconds for gossip message randomization. Returns: u64 - Delay in seconds Example:
const delay = await invoke('get_broadcast_delay');
await new Promise(r => setTimeout(r, delay * 1000));

join_topic

Subscribe to a marketplace topic with tracker-based peer discovery. Parameters:
NameTypeDescription
topicStringTopic name (e.g., “laptops”, “services”)
actionStringUser action: “buying” or “selling”
Returns: () - Success indicator Example:
await invoke('join_topic', { topic: 'laptops', action: 'buying' });

classify_intent

Classify user intent using the Groq API (backend proxy to avoid CORS). Parameters:
NameTypeRequiredDescription
user_promptStringYesNatural language query
api_keyStringYesGroq API key
base_urlStringNoCustom base URL (defaults to Groq)
modelStringNoModel name (defaults to config)
Returns: JsonValue - Structured intent JSON Example:
const intent = await invoke('classify_intent', {
  user_prompt: 'I want to buy a gaming laptop under $1000',
  api_key: import.meta.env.VITE_GROQ_API_KEY
});
// { topic: "laptops", action: "buying", filters: { price_max: 1000, category: "gaming" } }

classify_k2_endpoint

Alternative intent classification via a K2 backend endpoint. Parameters:
NameTypeDescription
user_promptStringNatural language query
Returns: JsonValue - Structured intent JSON

broadcast_offer

Broadcast an offer or request to a topic. Parameters:
NameTypeDescription
topicStringTopic name
form_dataJsonValueOffer/request data
Returns: () - Success indicator Example:
await invoke('broadcast_offer', {
  topic: 'laptops',
  form_data: {
    title: 'Gaming Laptop',
    price: 950,
    currency: 'USD',
    description: 'RTX 4060, 16GB RAM'
  }
});

send_interest

Send an interest message to a specific seller. Parameters:
NameTypeDescription
topicStringTopic name
seller_node_idStringSeller’s NodeId
form_dataJsonValueInterest details
Returns: () - Success indicator

listen_offers

Blocking poll for offers on a topic (returns after timeout or when offers are found). Parameters:
NameTypeDescription
topicStringTopic name
timeout_secsu64Maximum wait time
Returns: Vec<Offer> - Array of received offers Example:
const offers = await invoke('listen_offers', { topic: 'laptops', timeout_secs: 30 });

start_listening

Start a real-time listener for marketplace events on a topic. Parameters:
NameTypeDescription
topicStringTopic name
Events Emitted:
  • k2://offer-received - New offer broadcast
  • k2://peer-connected - Peer joined the topic
  • k2://peer-disconnected - Peer left the topic
Example:
await invoke('start_listening', { topic: 'laptops' });

listen('k2://offer-received', (event) => {
  console.log('New offer:', event.payload);
});

Sync

Commands for Syncthing-style folder synchronization via iroh-docs.

get_sync_folders

List all configured sync folders with their status. Returns: Vec<SyncFolder> - Array of folder configurations Example:
const folders = await invoke('get_sync_folders');

add_sync_folder

Add a new folder to synchronize. Parameters:
NameTypeDescription
configSyncFolderConfigFolder configuration object
Returns: String - Generated folder ID Example:
const folderId = await invoke('add_sync_folder', {
  config: {
    local_path: '/home/user/Documents',
    label: 'Documents'
  }
});

remove_sync_folder

Remove a sync folder (soft delete with tombstone). Parameters:
NameTypeDescription
idStringFolder ID

get_sync_devices

List all paired sync devices with online status. Returns: Vec<SyncDevice> - Array of device information

add_sync_device

Add a device to the sync network. Parameters:
NameTypeDescription
configSyncDeviceConfigDevice configuration

remove_sync_device

Remove a device from the sync network. Parameters:
NameTypeDescription
idStringDevice ID

test_sync_device

Check if a sync device is online (10-second timeout). Parameters:
NameTypeDescription
node_idStringDevice NodeId
Returns: bool - Online status

get_sync_settings

Get global sync settings. Returns: SyncSettings - Current settings object

update_sync_settings

Update global sync settings. Parameters:
NameTypeDescription
settingsSyncSettingsNew settings object

sync_now

Trigger an immediate sync for a specific folder. Parameters:
NameTypeDescription
idStringFolder ID

accept_sync_folder

Accept an incoming folder sync invitation. Parameters:
NameTypeDescription
folder_idStringFolder ID from invitation
local_pathStringLocal path to sync to
Returns: () - Success indicator

Command Summary Table

CommandCategoryDescription
init_nodeNodeInitialize P2P node
get_my_node_idNodeGet full NodeId
get_profileProfileRetrieve user profile
get_profile_imageProfileGet profile image by hash
update_profile_textProfileUpdate profile text fields
update_profile_imageProfileUpload profile image
add_contactContactsAdd contact to address book
remove_contactContactsRemove contact
update_contact_nicknameContactsUpdate contact nickname
list_contactsContactsList all contacts
ping_contactContactsCheck contact online status
send_chat_messageChatSend direct message
start_dm_listenerChatStart DM event listener
share_fileFilesShare file by path
share_bytesFilesShare raw bytes
download_fileFilesDownload from ticket
generate_qr_svgFilesGenerate QR code SVG
get_broadcast_delayMarketplaceGet random gossip delay
join_topicMarketplaceSubscribe to topic
classify_intentMarketplaceAI intent classification
classify_k2_endpointMarketplaceK2 backend classification
broadcast_offerMarketplaceBroadcast offer to topic
send_interestMarketplaceSend interest to seller
listen_offersMarketplacePoll for offers
start_listeningMarketplaceStart real-time listener
get_sync_foldersSyncList sync folders
add_sync_folderSyncAdd sync folder
remove_sync_folderSyncRemove sync folder
get_sync_devicesSyncList paired devices
add_sync_deviceSyncAdd sync device
remove_sync_deviceSyncRemove sync device
test_sync_deviceSyncTest device connectivity
get_sync_settingsSyncGet sync settings
update_sync_settingsSyncUpdate sync settings
sync_nowSyncTrigger immediate sync
accept_sync_folderSyncAccept folder invitation