Invoking from Solidity
This guide shows how to invoke Somnia Agents from smart contracts. Your contracts can call agents and receive responses via callbacks.
Generate Code from the Web App
The easiest way to get started is to use the code generator:
Select an agent from the list
Click on a method to expand it
Select the "Solidity" tab to see generated code for that method
Copy the generated code into your project
The generator creates a complete contract with:
Platform interface definitions
Agent method encoding
Callback handling
Proper error handling
Tip: The "TypeScript" tab generates code for invoking agents from JavaScript/TypeScript — it sends transactions to the platform contract and listens for response events.
How It Works
Your contract calls
createRequest()with the agent ID, encoded payload, and a deposit.The platform splits the deposit into an operations reserve (
minPerAgentDeposit × subSize, funds gas refunds / callback / upkeep) and an agent reward pot (the rest).perAgentBudget = rewardPot / subSizeis emitted inRequestCreatedso runners know their cap.Elected validators execute the agent and submit responses; each submission is gas-refunded from the operations reserve.
Once consensus is reached, the platform calls your callback, then pays every elected subcommittee member the median of the reported
executionCostvalues via the committee.Any unused funds are rebated to the requester.
See Gas Fees for a full fund-flow diagram and details on sizing msg.value.
Platform Interface
SomniaAgents / AgentRequester Addresses:
Mainnet (chain ID 5031)
0x5E5205CF39E766118C01636bED000A54D93163E6
Testnet (chain ID 50312)
0x037Bb9C718F3f7fe5eCBDB0b600D607b52706776
The examples below use the testnet address; swap in the mainnet address for production.
Basic Example
Callback Function Signature
Your callback function must match this signature:
requestId: The ID returned fromcreateRequest()responses: Array ofResponsestructs from validators (each contains.resultbytes,.status,.validator, etc.)status: The overall request status — one ofSuccess(2),Failed(3), orTimedOut(4)details: The fullRequeststruct with metadata (subcommittee, threshold, deadline, etc.)
You can name the function anything, but the parameter types must match. The selector you pass to createRequest must match your function.
Response Status
None
0
Default zero value (uninitialized storage)
Pending
1
Awaiting responses
Success
2
Validators reached consensus
Failed
3
Validators reported failure (success became impossible)
TimedOut
4
Request timed out before consensus was reached
Decoding Responses
The responses array contains Response structs from validators. Each response has a .result field with ABI-encoded bytes and a .status field. The callback receives all responses submitted up to finalization. For Majority consensus, at least threshold responses will have identical .result bytes (the consensus value). For Threshold consensus, results may differ.
Multiple Callbacks
Use different callbacks for different response types:
Error Handling
Handle all response statuses gracefully:
Security Considerations
Always verify the caller: Check that
msg.senderis the platform contractTrack pending requests: Use a mapping to validate incoming callbacks
Handle all statuses: Check
status— requests can succeed, fail, or time outValidate responses length: Check
responses.lengthbefore decodingAccept rebates: Implement
receive()so your contract can receive rebated funds
Next Steps
Gas Fees — Understand costs
Last updated