← Cookbook

✅ runnablePython ✓TypeScript ✓

Recipe 02 — Execute a swap with slippage protection

Status: ✅ runnable SDK surface: client.swap.execute(...) (auto-injects X-Idempotency-Key) Server endpoint: POST /v1/swap/execute TZ reference: TZ-1 §13.2, TZ-3 §3.4

What this recipe does

Quotes a USDC→USDT swap (recipe 01) then commits it with a 50 bps (0.5%) slippage cap. The SDK auto-generates a fresh X-Idempotency-Key so a network retry won't double-execute the swap.

Prerequisites

Same as recipe 01 plus:

Run

PYTHONPATH=packages/sdk-py/src python cookbook/recipes/02-execute-swap-slippage/python_example.py
npx tsx cookbook/recipes/02-execute-swap-slippage/typescript_example.ts

Expected output

intent.intent_id  = i_01HZ...
intent.status     = submitted
intent.tx_hash    = 0x...        (populated once the bundler relays)

Idempotency semantics

The SDK auto-attaches X-Idempotency-Key: <uuid4> on POST /v1/swap/execute. If the network call fails mid-flight and the SDK retries (or your caller retries explicitly), the same key is replayed, and the server returns the existing intent rather than executing twice. Pass idempotency_key="..." to lock the key yourself when you need cross-process retry consistency.

What to read next

Source: cookbook/recipes/02-execute-swap-slippage/README.md