Skip to content

First API Call

Let's walk through making your first API call to CyberPay. We'll get a quote for a cross-chain token swap.

Prerequisites

Before you start, make sure you have:

  • ✅ A CyberPay API key (get one here)
  • ✅ A development environment set up
  • ✅ Basic understanding of REST APIs

Step 1: Get a Quote

Let's get a quote to swap USDC from Optimism to Polygon:

curl -X POST https://api.cyberpay.org/v1/quote \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "crossSwapType": "anyToBridgeable",
    "amountType": "exactInput",
    "inputToken": {
      "address": "0x7F5c764cBc14f9669B88837ca1490cCa17c31607",
      "decimals": 6,
      "symbol": "USDC.e",
      "chainId": 10
    },
    "outputToken": {
      "address": "0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359",
      "decimals": 6,
      "symbol": "USDC",
      "chainId": 137
    },
    "inputAmount": "1000000",
    "slippage": 0.5
  }'

Step 2: Understanding the Response

You'll receive a response like this:

{
  "success": true,
  "data": {
    "id": "quote_1759231185333_abc123",
    "crossSwapType": "anyToBridgeable",
    "inputAmount": "1000000",
    "expectedOutputAmount": "995000",
    "minOutputAmount": "990000",
    "expectedFillTime": 420,
    "fees": {
      "total": {
        "amount": "5000",
        "amountUsd": "5.00",
        "pct": "0.5"
      }
    },
    "steps": {
      "originSwap": {...},
      "bridge": {...}
    }
  }
}

Key Response Fields

  • id: Unique quote identifier for creating orders
  • inputAmount: Actual amount needed (may differ from requested)
  • expectedOutputAmount: Expected tokens you'll receive
  • minOutputAmount: Minimum guaranteed output (after slippage)
  • expectedFillTime: Estimated completion time in seconds
  • fees: Detailed fee breakdown

Step 3: JavaScript Example

Here's the same request using JavaScript:

const response = await fetch('https://api.cyberpay.org/v1/quote', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer YOUR_API_KEY',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    crossSwapType: 'anyToBridgeable',
    amountType: 'exactInput',
    inputToken: {
      address: '0x7F5c764cBc14f9669B88837ca1490cCa17c31607',
      decimals: 6,
      symbol: 'USDC.e',
      chainId: 10
    },
    outputToken: {
      address: '0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359',
      decimals: 6,
      symbol: 'USDC',
      chainId: 137
    },
    inputAmount: '1000000',
    slippage: 0.5
  })
});
 
const quote = await response.json();
console.log('Quote received:', quote);

Step 4: Error Handling

Always implement proper error handling:

try {
  const response = await fetch('https://api.cyberpay.org/v1/quote', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer YOUR_API_KEY',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(quoteRequest)
  });
 
  if (!response.ok) {
    const error = await response.json();
    throw new Error(`API Error: ${error.error.message}`);
  }
 
  const quote = await response.json();
  return quote.data;
} catch (error) {
  console.error('Failed to get quote:', error);
  throw error;
}

Common Error Responses

Invalid Token Address

{
  "success": false,
  "error": {
    "code": "INVALID_TOKEN",
    "message": "Token address is not supported",
    "details": "Token 0x... is not available on chain 10"
  }
}

Insufficient Liquidity

{
  "success": false,
  "error": {
    "code": "INSUFFICIENT_LIQUIDITY",
    "message": "Not enough liquidity for this swap",
    "details": "Try reducing the amount or using a different token pair"
  }
}

Next Steps

Great! You've made your first API call. Now you can:

  1. Learn the concepts - Integration Guide
  2. Explore more APIs - Create Order
  3. Use our SDKs - JavaScript SDK
  4. Follow best practices - Integration Guide

Need Help?

If you run into issues: