Manage user delegates
This guide shows how to interact with the Safe Transaction Service API to manage user delegates.
The different steps are implemented using Curl (opens in a new tab) requests, the Safe{Core} SDK (opens in a new tab) TypeScript library and the safe-eth-py (opens in a new tab) Python library.
The Safe Queue Service serves the delegate endpoints for every chain, so the Curl requests in this guide call it and pass the chain ID in the request. If your integration still calls the Safe Transaction Service for these endpoints, see the migration guide.
Prerequisites
- Node.js and npm (opens in a new tab) when using the Safe{Core} SDK.
- Python (opens in a new tab) >= 3.9 when using
safe-eth-py. - Have a Safe account.
Steps
Install dependencies
_10yarn add ethers @safe-global/api-kit @safe-global/protocol-kit @safe-global/types-kit
Imports
_10import { ethers } from 'ethers'_10import SafeApiKit, { AddSafeDelegateProps } from '@safe-global/api-kit'
Get the delegates from a Safe
_11// Initialize the API Kit_11// How to get an Api key => http://docs.safe.global/core-api/how-to-use-api-keys_11const apiKit = new SafeApiKit({_11 chainId: 11155111n,_11 apiKey: 'YOUR_API_KEY'_11})_11_11// Get the Safe delegates_11const delegates = await apiKit.getSafeDelegates({_11 delegatorAddress: config.SAFE_ADDRESS_11})
Add a delegate to a delegator
_17const provider = new ethers.JsonRpcProvider(config.RPC_URL)_17_17const ownerA = new ethers.Wallet(config.OWNER_A_PRIVATE_KEY, provider)_17const ownerAAddress = await ownerA.getAddress()_17_17const ownerB = new ethers.Wallet(config.OWNER_B_PRIVATE_KEY, provider)_17const ownerBAddress = await ownerB.getAddress()_17_17const delegateConfig: AddSafeDelegateProps = {_17 delegateAddress: ownerBAddress || '0x',_17 delegatorAddress: ownerAAddress || '0x',_17 signer: ownerA,_17 label: 'Label'_17}_17_17// Add Owner B as a delegate of Owner A for all Safes accounts (safeAddress = null)_17const safeDelegateAddResponse = await apiKit.addSafeDelegate(delegateConfig)
Delete a delegate of a delegator
_10const delegateConfig: DeleteSafeDelegateProps = {_10 delegateAddress: ownerBAddress || '0x',_10 delegatorAddress: ownerAAddress || '0x',_10 signer: ownerA_10}_10_10// Remove Owner B as delegate of Owner A_10await apiKit.removeSafeDelegate(delegateConfig)