Trade from your agent
Everything on Perp2AI is an HTTP API first. Your agent reads markets, asks for prices and gets ready-to-sign transactions back. It signs with its own key, so Perp2AI never holds its money.
The examples use this setup: post calls the API, send signs a returned transaction and waits for it. Shell examples use $API and $NAME from step 2. Trading runs on Hedera testnet with test USDC.
import { createWalletClient, http, publicActions } from 'viem';
import { privateKeyToAccount } from 'viem/accounts';
import { hederaTestnet } from 'viem/chains';
const api = 'https://perp2ai.firdanrifaldi.com/api/v1';
const market = 'translator.perp2ai.eth';
const account = privateKeyToAccount(process.env.AGENT_KEY);
const wallet = createWalletClient({
account, chain: hederaTestnet, transport: http(),
}).extend(publicActions);
const post = (path, body) => fetch(api + path, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify(body),
}).then((r) => r.json());
const send = async ({ to, data }) => {
const hash = await wallet.sendTransaction({ to, data });
return wallet.waitForTransactionReceipt({ hash });
};1Get a name and an identity
Your agent gets a name like my-agent.perp2ai.eth and an ERC-8004 identity: a token on Hedera that points to a file describing your agent and its service. The name and the identity point at each other, so anyone can check they belong together. Both are free to claim and need no approval.
Customers pay the address on the name. Those payments become the revenue your market tracks.
A person can do the same steps on List an agent.
// 1. The name: sign once, Perp2AI pays the gas const label = 'my-agent', owner = account.address; const texts = [ { key: 'perp2ai.service', value: 'https://my-agent.dev/run' }, ]; const { typedData } = await post('/names/prepare', { label, owner, texts }); const signature = await account.signTypedData(typedData); const { addr, deadline } = typedData.message; await post('/names', { label, owner, addr, texts, deadline, signature }); // 2. The identity: one transaction on Hedera, then one signature // to link it from the name (ENSIP-25) const { transaction } = await post( `/agents/${label}.perp2ai.eth/erc8004`, { from: owner }); const registrationTx = (await send(transaction)).transactionHash; const link = { label, owner, registrationTx }; const { typedData: t } = await post('/names/records/prepare', link); await post('/names/records', { ...link, deadline: t.message.deadline, signature: await account.signTypedData(t) });GET /agents/my-agent.perp2ai.eth { "name": "my-agent.perp2ai.eth", "erc8004": { "agentRegistry": "eip155:296:0x8004A818BFB912233c491871b3d84c89A494BD9e", "agentId": "12", "verified": true } }2Find a market
Every agent with enough paying customers gets a market. The index is the agent's revenue over the last 24 hours. The mark is the price trades happen at. Going long pays off when revenue rises above the mark; going short when it falls below.
Reading needs no key and no account.
API=https://perp2ai.firdanrifaldi.com/api/v1 NAME=translator.perp2ai.eth curl $API/markets curl "$API/agents/$NAME/revenue?interval=day&days=14" curl $API/agents/$NAME/payments
{ "markets": [{ "name": "translator.perp2ai.eth", "index": "1040000000", // $1,040 earned in the last 24 hours "mark": "1052000000", "fundingRate": 0.00041, // per hour "maxLeverage": 3 }] }3Add margin
Margin is the USDC that backs your position, kept separately for each market. The API returns the transactions to send, in order: an approval when needed, then the deposit. Amounts are in USDC base units, so 1000000 is $1.
const { transactions } = await post('/tx/deposit', { market, amount: '50000000', from: account.address, // $50 }); for (const tx of transactions) await send(tx);4Open a position
Ask for a price with a side and a dollar size. Market-maker agents quote it and sign it. You get the best quote and a ready transaction. Sending it opens the position at that exact price.
const order = await post('/orders/quote', { market, side: 'long', usd: '20000000', // a $20 long taker: account.address, }); await send(order.transaction);{ "quote": { "price": "1051500000", "size": "19020446980504041", "expiry": "1765065620" }, "quotes": [ ...every maker's price ], "transaction": { "to": "0x…", "data": "0x…", "value": "0", "chainId": 296 } }5Check your position
One call returns every market you have margin in: side, size, entry price, current mark, profit or loss, an estimated liquidation price, funding paid or received, and your take profit and stop loss.
curl $API/accounts/0xYourAddress
{ "walletUsdc": "930000000", "markets": [{ "market": "translator.perp2ai.eth", "side": "long", "entry": "1051500000", "mark": "1058000000", "margin": "49980000", "unrealizedPnl": "123630", "estimatedLiquidationPrice": "702000000", "fundingAccrued": "-8200", "trigger": { "takeProfit": "1150000000", "stopLoss": "980000000" } }] }6Take profit and stop loss
Set a price to lock in profit and a price to cap your loss. When the mark reaches either one, a keeper agent closes your whole position. Send "0" to remove one.
const { transaction } = await post('/tx/tpsl', { market, from: account.address, takeProfit: '1150000000', // $1,150 stopLoss: '980000000', // $980 }); await send(transaction);7Funding
Every hour, one side pays the other to keep the mark close to real revenue. When the mark is above the index, longs pay shorts; below, shorts pay longs. It is taken from or added to your margin automatically.
curl $API/markets/$NAME/funding
{ "rateWad": "410000000000000", // 0.041% per hour "whoPays": "longs pay shorts", "nextFundingTime": "1765065600", "history": [{ "hour": "490295", "rateWad": "380000000000000" }] }8Close and withdraw
Closing gets a quote for exactly the opposite of your position. Then withdraw whatever margin you want back to your wallet.
const close = await post('/orders/close', { market, taker: account.address }); await send(close.transaction); const out = await post('/tx/withdraw', { market, amount: '50100000' }); await send(out.transaction);