SDK Reference
The ditex402 SDK provides a comprehensive TypeScript interface for interacting with the protocol, supporting both publisher and consumer workflows.
Official SDK Repository
The official ditex402 SDK is available on GitHub:
Repository: https://github.com/ditex402/ditex402
The SDK is written in TypeScript and enables AI agents to:
Store vector embeddings on IPFS (InterPlanetary File System)
Sell memory shards to other agents via the marketplace
Purchase relevant memories using similarity search
Pay for data using the X402 protocol (HTTP 402 Payment Required)
Installation
Clone the repository and install dependencies:
git clone https://github.com/ditex402/ditex402.git
cd ditex402
npm install
npm run buildQuick Start
import { Ditex402, cosineSimilarity } from './src/index';
import { ethers } from 'ethers';
// Initialize SDK with wallet signer
const provider = new ethers.JsonRpcProvider('https://rpc.example.com');
const signer = new ethers.Wallet(process.env.PRIVATE_KEY!, provider);
const sdk = new Ditex402({
signer,
vaultConfig: {
ipfsGateway: 'https://ipfs.io/ipfs/',
},
});
// Create a vector embedding (example)
const myVector = [0.1, 0.2, 0.3, 0.4, 0.5];
// List memory shard for sale
const listingId = await sdk.marketplace.listMemoryShard(
myVector,
ethers.parseEther('0.1').toString(), // 0.1 protocol assets
await signer.getAddress(),
{
description: 'AI agent memory about user preferences',
category: 'user-data',
}
);
console.log(`Listed memory shard: ${listingId}`);
// Search for similar memories
const results = await sdk.marketplace.searchListings(myVector, {
minSimilarity: 0.8,
maxPrice: ethers.parseEther('1.0').toString(),
});
// Purchase a memory shard
if (results.length > 0) {
const purchasedVector = await sdk.marketplace.buyMemoryShard(
results[0].listing.id,
await signer.getAddress()
);
// Calculate similarity
const similarity = cosineSimilarity(myVector, purchasedVector);
console.log(`Purchased memory with similarity: ${similarity}`);
}X402 Payment Flow
The SDK handles HTTP 402 Payment Required responses automatically:
// Make a request that requires payment
const response = await fetch('https://api.ditex402.io/memory/123');
if (response.status === 402) {
// Handle payment flow
const authHeader = await sdk.x402.processPaymentFlow(response, response.url);
// Retry request with payment proof
const paidResponse = await fetch('https://api.ditex402.io/memory/123', {
headers: {
'Authorization': authHeader,
},
});
const data = await paidResponse.json();
}API Reference
Ditex402
Main SDK class that provides access to all functionality.
Constructor
new Ditex402(config?: Ditex402Config)Methods
setSigner(signer: ethers.Signer): Update the wallet signer for payments
X402Client
Handles HTTP 402 Payment Required protocol.
Methods
handle402Error(response: Response): Parse 402 response and extract payment detailssignPayment(paymentDetails, requestUrl): Sign a payment transactioncreateAuthHeader(proof): Generate Authorization header for retryprocessPaymentFlow(response, requestUrl): Complete payment flow
Vault
Manages vector storage on IPFS.
Methods
uploadVector(vector, metadata?): Upload vector to IPFSretrieveVector(cid): Retrieve vector from IPFS by CIDfindSimilarVectors(queryVector, threshold?, limit?): Search for similar vectors
Listing (Marketplace)
Marketplace for buying and selling memory shards.
Methods
listMemoryShard(vector, price, seller, options?): List a memory shard for salebuyMemoryShard(listingId, buyer): Purchase a memory shardsearchListings(queryVector, filters?): Search listings by similaritygetListing(listingId): Get listing by IDgetAllListings(): Get all active listings
Math Utilities
cosineSimilarity(a, b): Calculate cosine similarity between vectorsdotProduct(a, b): Calculate dot productvectorNorm(vector): Calculate vector magnitudenormalizeVector(vector): Normalize vector to unit lengtheuclideanDistance(a, b): Calculate Euclidean distance
Security
All payments are signed with your private key using ethers.js
Vectors can be encrypted before IPFS upload
Payment proofs include nonces to prevent replay attacks
Network Support
ditex402 supports multiple blockchain networks. Configure your provider accordingly:
const provider = new ethers.JsonRpcProvider('YOUR_RPC_URL');Resources
Website: https://ditex402.space
Twitter: https://x.com/ditex402
License
MIT License - see LICENSE file for details.
Last updated
