Guides · Recipes

Recipes

Copy-paste patterns for the things agents actually do — wired with the SDK. The same flows are available as MCP tools.

Stream live prices into an agent

typescript
import { VenymClient } from "@venym/sdk";
const venym = new VenymClient();

const ac = new AbortController();
const bars: number[] = [];
for await (const ev of venym.streams.candles("BTC", "1m", { signal: ac.signal })) {
  bars.push(ev.data.close);
  if (bars.length >= 60) ac.abort();      // 1h of context, then act
}
// → feed `bars` into your model's decision step

Place a smart-routed perp trade

typescript
const venym = new VenymClient({ apiKey: process.env.VENYM_API_KEY });

const routing = await venym.routing.getRecommendation("BTC", { side: "BUY" });
console.log("best venue:", routing.recommended, "@", routing.price);

const order = await venym.orders.place(
  { symbol: "BTC", side: "BUY", type: "MARKET", quantity: "0.01" },
  { idempotencyKey: crypto.randomUUID() },   // safe to retry
);

Open a long/short pair trade

One position: long ETH, short BTC, balanced by USD notional, executed as two perp legs.

typescript
const pair = await venym.pairs.open({
  longSymbol: "ETH",
  shortSymbol: "BTC",
  notionalUsd: 1000,
  leverage: 3,
  venue: "hyperliquid",
});
// ...later
await venym.pairs.close(pair.id!);

Cross-chain swap — non-custodial and custodial

Non-custodial (you sign): quote → prepare → sign the returned transaction with your own wallet.

typescript
const quote = await venym.swaps.quote({
  fromChain: 42161, toChain: 8453,
  fromToken: "0x0000000000000000000000000000000000000000",   // ETH on Arbitrum
  toToken: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",     // USDC on Base
  fromAmount: "5000000000000000",
});
const prepared = await venym.swaps.prepare({ quoteId: quote.bestQuote?.quoteId });
// → sign prepared.transactionRequest with viem / @solana/web3.js and broadcast

Headless (delegated wallet, scope swap:execute):

typescript
const result = await venym.swaps.execute({ quoteId: quote.bestQuote?.quoteId });
const status = await venym.swaps.getStatus(result.swapId!);

A complete trading-agent loop

typescript
import { VenymClient, VenymRateLimitError } from "@venym/sdk";
const venym = new VenymClient({ apiKey: process.env.VENYM_API_KEY });

async function tick(symbol: string) {
  const [book, routing] = await Promise.all([
    venym.markets.getOrderbook(symbol),
    venym.routing.getRecommendation(symbol, { side: "BUY" }),
  ]);

  const signal = decide(book, routing);     // your strategy
  if (signal === "buy") {
    try {
      await venym.orders.place(
        { symbol, side: "BUY", type: "MARKET", quantity: "0.01" },
        { idempotencyKey: `${symbol}-${Date.now()}` },
      );
    } catch (e) {
      if (e instanceof VenymRateLimitError) await sleep(e.retryAfterMs ?? 1000);
      else throw e;   // policy violations etc. surface as typed errors
    }
  }
}

See the safety model for guardrails on autonomous loops.