Self-run agents
Routes for an agent that runs on its own machine with its own key (bring your own agent). The agent signs in with that key; the owner, signed in with a passkey or any Sui wallet key, claims the agent's setup code and creates its account. The SDK wraps both sides: EsseyService for the agent, EsseyOwner for the owner (Approve from a wallet key).
An agent session and an owner session never open each other's routes: an agent token on an owner route, or an owner token on an agent route, gets 401 NO_SESSION.
Agent sign-in#
{address} → {ok, message, expiresAtMs}, then POST /v1/agent/session {address, message, signature} → {ok, token, …}. The same two calls as Authentication, with a message of its own ("… agent wants you to sign in …"), so an owner challenge cannot be replayed here. Challenges are limited to 10 per minute per IP, counted apart from owner challenges.
Start a setup#
{side} → 201 {ok, code, side, expiresAtMs}. side is lender or borrower. The code is 32 hex digits, works once and expires after 15 minutes. Starting a new setup deletes this agent's unclaimed ones.
| Error | Status | When |
|---|---|---|
BAD_SIDE | 400 | side is not lender or borrower. |
DENIED | 403 | The address is on the operator's deny list. |
AGENT_EXISTS | 409 | An agent record already exists for this address. |
TRY_LATER | 429 | More than 5 setups in an hour from this address. |
Read the setup#
{ok, setup}Where setup is null or:
| Field | What it is |
|---|---|
| state | waiting, expired, claimed, ready or revoked. |
| side | lender or borrower. |
| expiresAtMs | When the code expires. |
| owner, agentId | Set once claimed. |
| account | Once ready: {treasuryId, agentCapId}, and cash: {treasuryId, capId} for a borrower. |
| capExpiresAtMs | When the agent's rules end. |
ready means the owner's create has landed and the rules are on chain. account is null while state is claimed. After POST /v1/byo/claim, poll this GET (or EsseyService.setup(), which is this GET) until state is ready. That is where treasuryId and agentCapId come from. A borrower also reads account.cash.treasuryId and account.cash.capId.
The borrower create form, sent by the owner as POST /v1/agents/:id/tx with { action: "create", form }. There is no top-level amount.
| Field | Type | Required | Default | Units | Validation |
|---|---|---|---|---|---|
| name | string | yes | — | — | ^[a-z0-9][a-z0-9-]{0,62}$. |
| preset | string | yes | — | — | taker, switcher or shopper. switcher needs a pool. |
| collateral | string | yes | — | SUI, ≤ 9 decimals | Above zero. |
| amount | string | yes | — | USDC, ≤ 6 decimals | Above zero, at most 100. |
| maxRateBps | number | yes | — | basis points a year | Integer 1–100000. |
| repayBeforeHours | number | yes | — | hours | Integer 0–720. The on-chain lead is at least 1 hour. |
| cashFloat | string | yes | — | USDC | "0" or positive. |
| expiresAtMs | number | yes | — | Unix ms | 62 minutes to 30 days ahead. |
| autoTopUp | boolean | no | off | — | Boolean when present. |
| topUpSui | string | no | "0" | SUI | "0" or positive. |
| autoClaim | boolean | no | on | — | Boolean when present. Only false turns it off. |
{
"action": "create",
"form": {
"name": "desk-taker",
"preset": "taker",
"collateral": "1",
"amount": "1",
"maxRateBps": 800,
"repayBeforeHours": 6,
"cashFloat": "0.05",
"expiresAtMs": 1793500000000,
"topUpSui": "0.1"
}
}autoTopUp and autoClaim are omitted here, so top-up is off and claim is on. Replace expiresAtMs with a time inside the window.
From the agent key, once setup.state is ready:
const agent = await EsseyAgent.start({
chain,
deployment,
signer: agentKey,
account: setup.account, // treasuryId, agentCapId, cash: { treasuryId, capId }
gas: "address-balance",
});
const SUI = 1_000_000_000n;
const USDC = 1_000_000n;
await agent.borrowFromOffer(offerId, 1n * SUI, 1n * USDC); // SUI treasury out, cash treasury in
await agent.borrowFromPool(1n * SUI, 1n * USDC); // NO_POOL when there is no pool
await agent.repay(loanId); // owed leaves the cash treasuryEsseyOwner is the owner's wallet-key client for claim and create. See Approve from a wallet key.
Peek and claim (owner session)#
{code} → {ok, address, side, expiresAtMs}. Shows which agent the code is for, without spending it. Compare address with the address the agent showed you.
{code, name, preset} → 201 {ok, agent}. Spends the code and creates the agent record for that address, owned by your session. The preset must match the setup's side. Then create the account with POST /v1/agents/:id/tx and action: "create" (Agent transactions).
| Error | Status | When |
|---|---|---|
BAD_CODE | 400 | Not 32 hex digits. |
WRONG_SIDE | 400 | A lender preset for a borrower setup, or the other way round (claim only). |
NO_SUCH_SETUP | 404 | Unknown, or already claimed. |
SETUP_EXPIRED | 410 | Older than 15 minutes. |
TRY_LATER | 429 | More than 20 peeks and claims in a minute from your address. |
A claim can also return the agent-creation errors of POST /v1/agents.
Sponsored transactions#
{bytes, signature} → {ok, digest}. The agent builds a transaction with the operator (selfRun.sponsor in GET /v1/config) as gas owner, a budget of at most selfRun.gasBudgetMax, signs it and sends it here; the operator co-signs and executes it. Only what the hosted agents' lending lane would sponsor is accepted. EsseyService.sponsored(client, tx, config) does all of this.
| Error | Status | When |
|---|---|---|
BAD_BODY | 400 | bytes or signature missing or not base64. |
NOT_A_SELF_RUN_AGENT | 403 | No self-run agent for this address. |
SENDER_NOT_SESSION | 403 | The transaction's sender is not the signed-in agent. |
KEYS_NOT_SEPARATE, OWNER_CAP_PUBLIC, OWNER_CAP_UNVERIFIABLE | 403 | The agent's OwnerCap is not with its owner. |
SPONSOR_REFUSED | 403 | Outside the lending lane; message says why. |
NOT_READY | 409 | The account is not set up yet. |
TRY_LATER | 429 | More than 20 a minute. |
DAILY_SPONSOR_LIMIT | 429 | More than 200 a day. |
TX_FAILED | 502 | The chain refused it; digest when it landed. |
Membership credential#
{commitment} → {ok, credential}: the dregg membership credential for the agent's own commitment, issued the same way as the owner's POST /v1/private/credential. Fields, errors and limits are in Private flows & credentials.