# API

The De.Fi API is the easiest way to integrate our expansive functionality into any app of your choosing. Whether you are interested in querying our audit or Rekt databases, querying a wallet's balances or details about the many blockchains we've integrated, our API is here for you.

Our API supports all the tokens, protocols and blockchains supported by our app - meaning you'll have access to data from 35+ blockchains, 300+ protocols and countless tokens.

## Querying

Queries should be made to the following URL:

[`https://public-api.de.fi/graphql`](https://public-api.de.fi/graphql)

You can test out your queries before setting them up in your app through our handy [**GraphQL playground**](https://public-api.de.fi/graphql/).

Before being able to perform queries, make sure you have properly [**authenticated**](#authentication) them!

## Available Endpoints

#### General endpoints

* **credits** - Credit information with regards to your API key.
* **chains** - Includes information about the many blockchains we have integrated into our dashboard. **Please use `chain_id` values from the response for other calls listed below**
* **assets** - Paginated data about the many assets we have integrated into our dashboard.&#x20;

&#x20;  <mark style="color:blue;">**Please be aware that the values of**</mark><mark style="color:blue;">**&#x20;**</mark><mark style="color:blue;">**`chain_id`**</mark><mark style="color:blue;">**&#x20;**</mark><mark style="color:blue;">**properties across the platform (in the response and the query params ) DON'T correspond with the absolute values (56 is for BSC, 137 is for Polygon, etc.) - the platform operates values like 1 is for Ethereum, 2 is for BNB, 3 for Polygon, etc. Please see all the values by calling**</mark><mark style="color:blue;">**&#x20;**</mark><mark style="color:blue;">**`chains`**</mark><mark style="color:blue;">**&#x20;**</mark><mark style="color:blue;">**endpoint.**</mark>

Portfolio endpoints

* **assetBalances** - ERC20 balances for any given wallet address&#x20;
* **assetBalancesAdvanced (BETA)** - ERC20 balances for any given wallet address. Allows fetching data for multiple addresses and chains through a single request&#x20;
* **protocols** - Includes information about the many dapps we have integrated into our dashboard ([**list here**](https://docs.de.fi/dashboard/the-defi-dashboard/supported-protocols)).&#x20;
* **protocolBalanceV2  (BETA) -** protocol balances for any given wallet address. Allows fetching data for multiple addresses and chains through a single request&#x20;
* **protocolBalance** - protocol balances for any given wallet address&#x20;

#### Security endpoints

* **rekts** - Paginated data about all hacks and exploits in our [**Rekt Database**](https://docs.de.fi/audits/rekt-database).
* ~~**shields**~~**&#x20;(DEPRECATED)** - ~~Safety information from~~ [~~**De.Fi Shield**~~](https://docs.de.fi/security-toolkit/shield) ~~regarding any given wallet address(es).~~
* **shieldAdvanced** - Safety information from [**De.Fi Shield**](https://docs.de.fi/security-toolkit/shield) regarding any given wallet address(es).
* **shieldApprovals** - The list of ERC20 approvals regarding any given wallet address(es).
* **shieldApprovalsNft** - The list of ERC721 and ERC1155 approvals regarding any given wallet address(es).
* **scannerLiquidityAnalysis** - The global liquidity distribution, top 10 LP holders and top 10 liquidity pairs regarding any given token address.
* **scannerHolderAnalysis** - Info on the top 10 holders for any given token address.
* **scannerProject** - The list of all issues found (both static and dynamic), security score, similar contracts and onchain governance info for any given smart contract address.

#### Opportunities endpoints

* **opportunities** - Information regarding any DeFi yield opportunities available based on your preferred search terms.
* **opportunityFarms** - List of DeFi yield opportunities.

## Potential Use Cases

Need some ideas on how to use our API endpoints to build something cool? Check below:

#### Portfolio Dashboard

* Make a call to the `chains` endpoint to get all the chains supported by De.Fi

```
query {
  chains  {
    id
    name
  }
}
```

* Make calls to the `assetBalancesAdvanced` endpoint to get ERC20 balances (in case of EVM chains) and `assetBalances` for non-EVM chains (please see the details here <https://public-api.de.fi/graphql> )

```
query {
  assetBalancesAdvanced(
    chainIds: [1, 2]
    walletAddresses: ["...wallet #1...", "...wallet #2..."]) {
    total
    wallet
    chains {
      chain {
        id
      }
    }
  }
}
```

* Make a call to `protocols`  with `version = V2` for TOP EVM and `version = V1` for the rest of the platforms supported by De.Fi, including non-EVM chains

<mark style="color:blue;">The list of platforms supported by the V2 endpoint is constantly expanding! Please reach out to the team if you have any questions or suggestions!</mark>

```
 query {
  protocols (version: V2) {
    name
    slug
    features {
      list 
    }
  }
}
```

* Make calls to the `protocolBalanceV2` endpoint to get user positions and balances for the TOP platforms supported by De.Fi &#x20;

```
query {
  protocolBalanceV2 (
    chainIds: [1,2]
    walletAddresses:["...wallet #1...", "...wallet #2..."]
  ){
    errors
    balances {
      protocol {
        slug 
      }
      address
      total
    }

  }
}
```

* Make calls to the `protocolBalance` endpoint for each of the chain-wallet-protocol combinations to get user positions and balances for the rest of the platforms supported by De.Fi, including non-EVM chains

```
query {
  protocolBalance ( balances: {
    chainIds: [1]
    walletAddress: "...wallet..."
    protocolName: "Arrakis"
  }
  ){
    protocol {
      slug
    }
    total
    chains {
      total
    }
  }
}
```

#### Opportunities Dashboard

* Make a call to the `opportunityFarms` endpoint to get all platforms and features supported by De.Fi's Explore Yield feature
* Make a call to the `opportunities` endpoint to get all the yield opportunities available. It's possible to filter by platform, feature, chain, etc.

#### Security Dashboard

* Make a call to the `shieldApprovals` endpoint to get all approvals given out by a wallet address
* Make a call to the `assetBalances` endpoint with the token addresses returned by the previous call. It will return the list of tokens that are still in the wallet (helps filter out approvals for zero-balance tokens)
* Make a call to the `shieldAdvanced` endpoint with the smart contract addresses received prior for more info

#### Scanner Dashboard

* Make calls to the `scannerProject` endpoint with the smart contract addresses received from similar calls to the "Security Dashboard" idea. This will return advanced security analysis for each relevant smart contract
* Make a call to the `scannerHolderAnalysis` endpoint with the tokens addresses received prior for more holder information for each contract
* Make a call to the `scannerLiquidityAnalysis` endpoint with the tokens addresses received prior for more liquidity information for each contract

## Authentication

In order to use our API, you must first get a valid API key. Get one by sending a request to `info@de.fi`.

You will have to use a `X-Api-Key` header with your key on every request. For example, on our GraphQL playground, you should test your key as follows:

<figure><img src="https://1036063852-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FXOyvZ43P03BZ8mN6KNWT%2Fuploads%2F67UgKVICRYE3APgn7YCP%2FScreenshot%20at%20Oct%2012%2014-34-25.png?alt=media&#x26;token=74a2ea91-32d0-4f9a-90dd-b6f9199def52" alt=""><figcaption></figcaption></figure>

## Pricing & Rate Limits

In order to make queries with your API key, you must purchase credits. The more complex your queries, the more credits may be used up when requesting data from our API.

| Endpoint                 | Credit Cost | Rate Limit (per minute) |
| ------------------------ | ----------: | ----------------------: |
| assetBalances            |          10 |                      20 |
| assets                   |           1 |                     100 |
| assetsPrices             |           1 |                     100 |
| chains                   |           1 |                     100 |
| credits                  |           1 |                     100 |
| opportunities            |           3 |                     100 |
| opportunityFarms         |           1 |                     100 |
| protocolBalance          |          10 |                      20 |
| protocols                |           1 |                     100 |
| rekts                    |           3 |                     100 |
| shields                  |          10 |                      20 |
| shieldApprovals          |          10 |                      20 |
| shieldApprovalsNft       |           5 |                      20 |
| shieldAdvanced           |           5 |                      20 |
| scannerLiquidityAnalysis |          10 |                      20 |
| scannerHolderAnalysis    |           5 |                      20 |
| scannerProject           |          25 |                      20 |

## SDK

We also provide an SDK to facilitate our API's usage if you plan on utilizing it in a Node.js environment. Install it as follows:

`npm i @de-fi/sdk`

The SDK's documentation can be found [**here**](https://www.npmjs.com/package/@de-fi/sdk).


---

# 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://docs.de.fi/api/api.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.
