# Quote With Fallback

This example shows an implementation for token swapping using the [ParaSwap API](https://x.com/i/grok?text=ParaSwap%20API), particularly focusing on getting a quote for a token swap and handling both [Delta pricing](https://x.com/i/grok?text=Delta%20pricing) and a fallback to market pricing when Delta fails.

{% hint style="info" %}
We recommend using [Paraswap SDK](https://github.com/paraswap/paraswap-sdk) for better developer experience. You can find SDK example of the exact functionality of this code [here](https://github.com/paraswap/paraswap-sdk/blob/master/src/examples/simpleQuote.ts).
{% endhint %}

Example:

```typescript
import axios from 'axios';
import { ethers } from 'ethers';

const API_URL = "https://api.paraswap.io";

const chainId = 1; // Mainnet
const DAI_TOKEN = '0x6b175474e89094c44da98b954eedeac495271d0f';
const PSP_TOKEN = '0xcafe001067cdef266afb7eb5a286dcfd277f3de5';

async function quoteWithFallback() {
  // @ts-expect-error assume window.ethereum is available
  const ethersProvider = new ethers.providers.Web3Provider(window.ethereum);

  const accounts = await ethersProvider.listAccounts();
  const account = accounts[0]!;
  const signer = ethersProvider.getSigner(account);

  const amount = '100000000000000000000'; // 100 DAI

  const { data: quote } = await axios.get(`${API_URL}/quote`, {
    params: {
      chainId,
      srcToken: DAI_TOKEN,
      destToken: PSP_TOKEN,
      amount,
      userAddress: account,
      srcDecimals: 18,
      destDecimals: 18,
      mode: 'all', // Delta quote if possible, with fallback to Market price
      side: 'SELL',
    },
  });

  if ('delta' in quote) {
    // Delta pricing is available - we can build and submit a order
    const price = quote.delta;
    
    // build order
    const { data: builtOrder } = await axios.post(`${API_URL}/orders/build`, {
      price,
      chainId,
      owner: account,
    });
    
    // sign the order
    const signature = signer.signTypedData(
      builtOrder.domain,
      builtOrder.types,
      builtOrder.value,
    );
    
    // compact the signature
    const compactSignature = ethers.Signature.from(signature).compactSerialized;

    // submit the order
    const { data: deltaAuction } = await axios.post(`${API_URL}/orders`, {
      order: builtOrder.value,
      signature: compactSignature,
      // partner: '...' // if available
    });

    // poll if necessary
    const { data: auction } = await axios.get(`${API_URL}/orders/${deltaAuction.id}`);
    if (auction?.status === 'EXECUTED') {
      console.log('Auction was executed');
    }
  } else {
    // Delta pricing failed - falling back to market
    console.log(
      `Delta Quote failed: ${quote.fallbackReason.errorType} - ${quote.fallbackReason.details}`
    );
    const priceRoute = quote.market;

    // build transaction params
    const { data: txParams } = await axios.post(`${API_URL}/transactions/${chainId}`, {
      srcToken: DAI_TOKEN,
      destToken: PSP_TOKEN,
      srcAmount: amount,
      slippage: 100, // 1%
      priceRoute,
      userAddress: account,
      // partner: '...' // if available
    });

    // submit the transaction
    const swapTx = await signer.sendTransaction(txParams);
  }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://developers.velora.xyz/api/velora-api/velora-delta-api/example-quote-with-fallback/quote-with-fallback.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
