Skip to main content

Code Examples

Real-world patterns and use cases

Basic Store & Retrieve

Beginner

Simple key-value storage for user preferences

// Store user preferences
await tetto.callAgent(AGENT_ID, {
  action: 'store',
  key: 'user:prefs',
  value: JSON.stringify({
    theme: 'dark',
    language: 'en',
    notifications: true
  })
}, wallet);

// Retrieve preferences
const result = await tetto.callAgent(AGENT_ID, {
  action: 'retrieve',
  key: 'user:prefs'
}, wallet);

if (result.output.exists) {
  const prefs = JSON.parse(result.output.value);
  console.log(prefs.theme); // 'dark'
}

Hierarchical Keys

Intermediate

Organize data with colon-separated namespaces

// Store related data
await tetto.callAgent(AGENT_ID, {
  action: 'store',
  key: 'recipes:italian:carbonara',
  value: JSON.stringify({
    ingredients: ['eggs', 'bacon', 'pasta'],
    servings: 4
  })
}, wallet);

await tetto.callAgent(AGENT_ID, {
  action: 'store',
  key: 'recipes:italian:marinara',
  value: JSON.stringify({
    ingredients: ['tomatoes', 'garlic'],
    servings: 4
  })
}, wallet);

// List all Italian recipes
const list = await tetto.callAgent(AGENT_ID, {
  action: 'list',
  prefix: 'recipes:italian:'
}, wallet);

console.log(list.output.items);
// ['recipes:italian:carbonara', 'recipes:italian:marinara']

Temporal Context

Featured

Track when, where, and how you learned something

// Store with temporal context
await tetto.callAgent(AGENT_ID, {
  action: 'store',
  key: 'sarah:allergies',
  value: JSON.stringify({
    allergic_to: 'shellfish',
    severity: 'high'
  }),
  occurred_at: '2025-10-31T19:30:00Z',
  location: 'Downtown restaurant',
  learned_from: 'conversation'
}, wallet);

// Retrieve with temporal fields
const result = await tetto.callAgent(AGENT_ID, {
  action: 'retrieve',
  key: 'sarah:allergies'
}, wallet);

console.log(result.output.occurred_at);
// '2025-10-31T19:30:00Z'
console.log(result.output.location);
// 'Downtown restaurant'

// Build a timeline of what you learned
const memories = await buildTimeline();

Agent-to-Agent Calls

Advanced

WarmAnswers storing Q&A in user's namespace

// WarmAnswers agent handler
export const POST = createAgentHandler({
  async handler(input, context) {
    const tetto = TettoSDK.fromContext(context.tetto_context);
    const userWallet = context.tetto_context.caller_wallet;

    if (input.action === 'teach') {
      // Agent pays, stores in user's namespace
      await tetto.callAgent(WARMMEMORY_ID, {
        action: 'store',
        key: `qa:${Date.now()}`,
        value: JSON.stringify({
          question: input.question,
          answer: input.answer,
          created_at: new Date().toISOString()
        })
      }, operationalWallet, userWallet);

      return {
        success: true,
        cost: 0.011  // $0.01 + $0.001 WarmMemory
      };
    }
  }
});

Pagination

Intermediate

Retrieve all keys with cursor-based pagination

async function getAllKeys(prefix) {
  const allKeys = [];
  let cursor = '';

  while (true) {
    const result = await tetto.callAgent(AGENT_ID, {
      action: 'list',
      prefix,
      limit: 50,  // Max per request
      cursor: cursor || undefined
    }, wallet);

    if (!result.output.success) {
      throw new Error(result.output.error);
    }

    allKeys.push(...result.output.items);

    // Check if more pages
    if (!result.output.cursor) break;
    cursor = result.output.cursor;
  }

  return allKeys;
}

const allRecipes = await getAllKeys('recipes:');

Error Handling

Best Practice

Graceful degradation and retry logic

try {
  const result = await tetto.callAgent(AGENT_ID, {
    action: 'store',
    key: 'data',
    value: JSON.stringify({ value: 123 })
  }, wallet);

  if (!result.output.success) {
    const error = result.output.error;

    if (error.includes('Rate limit')) {
      // Wait and retry
      await sleep(60000);
      return retry();
    }

    if (error.includes('Quota exceeded')) {
      // Clean up old data
      await cleanupOldEntries();
      return retry();
    }

    throw new Error(error);
  }

  return { success: true };

} catch (error) {
  console.error('Transaction failed:', error);
  return { success: false };
}