Kaia Safe API Kit
API-Kit is your go-to kit for securely interacting with the Safe Transaction API. The core of this kit is to allow valid signers to propose and share transactions with the other signers of a Safe, send the signatures to the service to collect them, and get information about a Safe (like reading the transaction history, pending transactions, enabled Modules and Guards, etc.), among other features.
Quickstart
By the end of this guide, you will be able to propose transactions to the service and obtain the owners' signatures for execution.
Prerequisites
- Node.js and npm
- A Safe with several signers
Set up environment
Step 1: Create a project directory.
Copy and paste this command in your terminal to create the project folder.
mkdir kaiasafe-api-kitcd kaiasafe-api-kit
Step 2: Initialize an npm project.
Copy and paste this command in your terminal to create a package.json
file.
npm init -y
Step 3: Install dependencies.
Using API-Kit is as simple as running the installation command below:
- npm
- yarn
npm install @safe-global/api-kit @safe-global/protocol-3 @safe-global/safe-core-sdk-types
yarn add @safe-global/api-kit @safe-global/protocol-kit @safe-global/safe-core-sdk-types
Step 4: Import dependencies.
Create a file named app.js
. This is where all our code snippets for this interaction would live.
Copy and paste these necessary imports at the top of the app.js
file.
import SafeApiKit from '@safe-global/api-kit'import Safe from '@safe-global/protocol-kit'import { OperationType} from '@safe-global/safe-core-sdk-types'
Step 5: Configure Setup
To efficiently illustrate how API-Kit works, we will use a Safe account setup with two or more signers, and threshold two, so we have multiple signatures that need to be collected when executing a transaction.
Copy and paste the following under the import statements in your app.js
file:
// https://chainlist.org/?search=kaia&testnets=trueconst RPC_URL = 'https://public-en-kairos.node.kaia.io'const SAFE_ADDRESS = "<REPLACE WITH SAFE PUBLIC ADDRESS HERE>"; // 2 Owner Safe Address Ex: 0x123.... SAFE SHOULD const OWNER_1_ADDRESS = "<REPLACE WITH OWNER 1 PUBLIC KEY HERE>"; // ONLY OWNER 1 and SAFE ADDRESS Need to have some test KAIA balanceconst OWNER_1_PRIVATE_KEY = "<REPLACE WITH OWNER 1 PRIVATE KEY HERE>";const OWNER_2_PRIVATE_KEY = "<REPLACE WITH OWNER 2 PRIVATE KEY HERE>"; // OWNER 2 need not have any test KAIAconst TO_ADDRESS = OWNER_1_ADDRESS; // Receiver address of sample transaction who receives 1 wei
Use API Kit
Step 1: Initialize API Kit
To initialize API Kit, we need to create an instance of the API Kit.
In chains where the Safe Transaction Service is supported, it's enough to specify the chainId property.
const apiKit = new SafeApiKit.default({ chainId: 1001n, txServiceUrl: 'https://docs-safe.kaia.io/txs-baobab/api'})
As you can see above, we included custom service using the optional txServiceUrl property.