> ## Documentation Index
> Fetch the complete documentation index at: https://seilabs-docs-evm-cookbook.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# API3

> Complete guide to integrating API3 with Sei

API3 is a decentralized oracle network that supplies real-time data feeds to smart contracts. Traditional oracles rely on third-party intermediaries. API3 instead connects APIs directly to the blockchain through first-party oracles that the API providers operate themselves. This approach reduces trust assumptions and potential points of failure.

## What you'll be doing in this guide

This tutorial shows you how to:

1. Integrate API3's data feeds into your smart contract on Sei
2. Get real-time price data for the SEI token from API3's proxy contract
3. Understand the structure of API3's reader proxy interface
4. Deploy and test your integration on Sei

By the end of this guide, you will have a working smart contract that can fetch SEI price data from API3's decentralized oracle network.

## Prerequisites

Before you start this tutorial, make sure that you have:

### Technical requirements

* **Solidity knowledge**: A basic understanding of smart contract development in Solidity
* **Development environment**: Remix IDE, Hardhat, or a similar Solidity development setup
* **Sei network access**: An RPC endpoint and access to Sei Mainnet

### Required dependencies

* OpenZeppelin Contracts library (`@openzeppelin/contracts`)
* API3 Contracts library (`@api3/contracts`)

#### Install

```bash theme={null}
# npm
npm install @openzeppelin/contracts @api3/contracts

# yarn
yarn add @openzeppelin/contracts @api3/contracts

# pnpm
pnpm add @openzeppelin/contracts @api3/contracts
```

### Sei network configuration

Make sure that your development environment is configured for Sei:

* **Mainnet RPC**: `https://evm-rpc.sei-apis.com`
* **Chain ID**: 1329 (Sei Mainnet)

## Contract code

This complete smart contract reads SEI price data from API3:

```solidity theme={null}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/access/Ownable.sol";
import "@api3/contracts/interfaces/IApi3ReaderProxy.sol";

contract DataFeedReaderExample is Ownable {
    // This contract reads from a single proxy. Your contract can read from
    // multiple proxies.
    address public proxy;

    constructor(address proxy_) Ownable(msg.sender) {
        setProxy(proxy_);

    }

    // Updating the proxy address is a security-critical action. In this
    // example, only the owner is allowed to do so.
    // You may want to update your proxy to switch to another data feed, enable
    // OEV support, or even switch to another oracle solution. Implementing a
    // method to update proxies is highly recommended.
    function setProxy(address proxy_) public onlyOwner {
        proxy = proxy_;
    }

    function readDataFeed()
        public
        view
        returns (int224 value, uint256 timestamp)
    {
        (value, timestamp) = IApi3ReaderProxy(proxy).read();
        // If you have any assumptions about `value` and `timestamp`, make sure
        // to validate them right after reading from the proxy. For example,
        // if the value you are reading is the spot price of an asset, you may
        // want to reject non-positive values...
        require(value > 0, "Value not positive");
        // ...and if the data feed is being updated with a one day-heartbeat
        // interval, you may want to check for that.
        require(
            timestamp + 1 days > block.timestamp,
            "Timestamp older than one day"
        );
        // After validation, you can implement your contract logic here.

        // Refer to https://docs.api3.org/dapps/integration/contract-integration.html
        // for more information about how to integrate your contract securely.
    }
}
```

### Contract breakdown

**Main components:**

1. **Proxy address**: The contract stores the address of the API3 proxy that supplies SEI price data
2. **Data validation**: The contract validates that price values are positive and that timestamps are recent
3. **IApi3ReaderProxy interface**: This interface has the `read()` function, which fetches price data

**The `readDataFeed()` function:**

* Returns `int224 value`: The SEI price (likely in USD with 18 decimal places)
* Returns `uint256 timestamp`: When the price was last updated
* Checks that the data is fresh (within 1 day)
* Checks that the price is positive

## Proxy address

This tutorial uses the API3 proxy at this address on Sei:

[`0x09c6e594DE2EB633902f00B87A43b27F80a31a60`](https://seiscan.io/address/0x09c6e594DE2EB633902f00B87A43b27F80a31a60)

This proxy supplies real-time [SEI/USD](https://market.api3.org/sei/sei-usd/integrate) price data from API3's decentralized oracle network.

## How to test

### 1. Deploy the contract

**Using Remix IDE:**

1. Open [Remix IDE](https://remix.ethereum.org/)
2. Create a new file named `DataFeedReaderExample.sol`
3. Paste the contract code from above
4. Compile the contract with the Solidity compiler (version ^0.8.0)
5. Deploy with this constructor parameter: `0x09c6e594DE2EB633902f00B87A43b27F80a31a60`

**Using Hardhat:**

```javascript theme={null}
const proxy = '0x09c6e594DE2EB633902f00B87A43b27F80a31a60';
const DataFeedReader = await ethers.getContractFactory('DataFeedReaderExample');
const reader = await DataFeedReader.deploy(proxy);
await reader.deployed();
```

### 2. Read price data

After you deploy the contract, call the `readDataFeed()` function to get the current SEI price:

```javascript theme={null}
// Using ethers.js
const result = await reader.readDataFeed();
console.log('SEI Price:', ethers.utils.formatUnits(result.value, 18));
console.log('Last Updated:', new Date(result.timestamp * 1000));
```

**Expected output format:**

* `value`: The SEI price in USD (with 18 decimals, for example `300000000000000000` = \$0.3)
* `timestamp`: Unix timestamp of the last price update

### 3. Integration examples

**Basic price display:**

```solidity theme={null}
function getSEIPrice() external view returns (int224 value) {
    (value, ) = readDataFeed();
    // `value` has 18 decimals, e.g. 300000000000000000 = $0.30.
    // Divide by 1e18 off-chain (or scale on-chain) to get the dollar price.
}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.