Skip to main content

Embedding Swap in a dApp

This guide shows how to add token swap functionality to a web application using the Vulcx API and a Solana wallet adapter.

Prerequisites

  • A web app with a Solana wallet adapter (e.g., @solana/wallet-adapter-react)
  • @solana/web3.js installed

Implementation

1. Quote function

TypeScript

2. Swap function

TypeScript

3. Putting it together (React)

TypeScript

Error handling

Always handle these cases in your UI:

Best practices

  • Show price impact. Display priceImpactSeverity and warn users on moderate or higher.
  • Show route info. Display hopCount and pool types so users understand the execution path.
  • Refresh quotes. Quotes go stale. Re-fetch if the user hasn’t submitted within 15-30 seconds.
  • Handle rate limits. Implement retry with backoff for 429 responses. See Rate Limits.
Last modified on August 31, 2026