SDK Examples

Complete, runnable scripts that perform the full swap flow: register an agent, get a quote, execute a swap, and check the status.


Bash (curl)

#!/usr/bin/env bash
set -euo pipefail

BASE_URL="https://api.suwappu.bot/v1/agent"

# Step 1: Register
echo "Registering agent..."
REGISTER_RESPONSE=$(curl -s -X POST "$BASE_URL/register" \
  -H "Content-Type: application/json" \
  -d '{"name":"my-trading-bot","description":"Automated trading agent"}')

API_KEY=$(echo "$REGISTER_RESPONSE" | jq -r '.agent.api_key')
AGENT_ID=$(echo "$REGISTER_RESPONSE" | jq -r '.agent.id')
echo "Agent registered: $AGENT_ID"
echo "API key: $API_KEY"

# Step 2: Get a quote
echo ""
echo "Requesting quote..."
QUOTE_RESPONSE=$(curl -s -X POST "$BASE_URL/quote" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d '{
    "from_token": "USDC",
    "to_token": "ETH",
    "amount": "500.00",
    "chain": "ethereum"
  }')

QUOTE_ID=$(echo "$QUOTE_RESPONSE" | jq -r '.quote.quote_id')
AMOUNT_OUT=$(echo "$QUOTE_RESPONSE" | jq -r '.quote.amount_out')
EXPIRES=$(echo "$QUOTE_RESPONSE" | jq -r '.quote.expires_in_seconds')
echo "Quote ID: $QUOTE_ID"
echo "You will receive: $AMOUNT_OUT ETH"
echo "Quote expires in: ${EXPIRES}s"

# Step 3: Execute the swap
echo ""
echo "Executing swap..."
SWAP_RESPONSE=$(curl -s -X POST "$BASE_URL/swap/execute" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d "{\"quote_id\": \"$QUOTE_ID\"}")

SWAP_ID=$(echo "$SWAP_RESPONSE" | jq -r '.swap.swap_id')
TX_HASH=$(echo "$SWAP_RESPONSE" | jq -r '.swap.tx_hash')
echo "Swap ID: $SWAP_ID"
echo "Tx hash: $TX_HASH"

# Step 4: Poll for completion
echo ""
echo "Checking status..."
for i in $(seq 1 10); do
  STATUS_RESPONSE=$(curl -s -X GET "$BASE_URL/swap/status/$SWAP_ID" \
    -H "Authorization: Bearer $API_KEY")

  STATUS=$(echo "$STATUS_RESPONSE" | jq -r '.swap.status')
  echo "  Attempt $i: status=$STATUS"

  if [ "$STATUS" = "completed" ] || [ "$STATUS" = "failed" ]; then
    echo ""
    echo "$STATUS_RESPONSE" | jq .
    break
  fi

  sleep 2
done

Python

Requires Python 3.7+ and the requests library (pip install requests).


TypeScript

Requires Node.js 18+ (for native fetch). No external dependencies needed.

Last updated