SDKs overview
The 1inch ecosystem offers Software Development Kits (SDKs) and libraries designed to simplify integration with its decentralized finance (DeFi) protocols. These tools enable developers to programmatically access features such as multi-DEX token swaps, liquidity aggregation, and limit order functionalities. The primary goal of these SDKs is to abstract the complexities of interacting directly with blockchain smart contracts and the 1inch API, providing language-specific interfaces for common development environments. This allows developers to build decentralized applications (dApps) that leverage 1inch's liquidity routing capabilities without needing to manage raw API requests or blockchain transaction signing processes directly.
1inch's SDKs focus on facilitating interaction with its core products: the 1inch Aggregation Protocol, which sources liquidity across various decentralized exchanges to find optimal swap paths, and the 1inch Limit Order Protocol, which provides a flexible and gas-efficient way to place limit orders. These SDKs are maintained to reflect updates in the underlying protocols and support evolving blockchain standards, ensuring compatibility and security for integrated applications. The availability of SDKs in widely used languages like JavaScript and Python aims to lower the barrier to entry for developers looking to integrate DeFi functionalities into their projects, from wallets and trading platforms to analytical tools and automated strategies.
Official SDKs by language
1inch provides official SDKs to facilitate integration with its aggregation and limit order protocols. These SDKs are designed to abstract the complexities of direct API interaction and blockchain transaction management, offering language-specific methods for common operations such as obtaining swap data, submitting orders, and querying protocol status. The official SDKs are primarily available for JavaScript and Python, reflecting their common use in web and backend development within the blockchain ecosystem.
The following table outlines the official SDKs, including their respective packages and installation commands:
| Language | Package Name | Installation Command | Maturity |
|---|---|---|---|
| JavaScript (Node.js/Browser) | @1inch/api-connector |
npm install @1inch/api-connector or yarn add @1inch/api-connector |
Stable |
| Python | 1inch-py |
pip install 1inch-py |
Stable |
The JavaScript SDK is foundational for web-based decentralized applications, allowing developers to integrate 1inch functionalities directly into front-end interfaces or Node.js backend services. The Python SDK is often utilized for scripting, data analysis, and backend services that require interaction with the 1inch API, offering a concise way to manage complex DeFi operations. Developers can find detailed documentation and usage examples for 1inch API interaction within the official 1inch developer documentation.
Installation
Installing the 1inch SDKs involves using standard package managers for each respective language. These commands retrieve the necessary libraries and their dependencies, making them available for import into your development projects. Before installation, ensure you have the appropriate runtime environment set up (Node.js for JavaScript, Python for Python).
JavaScript SDK Installation
For JavaScript projects, the official 1inch API connector can be installed using either npm or yarn, which are package managers for Node.js. This SDK provides connectivity to the 1inch Aggregation Protocol API.
Using npm:
npm install @1inch/api-connector
Using yarn:
yarn add @1inch/api-connector
After installation, you can import the module into your JavaScript or TypeScript files to begin interacting with the 1inch API. The 1inch API reference provides further details on available endpoints and data structures.
Python SDK Installation
The Python SDK for 1inch is distributed via PyPI (Python Package Index) and can be installed using pip, Python's package installer. This SDK simplifies interaction with the 1inch Aggregation Protocol for Python applications.
pip install 1inch-py
Once installed, the 1inch_py library can be imported into your Python scripts or applications. This allows access to functions for fetching quotes, performing swaps, and querying protocol information, as detailed in the 1inch Aggregation Protocol API documentation.
It is recommended to use a virtual environment for Python projects to manage dependencies effectively and avoid conflicts. You can create and activate a virtual environment before installing packages:
python -m venv venv
source venv/bin/activate # On Windows, use `venv\Scripts\activate`
pip install 1inch-py
Quickstart example
This quickstart example demonstrates how to use the 1inch API connector to fetch a swap quote for a token pair using the JavaScript SDK. The example assumes you have Node.js installed and the @1inch/api-connector package installed in your project.
Before running, ensure you have an API key if required by a specific endpoint or rate limit, though basic quote fetching often does not require one for the public API endpoints. You will also need to specify the blockchain network (e.g., Ethereum, Polygon) and the token addresses for the swap.
JavaScript Quickstart: Get a Swap Quote
This example retrieves the optimal swap route and amount for exchanging a certain quantity of Wrapped Ether (WETH) to Dai (DAI) on the Ethereum mainnet using the 1inch Aggregation Protocol.
import { OneInchKit } from '@1inch/api-connector';
const oneInch = new OneInchKit(1); // 1 for Ethereum Mainnet
async function getSwapQuote() {
try {
const fromTokenAddress = '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'; // WETH on Ethereum
const toTokenAddress = '0x6B175474E89094C44Da98b954EedeAC495271d0F'; // DAI on Ethereum
const amount = '1000000000000000000'; // 1 WETH in wei (10^18)
const fromAddress = '0xYourWalletAddressHere'; // Your wallet address
console.log('Fetching swap quote...');
const quote = await oneInch.getQuote({ fromTokenAddress, toTokenAddress, amount });
console.log('Quote received:');
console.log(` Source Token: ${quote.fromToken.symbol} (${quote.fromToken.name})`);
console.log(` Destination Token: ${quote.toToken.symbol} (${quote.toToken.name})`);
console.log(` Expected amount (destination): ${quote.toTokenAmount / (10 ** quote.toToken.decimals)} ${quote.toToken.symbol}`);
console.log(` Gas estimate: ${quote.estimatedGas}`);
// To get the transaction data for a swap, you would typically use getSwap()
// const swapTx = await oneInch.getSwap({ fromTokenAddress, toTokenAddress, amount, fromAddress, slippage: 1 });
// console.log('Swap transaction data:', swapTx.tx);
} catch (error) {
console.error('Error fetching swap quote:', error.message);
}
}
getSwapQuote();
To execute this code:
- Save the code as a
.jsfile (e.g.,getQuote.js). - Ensure
@1inch/api-connectoris installed (npm install @1inch/api-connector). - Run from your terminal:
node getQuote.js.
Remember to replace 0xYourWalletAddressHere with an actual Ethereum wallet address for production use, especially when requesting full swap transaction data. The OneInchKit constructor takes a chain ID; 1 represents Ethereum mainnet. Other chain IDs are available for networks like Polygon (137) or Optimism (10), as described in the 1inch API documentation.
Community libraries
While 1inch actively maintains official SDKs for JavaScript and Python, the open-source nature of blockchain development often leads to the creation of community-contributed libraries and wrappers. These might offer support for additional programming languages, specific frameworks, or specialized functionalities not covered by the official offerings. Community libraries can vary in their level of maintenance, feature completeness, and security. Developers should exercise due diligence, reviewing the codebase, community activity, and historical reliability before integrating third-party solutions into production environments.
For instance, developers building on platforms like Google Cloud or leveraging specific serverless functions might find community-built wrappers that streamline interactions with 1inch in environments where official SDKs might require more manual setup. Similarly, for languages like Go or Rust, community efforts might fill gaps, providing type-safe bindings or more idiomatic interfaces. However, the direct interaction with 1inch's Aggregation and Limit Order Protocols is well-covered by the official 1inch API, which provides comprehensive endpoints that can be consumed by any language with HTTP client capabilities.
Developers are encouraged to consult the official 1inch documentation and community forums (e.g., GitHub discussions, Discord channels) to discover and evaluate any community-supported tools. It is also common practice in the blockchain space to inspect the smart contracts directly on block explorers (like Etherscan) to understand the underlying logic, which is critical when relying on any third-party library, ensuring it aligns with the expected protocol behavior.