# Custom Operation
URL: /docs/guides/custom-user-operation
Run a custom sequence of on-chain actions in a single operation.
### Overview [#overview]
Use this endpoint to call any on-chain app that the other Notus endpoints do not cover, or to combine several actions into one operation. Most apps do not need it and can use the dedicated endpoints instead. The steps below walk through a simple transfer to show the flow.
Use this guide to build and execute a sequence of contract calls in a single UserOperation. In this example, you create a custom UserOperation that transfers tokens from your smart wallet to a recipient, then sign and execute it.
## Steps [#steps]
### Install dependencies and set up the user's key [#install-dependencies-and-set-up-the-users-key]
Install `viem` to approve operations with the user's key.
Install `viem` to sign messages and interact with the blockchain, then initialize your wallet account.
```package-install
viem
```
Initialize the key that approves operations for the user's account.
Use your private key to initialize a wallet account.
```js
import { privateKeyToAccount } from 'viem/accounts'
const BASE_URL = 'https://api.notus.team/api/v1'
const API_KEY = ''
const USDC_POLYGON = '0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359'
const privateKey = '0x'
const account = privateKeyToAccount(privateKey)
```
### Register the user's account [#register-the-users-account]
Register and retrieve the user's account address.
Register and retrieve the smart wallet address used for Account Abstraction.
```js
async function setupSmartWallet() {
const FACTORY_ADDRESS = '0x0000000000400CdFef5E2714E63d8040b700BC24'
const externallyOwnedAccount = account.address
const res = await fetch(`${BASE_URL}/wallets/register`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': API_KEY,
},
body: JSON.stringify({
externallyOwnedAccount,
factory: FACTORY_ADDRESS,
salt: '0',
}),
})
if (!res.ok) return
const data = await res.json()
return data.wallet.walletAddress
}
```
The smart wallet is deployed automatically on the first onchain transaction via a UserOperation.
### Create a custom operation [#create-a-custom-operation]
Call `POST /crypto/custom-user-operation` to build the operation. In this example, you send 5 digital dollars (USDC) on the Polygon network to a recipient.
Compose a single ERC-20 transfer operation. In this example, you transfer 5 USDC on Polygon to a recipient.
```js
const smartWalletAddress = await setupSmartWallet()
const customUserOperationResponse = await fetch(`${BASE_URL}/crypto/custom-user-operation`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': API_KEY,
},
body: JSON.stringify({
chainId: 137, // Polygon
walletAddress: smartWalletAddress,
payGasFeeToken: USDC_POLYGON, // token used to pay fees
operations: [
{
address: USDC_POLYGON, // ERC-20 token address
methodSignature: 'function transfer(address to, uint256 amount)',
arguments: [
'0x', // recipient
'5000000', // 5 USDC (6 decimals)
],
},
],
metadata: { useCase: 'custom-flow', requestId: 'req_123' },
}),
}).then((r) => r.json())
const { userOperation } = customUserOperationResponse
console.log('userOperationHash:', userOperation.userOperationHash)
```
- `payGasFeeToken` must be a currency the user's account holds; it pays partner fees and the network fee.
- To send a network's own currency (such as MATIC on Polygon), omit `methodSignature` and set only `address` (the recipient) and `value` (the amount in its smallest unit).
- `payGasFeeToken` must be a token the smart wallet holds; it pays partner fees and gas.
- For a native token transfer, omit `methodSignature` and set only `address` (recipient) and `value` (in wei).
**Example: Send the network's currency (MATIC)**
**Example: Native Token Transfer**
```js
const nativeTransferResponse = await fetch(`${BASE_URL}/crypto/custom-user-operation`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': API_KEY,
},
body: JSON.stringify({
chainId: 137, // Polygon
walletAddress: smartWalletAddress,
payGasFeeToken: USDC_POLYGON, // token used to pay gas fees (separate from the native token being transferred)
operations: [
{
address: '0xd8da6bf26964af9d7eed9e03e53415d37aa96045', // recipient address
value: '1000000000000000000', // 1 MATIC in wei (18 decimals)
},
],
metadata: { useCase: 'native-transfer', requestId: 'req_124' }, // optional metadata for tracking transactions
}),
}).then((r) => r.json())
```
### Execute the operation [#execute-the-operation]
Approve the returned `userOperationHash` with the user's key, then send the approval to `POST /crypto/execute-user-op`.
Sign the returned `userOperationHash` with the EOA, then queue execution.
```js
const signature = await account.signMessage({
message: { raw: userOperation.userOperationHash },
})
const executionResponse = await fetch(`${BASE_URL}/crypto/execute-user-op`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': API_KEY,
},
body: JSON.stringify({
userOperationHash: userOperation.userOperationHash,
signature,
}),
}).then((r) => r.json())
console.log(executionResponse)
```
If your wallet uses EIP-7702, include the authorization signature when executing.