Examples

Examples of using the REST API with Javascript

Example 1:

Get the estimated output from executing a bridge

async function estimateBridgeOutput(
  fromChain,
  toChain,
  fromToken,
  toToken,
  amountFrom
) {
  const query_string = `fromChain=${fromChain}&toChain=${toChain}&fromToken=${fromToken}&toToken=${toToken}&amountFrom=${amountFrom}`;
  const response = await fetch(
    `https://synapse-rest-api-v2.herokuapp.com/bridge?${query_string}`
  );

  const response_json = await response.json();
    // Check if the response is an array and has at least one item
  if (Array.isArray(response_json) && response_json.length > 0) {
    return response_json[0]; // Return the first item
  } else {
    throw new Error('No bridge quotes available');
  }
}


estimateBridgeOutput(
  1,     // Ethereum Chain ID
  42161, //Arbitrum Chain ID
  "USDC", 
  "USDC", 
  "1000"
).then(firstQuote => {
  console.log('First bridge quote:', firstQuote);
}).catch(error => {
  console.error('Error:', error.message);
});

Example 2:

Generate an unsigned bridge transaction

async function generateUnsignedBridgeTxn(
  fromChain,
  toChain,
  fromToken,
  toToken,
  amountFrom,
  destAddress
) {
  const query_string = `fromChain=${fromChain}&toChain=${toChain}&fromToken=${fromToken}&toToken=${toToken}&amount=${amountFrom}&destAddress=${addressTo}`;
  const response = await fetch(
    `https://synapse-rest-api-v2.herokuapp.com/bridgeTxInfo?${query_string}`
  );
  const response_json = await response.json();
  return await response_json;
}

generateUnsignedBridgeTxn(
   1,     // Ethereum Chain ID
  42161, //Arbitrum Chain ID
  "USDC", 
  "USDC", 
  "1000"
  "0x2D2c027E0d1A899a1965910Dd272bcaE1cD03c22"
);

Example 3:

A cURL request to get information about a swap transaction.

curl --request GET \
  --url 'https://synapse-rest-api-v2.herokuapp.com/swap?chain=1&fromToken=USDC&toToken=DAI&amount=100'

Last updated