Beginner Tutorial

πŸ“– What You'll Learn

This guide teaches you how to integrate Soroswap API into your application using:

  • Freighter Wallet for secure wallet connection and transaction signing

  • Soroswap API for finding the best swap routes and transaction submission

🎯 Prerequisites

Before starting, make sure you have:

  • Basic knowledge of HTML, CSS, and JavaScript

  • Freighter Wallet extension installed

  • An API key from Soroswap sign up at https://api.soroswap.finance/login

  • A web browser with developer tools

  • 5-10 minutes of focused time

πŸ—οΈ Project Structure Overview

Our swap application consists of 6 main parts:

1. HTML Structure     β†’ User interface elements with professional styling
2. External Libraries β†’ Freighter API for wallet integration  
3. Configuration     β†’ Centralized CONFIG object with all settings
4. Application State  β†’ Tracks wallet connection and transaction status
5. Utility Functions  β†’ Helper functions for UI updates and API calls
6. Core Functions    β†’ Connect, Quote, Sign, and Send (4 separate steps)

Since this guide is to use the api, we'll focus on the core functions that make the swap happen.

πŸ”§ Core Functions Explained

Function 1: Connect to Wallet πŸ”—

πŸ” What this function does:

  1. Checks if Freighter wallet is available

  2. Requests permission to access the wallet

  3. Stores the wallet address for later use

  4. Updates the UI to show connection status

🚨 Common issues:

  • User doesn't have Freighter installed β†’ Show installation instructions

  • User denies permission β†’ Ask them to try again

Function 2: Get Quote and Build Transaction πŸ’±

πŸ” What this function does:

  1. Validates wallet connection first

  2. Requests the best price from multiple exchanges

  3. Builds a transaction based on that price

  4. Prepares the transaction for signing (stores in app state)

  5. Updates the UI to enable the next step

🚨 Common issues:

  • Wallet not connected β†’ Connect wallet first

  • API key expired β†’ Get a new one from dashboard

  • Insufficient balance β†’ Make sure wallet has enough XLM

Function 3: Sign the Transaction ✍️

πŸ” What this function does:

  1. Validates wallet connection and transaction availability

  2. Calls Freighter to sign the transaction

  3. Stores the signed transaction in app state

  4. Updates UI to enable final step

🚨 Common issues:

  • User rejects signing β†’ Ask them to try again

  • Freighter not connected β†’ Check wallet connection

  • Wrong network β†’ Ensure Freighter is on testnet

Function 4: Send Transaction to Network πŸš€

πŸ” What this function does:

  1. Validates we have a signed transaction ready

  2. Submits the transaction to the Stellar network via Soroswap API

  3. Shows success message with transaction hash and explorer link

  4. Resets state for potential next transaction

πŸ”„ Complete Workflow Summary

4-Step Process:

πŸ› οΈ Key Concepts for Beginners

What is XDR?

XDR (External Data Representation) is how Stellar transactions are encoded:

  • Unsigned XDR: Transaction ready to be signed

  • Signed XDR: Transaction with digital signature, ready to submit

What is a Quote?

A quote tells you:

  • How much you'll receive for your trade

  • Which exchanges offer the best price

  • What path the trade will take

What is Signing?

Signing a transaction means:

  • Proving you own the wallet

  • Authorizing the transaction

  • Making it ready for the network

🚨 Security Best Practices

βœ… DO:

  • Use testnet for learning and testing

  • Keep your API keys secure

  • Verify transaction details before signing

  • Start with small amounts

❌ DON'T:

  • Put API keys in public code repositories

  • Sign transactions you don't understand

  • Use mainnet while learning

  • Hardcode private keys (never!)

πŸ”§ Common Troubleshooting

Problem: "Freighter not found"

Solution: Install Freighter wallet extension

Problem: "403 Forbidden" error

Solution: Check your API key is correct and not expired

Problem: "Insufficient liquidity"

Solution: Try a smaller amount or different token pair

Problem: Transaction fails

Solution: Check you have enough XLM for fees

πŸŽ“ Next Steps

Once you understand this basic example:

  1. Customize the UI with better styling

  2. Add error handling for better user experience

  3. Support multiple tokens instead of just XLM→USDC

  4. Add slippage protection for price changes

  5. Try new methods from Soroswap API

  6. Experiment using the SDK

πŸ“š Additional Resources

  • Soroswap API Docs: https://api.soroswap.finance/docs

  • Freighter Docs: https://freighter.app/docs

  • Stellar Docs: https://developers.stellar.org

  • Stellar Expert: https://stellar.expert (blockchain explorer)

πŸ’‘ Pro Tips

  1. Use browser dev tools to debug API calls

  2. Check the console for error messages

  3. Read API responses to understand what's happening

Last updated