This guide walks through a swap of 100 USDC on Base for USDC on Starknet, from quote to completion. Your app creates a secret, the user deposits, a solver locks the user’s tokens on Starknet, and your app reveals the secret so the user is paid. How Tern works explains why each step is there.

Before you start

  • Somewhere to store one secret per swap, encrypted, on your server or in the user’s app. Without the secret, a swap can’t complete and only refunds.
  • A connection to each destination chain you support (an RPC endpoint), so you can check the solver’s tokens yourself.
  • The user’s address on both chains. Refunds go to the source-chain address, so it must be one the user controls.
  • An API key, for production. You can build and test without one: see Authentication and limits.
1

Create a secret

Generate 32 random bytes with a secure random generator. This secret is what completes the swap. Tern’s API calls it the fulfillment.Its SHA-256 hash is what you share with Tern up front. The API calls it the executionCondition.
Use a new secret for every swap. Tern refuses a hash that any earlier quote used (409 condition_reused), because one revealed secret would unlock every swap that shared it.
2

Get a firm quote

When the user confirms the price, ask for a firm quote at POST /v1/quotes. It’s the same body as the price, plus three fields:
  • sender: the user’s address on the source chain, where any refund goes.
  • recipient: the user’s address on the destination chain.
  • executionCondition: the hash from step 1.
You can also set slippageBps: how far below the expected amount the user accepts, in case the solver has to buy the token they want. It’s 50 (0.5%) by default and at most 300 (3%). Add an Idempotency-Key header so a retried request returns the same quote.
Tern answers 201 Created, with the quote’s URL in Location:
  • id identifies this swap from now on. Store it with the secret.
  • to.amount is what the user should receive, and to.minAmount the least they’ll receive: show both. See Amounts.
  • expiresAt is the deadline to deposit, 30 minutes from now.
  • execute is how the user deposits, in the next step. steps lists what to send, in order: each step says what it does (kind) and holds a clean transaction (tx) with the chain it’s for (chainId) and who sends it (from).
This is the same quote object every quote endpoint and webhook returns. The dollar values, price impact, and gas estimate stay on the price you showed.For what to show about cost, see Fees and pricing.
3

Take the deposit

Before expiresAt, the user deposits in one of two ways.
  • EVM chains: send each step’s tx in order, as is, from sender on the chain in chainId, each once the one before it is mined. An approve step comes first only when it’s needed. USDT on Ethereum may get an extra approval of zero first, because that token requires it.
  • Starknet: each step carries a call instead of a tx. Send them all in one transaction, in order, from sender: account.execute(steps.map((step) => step.call)).
Before the user signs, check that the transactions only approve the token being sold to Tern’s contract and then call it. Hard-code the address from Contract addresses.If the page reloads before the user has sent anything, read the quote again with GET /v1/quotes/{id}?withTransactions=true: until a deposit is seen or the quote expires, its execute carries the steps again, with the approval only if it’s still needed.
Once you have the deposit’s transaction hash, you can send it to POST /v1/index so the status updates sooner. Tern finds the deposit either way.
4

Wait for the solver

Check the swap’s status with GET /v1/quotes/{id}, or receive it through webhooks. It moves from awaiting_deposit to deposit_confirmed once the deposit is confirmed, then to in_progress once the solver has locked the user’s tokens on the destination chain.
At in_progress, move to the next step. If the status becomes refunded or expired instead, the swap is over: see If something goes wrong. Track a swap covers how often to check and what each field means.
5

Check the solver's tokens

Before you reveal the secret, confirm on the destination chain that the solver has really locked the user’s tokens. Use your own connection to the chain, not Tern’s status or a webhook: this check is what makes revealing safe.The solver locks the tokens in Tern’s escrow contract, in one or more locks tied to your hash. From in_progress on, the quote names them in to.escrows: each lock’s id, the transaction that created it (txHash), and its amount.
to.escrows only tells you where to look. For each lock, read its transaction’s receipt and find the escrow contract’s Prepared event for that id, then check on chain that:
  1. The event carries your hash, comes from Tern’s escrow contract (hard-coded from Contract addresses), pays recipient, holds to.asset, and expires at least 30 minutes from now.
  2. The lock is still open: on EVM chains escrows(id) returns its amount and state (1 means open); on Starknet, get_escrow(id) returns the same.
  3. Together the locks hold at least to.minAmount.
On EVM chains the hash is the Prepared event’s third indexed field (topics[3]); on Starknet it’s a key of the event. The exact event formats are in the specification. If the locks named don’t pass, you can also search the escrow contract’s Prepared events for your hash.If the solver locked less than the minimum, don’t reveal. Wait a few minutes for it to add the rest; if it doesn’t, the swap refunds.
6

Reveal the secret

Send the secret from step 1 to Tern, with an Idempotency-Key:
Tern answers 202 Accepted with the quote, and pays the user on the destination chain. It runs the same checks first. If the solver’s tokens aren’t ready yet, it answers 409 destination_not_ready and does nothing: wait a few seconds and try again with the same key.Keep checking the quote until it’s completed. to.txHash is the transaction that paid the user.
Once revealed, the secret is public. Reveal it only after the checks in the previous step pass.

If something goes wrong

Next steps

Track a swap

Follow statuses and show users their swap history.

Webhooks

Get every status change sent to your server.

Refunds

Every way a swap can end without completing.

Errors

Every error and what to do about it.