Skip to main content

TypeScript SDK

The official TypeScript/JavaScript client for the Legend 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.account_id);

Client configuration

const client = new LegendPrime({
  queryKey: "qk_...",              // Required: your query key
  baseUrl: "https://prime-api.legend.xyz",   // 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

Yield

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

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

// Claim protocol rewards
const claimPlan = await client.plan.claimRewards("acc_2o0yiljtp378", {
  asset: "COMP",
});

// Reinvest rewards (claim + swap + resupply)
const reinvestPlan = await client.plan.reinvestRewards("acc_2o0yiljtp378", {
  asset: "USDC",
  protocol: "compound",
  network: "base",
  rewardAssets: ["COMP"],
});

Swaps

// Swap with sell amount
const swapPlan = await client.plan.swap("acc_2o0yiljtp378", {
  sellAsset: "USDC",
  buyAsset: "WETH",
  network: "base",
  sellAmount: "10000000",
});

// Swap and supply to a protocol
const zapPlan = await client.plan.swapAndSupply("acc_2o0yiljtp378", {
  sellAsset: "WETH",
  sellAmount: "1000000000000000000",
  buyAsset: "USDC",
  protocol: "compound",
  network: "base",
});

Transfers

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

Borrowing

// Borrow
const borrowPlan = await client.plan.borrow("acc_2o0yiljtp378", {
  amount: "1000000",
  asset: "USDC",
  collateralAmount: "500000000000000000",
  collateralAsset: "WETH",
  network: "base",
  protocol: "compound",
});

// Repay
const repayPlan = await client.plan.repay("acc_2o0yiljtp378", {
  amount: "1000000",
  asset: "USDC",
  collateralAmount: "500000000000000000",
  collateralAsset: "WETH",
  network: "base",
  protocol: "compound",
});

Leverage

// Open/increase a leveraged long position
const loopPlan = await client.plan.loopLong("acc_2o0yiljtp378", {
  exposureAsset: "WETH",
  backingAsset: "USDC",
  marketId: "0xb323495f7e4148be5643a4ea4a8221eef163e4bccfdedc2a6f4696baacbc86cc",
  isIncrease: true,
  exposureAmount: "1000000000000000000",
  maxSwapBackingAmount: "5000000000",
  maxProvidedBackingAmount: "1000000000",
  poolFee: 500,
  network: "base",
});

// Close/reduce a leveraged long position
const unloopPlan = await client.plan.unloopLong("acc_2o0yiljtp378", {
  exposureAsset: "WETH",
  backingAsset: "USDC",
  marketId: "0xb323495f7e4148be5643a4ea4a8221eef163e4bccfdedc2a6f4696baacbc86cc",
  exposureAmount: "1000000000000000000",
  backingAmountToExit: "500000000",
  minSwapBackingAmount: "490000000",
  poolFee: 500,
  network: "base",
});

// Add collateral to a position
const addBackingPlan = await client.plan.addBacking("acc_2o0yiljtp378", {
  exposureAsset: "WETH",
  backingAsset: "USDC",
  marketId: "0xb323495f7e4148be5643a4ea4a8221eef163e4bccfdedc2a6f4696baacbc86cc",
  amount: "1000000000",
  isShort: false,
  network: "base",
});

// Remove collateral from a position
const withdrawBackingPlan = await client.plan.withdrawBacking("acc_2o0yiljtp378", {
  exposureAsset: "WETH",
  backingAsset: "USDC",
  marketId: "0xb323495f7e4148be5643a4ea4a8221eef163e4bccfdedc2a6f4696baacbc86cc",
  amount: "500000000",
  isShort: false,
  network: "base",
});

Migration

// Migrate a supply position between protocols
const migratePlan = await client.plan.migrate("acc_2o0yiljtp378", {
  amount: "1000000",
  asset: "USDC",
  fromProtocol: "compound",
  toProtocol: "aave",
  network: "base",
});

Sign and execute

import { privateKeyToAccount } from "viem/accounts";

const signer = privateKeyToAccount(privateKey);
const signature = await signer.sign({
  hash: earnPlan.details.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 {
  account_id: string;
  signer_type: string;
  signer_address: string;
  legend_wallet_address: string;
  created_at: string;
}

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

interface ExecuteResult {
  plan_id: string;
  activity_id?: string;
  quark_intent_id?: string;
  status: string;
}