# Dexible API & SDK v2.0

<figure><img src="/files/hecNDZTLNTpLOTON5mOg" alt=""><figcaption></figcaption></figure>

See the [**Dexible Website**](https://dexible.io) | [**Dexible Web App UI**](https://app.dexible.io) | [**Dexible User Documentation** ](https://dexible.gitbook.io/dexible/)

If you're looking for docs on v1.0, please go here: <https://dexible.gitbook.io/dexible-sdk-v1.0/>

[Dexible](https://dexible.io) is a fully automated Execution Management System (EMS) for DeFi. In its simplest form, Dexible takes on the responsibility of executing DeFi "orders" according to user-specified "policies". These policies basically dictate ***how*** execution should be carried out.

The SDK is a library used to interact directly with the Dexible infrastructure. It makes it easier to call the appropriate APIs with wallet signatures, etc. With the SDK, it is possible to:

* Lookup token metadata quickly (including wallet balance and Dexible spend allowances)
* Get quotes for token swaps
* Submit orders to Dexible for execution
* Query active and historical orders
* Pause, Resume, and Cancel orders

Here is a quick example of how to get a quote and submit an order:

**Getting an Order Quote**

<pre><code><strong>import {Dexible, InfuraWeb3Factory, IERC20Token, TWAPSwap, units} from 'dexible-sdk';
</strong><strong>import {ethers} from 'ethers';
</strong><strong>
</strong><strong>const main = async () => {
</strong><strong>   const dex = new Dexible({
</strong>      web3Factory: new InfuraWeb3Factory(process.env.INFURA_KEY)
   });
   const weth: IERC20Token = {
      address: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
        decimals: 18,
        symbol: "WETH",
        name: "Wrapped ETH",
        chainId: 1
   };
   const usdc: IERC20Token = {
      address: "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
      decimals: 6,
      symbol: "USDC",
      name: "USD Coin",
      chainId: 1
   };
   
   const twap = new TWAPSwap({
      amountIn:  units.inBNUnits("1", 18),
      tokenOut: usdc,
      tokenIn: weth,
      slippage: new Slippage(.5, false), //false means not expressed as decimal but instead as a percentage
      timeWindowSeconds: 86400
   });
   
   const quote = await dex.exchange.quote(twap);
   console.log(quote);
}

main();

   
</code></pre>

This results in:

```
{
  id: 'bagaaierayqbbhep36rt45fopah6wnfdzry2767lnpjwf24ukqhj37js3fyjq',
  minAmountOut: BigNumber { _hex: '0x4b013278', _isBigNumber: true },
  bpsFee: BigNumber { _hex: '0x02d79883d20000', _isBigNumber: true },
  feeToken: {
    address: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
    decimals: 18,
    symbol: 'WETH',
    name: 'Wrapped ETH',
    chainId: 1
  },
  rounds: 1,
  totalEstimatedGasFees: BigNumber { _hex: '0x2ee5547da95e5b', _isBigNumber: true }
}
```

**Submit an Order**

```
 import {
     Dexible, 
     InfuraWeb3Factory, 
     IERC20Token, 
     TWAPSwap, 
     units, 
     ExeuctionStatus
} from 'dexible-sdk';
import {ethers} from 'ethers';

const main = async () => {
   const traderKey = process.env.TRADER_KEY;
   if(!traderKey) {
      throw new Error("Missing TRADER_KEY in environment");
   }
     
   const wallet = new ethers.Wallet(traderKey);
   const dex = new Dexible({
      signer: wallet,
      web3Factory: new InfuraWeb3Factory(process.env.INFURA_KEY)
   });
   const weth: IERC20Token = {
      address: "0xb4fbf271143f4fbf7b91a5ded31805e42b2208d6",
        decimals: 18,
        symbol: "WETH",
        name: "Wrapped ETH",
        chainId: 5
   };
   const uni: IERC20Token = {
      address: "0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984",
      decimals: 18,
      symbol: "UNI",
      name: "UNI",
      chainId: 5
   };
   
   const twap = new TWAPSwap({
      amountIn:  units.inBNUnits(".1", 18),
      tokenOut: uni,
      tokenIn: weth,
      slippage: new Slippage(.5, false), //false means not expressed as decimal but instead as a percentage
      timeWindowSeconds: 86400
   });
   
   let order = await dex.exchange.swap(twap);
   console.log(order);
   //then you can poll to get updated status
   while(order.status === ExecutionState.PENDING) {
       sleep(1000); //assume you can implement this
       order = await dex.exchange.status(order.id);
   }
}

main();
```

This results in order output:

```
{
      cummulativeBpsFee: BigNumber { _hex: '0x00', _isBigNumber: true },
      cummulativeGasFee: BigNumber { _hex: '0x00', _isBigNumber: true },
      cummulativeInput: BigNumber { _hex: '0x00', _isBigNumber: true },
      cummulativeOutput: BigNumber { _hex: '0x00', _isBigNumber: true },
      feeToken: undefined,
      id: '5:19',
      progress: 0,
      status: 'PENDING',
      tokenIn: {
        decimals: 18,
        symbol: 'WETH',
        balance: BigNumber { _hex: '0x06f337f894208e25', _isBigNumber: true },
        allowance: BigNumber {
          _hex: '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff',
          _isBigNumber: true
        }
      },
      tokenOut: {
        decimals: 18,
        symbol: 'UNI',
        balance: BigNumber { _hex: '0x00', _isBigNumber: true },
        allowance: BigNumber { _hex: '0x00', _isBigNumber: true }
      },
      totalInput: BigNumber { _hex: '0x016345785d8a0000', _isBigNumber: true },
      transactions: []
}
```

Before diving into the weeds of the SDK, it may be necessary to first understand how Dexible receives, validates, and executes orders--basically how Dexible works.


---

# 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://dexible.gitbook.io/dexible-sdk/master.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.
