Skip to main content
This example uses the HTTP API and @stellar/stellar-sdk@16 directly. If you would rather not maintain it, the TypeScript SDK builds and verifies the swap transaction for you.
The code blocks below form one file, stellar-swap.ts, in order. It targets a single-signature account using source-account authorization. Multisig wallets and separate Soroban authorization-entry signatures need their wallet’s authorization flow. It stops on restoration requirements and on multi-transaction plans. It never touches a secret key.
1

Define your wallet and deployment

Supply your wallet’s signing adapter, trusted deployment settings, and a review callback that decodes the transaction and obtains approval. Throw from review on any mismatch.
2

Quote against a verified deployment

Read /api/v1/config and compare its network and router with your trusted configuration before you trust any quote.
3

Sign, submit, and confirm

One in-flight transaction per source account. The helper checks the network and source, populates the sequence and prepares resources only when the server has not already prepared the envelope, asks for review, signs, verifies the wallet did not change the transaction, submits, and polls until a final result. Request a new quote if the account has been used since server preparation.
4

Swap from a wallet

Call swap from your confirmation handler.
5

Compose with lending

Pass your lending SDK’s buildControllerTransaction. It must return the unsigned outer transaction with the intended action, account, amounts, and the opaque route payload. The same helper then simulates, signs, submits, and confirms the complete outer transaction.

Run it on testnet

Use a testnet aggregator deployment and its router, a matching testnet RPC, a funded test wallet, supported token contracts, and enough balance. Check /api/v1/config against your trusted network and router. Do not assume the public service URL is testnet. The example is type-checked against SDK 16. Its sequence and submission flow can be checked offline with mocked RPC. A live run still depends on the deployment, balances, pool liquidity, and wallet authorization. Integration guide · Error recovery