This repository contains example implementations for invoking Somnia Agents from:
- Solidity, using a callback-based contract flow
- JavaScript/TypeScript, using direct request submission and response reads
The example app is a marketplace for new and used goods where an agent reviews a listing price and classifies it as:
cheapernormaloverpriced
The example is built around Somnia's ExtractString agent method.
The core flow is:
- A user creates a marketplace listing.
- The app submits an agent request to review the listing price against a trusted marketplace source.
- The request is linked to the listing with
requestId. - Once the agent response is confirmed, the listing is updated with the final assessment.
The Solidity example uses a platform callback. The Node example shows direct agent invocation from TypeScript.
- Platform contract:
0x037Bb9C718F3f7fe5eCBDB0b600D607b52706776 - Agent ID:
12875401142070969085
contracts/Hardhat workspace for Solidity contracts and deployment scripts.node/TypeScript workspace for direct agent invocation examples.frontend/Placeholder frontend workspace.
Main contract:
Reusable modules:
contracts/contracts/types/AgentTypes.solcontracts/contracts/interfaces/IAgent.solcontracts/contracts/interfaces/IAgentRequester.solcontracts/contracts/interfaces/IERC20.solcontracts/contracts/libraries/AgentCodec.sol
The marketplace contract:
- creates listings for
NewandUseditems - settles purchases in USDC
- submits a price-review request to a Somnia Agent
- links
requestIdtolistingId - updates the listing in the callback once the platform confirms the result
To reduce seller-controlled price manipulation, the contract does not accept a custom marketplace URL from the seller.
Instead, it selects a trusted source by category:
electronics,phones,laptops,gaming-> Swappainstruments,music,guitars-> Reverb Price Guidesneakers,streetwear-> StockX- everything else -> eBay completed listings
createListing(...)stores the listing.requestListingReview(...)builds anExtractStringpayload.- The contract calls
createRequest(...)on the Somnia platform. requestIdis mapped tolistingId.handleResponse(...)decodes the first string result and updates the listing assessment.
Listing prices are expected in USDC base units.
Examples:
1 USDC->1_00000025 USDC->25_000000450 USDC->450_000000
The Somnia request deposit is still paid in the chain's native token.
Main files:
The TypeScript example:
- encodes an
ExtractStringrequest - calculates the required deposit
- sends a request to the platform contract
- waits for
RequestCreated - waits for
RequestFinalized - reads and decodes the response
The current example in node/src/index.ts checks a used electronics listing against Swappa.
It submits a request roughly equivalent to:
await invokeWebAgent(
"price_assessment",
"Classify whether the listing price is cheaper, normal, or overpriced compared to similar used marketplace listings.",
["cheaper", "normal", "overpriced"],
"Review this used electronics listing: 'Used iPhone 13 128GB'. The seller is asking 450000000 USDC base units (450 USDC). Compare it against similar offers on the marketplace page and return exactly one of: cheaper, normal, overpriced.",
"https://swappa.com/buy/phones",
true,
1,
80,
)- Encode the
ExtractStringpayload. - Read
getRequestDeposit(). - Add execution budget on top of the reserve.
- Submit
createRequest(...). - Wait for
RequestCreated. - Wait for
RequestFinalized. - Read the request state and decode the successful response.
cd contracts
npm installUseful commands:
npx hardhat test
npx hardhat run scripts/deploy-marketplace.ts --network <network> -- <usdc-token-address>Deployment script:
cd node
npm install
cp .env.example .envSet PRIVATE_KEY in .env.
Useful commands:
npm run typecheck
npm run build
npm start- The Node example currently demonstrates direct invocation and response handling outside the Solidity callback flow.
- The Solidity example is better for agent-driven application design where request confirmation should update onchain state automatically.
- The reusable Solidity types, interfaces, and codec library are intended to make future agent-powered contracts easier to build.
- add frontend listing and review UI
- add contract tests for callback flows
- add network-specific deployment notes
- expand the examples into a full production application