Skip to main content

TypeScript SDK

The official TypeScript/JavaScript client for the Legend Prime API.

Installation

npm install legend-prime
For signing transactions, you’ll also need viem:
npm install viem

Quick start

import { LegendPrime } from "legend-prime";

const client = new LegendPrime({
  queryKey: "qk_abc123_8f2e9a7b4c1d6e3f5a0b",
});

// Create a sub-account
const account = await client.accounts.create({
  signerType: "eoa",
  signerAddress: "0x742d35Cc6634C0532925a3b844Bc9e7595f2bD18",
});

// List all accounts
const { accounts } = await client.accounts.list();

// Get portfolio
const folio = await client.accounts.folio(account.external_id);

Client configuration

const client = new LegendPrime({
  queryKey: "qk_...",              // Required: your query key
  baseUrl: "https://custom.url",   // Optional: override the API base URL
});

Accounts

// Create
const account = await client.accounts.create({
  signerType: "eoa",
  signerAddress: "0x...",
});

// List all
const { accounts } = await client.accounts.list();

// Get one
const account = await client.accounts.get("acc_2o0yiljtp378");

// Get portfolio
const folio = await client.accounts.folio("acc_2o0yiljtp378");
const cachedFolio = await client.accounts.folio("acc_2o0yiljtp378", { cached: true });

// Activities
const { activities } = await client.accounts.activities("acc_2o0yiljtp378");
const activity = await client.accounts.activity("acc_2o0yiljtp378", 1);

// Events (with long-polling)
const { events, cursor } = await client.accounts.events("acc_2o0yiljtp378", {
  since: 0,
  poll: true,
});

Plans

// Create plans
const earnPlan = await client.plan.earn("acc_2o0yiljtp378", {
  amount: "1000000",
  asset: "USDC",
  network: "base",
  protocol: "compound",
});

const withdrawPlan = await client.plan.withdraw("acc_2o0yiljtp378", {
  amount: "500000",
  asset: "USDC",
  network: "base",
  protocol: "compound",
});

const transferPlan = await client.plan.transfer("acc_2o0yiljtp378", {
  amount: "1000000",
  asset: "USDC",
  network: "base",
  recipient: "0x...",
});

// Sign and execute
import { privateKeyToAccount } from "viem/accounts";

const signer = privateKeyToAccount(privateKey);
const signature = await signer.sign({
  hash: earnPlan.chart.eip712_data.digest,
});

const result = await client.plan.execute("acc_2o0yiljtp378", {
  planId: earnPlan.plan_id,
  signature,
});

Reference data

// Get your Prime Account info
const me = await client.primeAccount();

// List supported networks
const { networks } = await client.networks();

// List supported assets
const { assets } = await client.assets();

Error handling

import { LegendPrime, LegendPrimeError } from "legend-prime";

try {
  await client.accounts.get("acc_nonexistent");
} catch (error) {
  if (error instanceof LegendPrimeError) {
    console.log(error.code);    // "account_not_found"
    console.log(error.message); // "Account not found"
    console.log(error.status);  // 404
  }
}

TypeScript types

All methods return typed responses. Key types:
interface AccountInfo {
  external_id: string;
  signer_type: string;
  signer_address: string;
  legend_wallet_address: string;
  created_at: string;
}

interface PlanResult {
  plan_id: string;
  chart: {
    quark_operation_actions: any[];
    eip712_data: {
      digest: string;
      domain_separator: string;
      hash_struct: string;
    };
  };
  expires_at: string;
}

interface ExecuteResult {
  plan_id: string;
  quark_intent_id: number;
  status: string;
}