@ai42/sdk
v0.1.4
Published
<div align="center">
Readme
AI42 SDK
True Pay-per-use AI API with automatic crypto payments via HTTP 402
Documentation • Installation • Quick Start • Examples
Overview
AI42 SDK provides seamless access to multiple AI models with automatic cryptocurrency payments using the X402 protocol. Pay only for what you use - no subscriptions, no API keys, just connect your wallet or use a private key.
✨ Features
- 🤖 Multiple AI Models: Access Llama, GPT, and Gemini models
- 💰 Pay-per-use: Automatic payments via Solana/Base using HTTP 402
- 🌐 Universal: Works in browser (Phantom wallet) and Node.js (private key)
- ⚡ Priority Options: Choose between fast, quality, or cheap responses
- 🔒 Type-safe: Full TypeScript support with comprehensive types
- 🎯 Simple API: Intuitive methods for common use cases
🎯 Supported Models
| Model | Provider | Identifier |
| ---------------- | -------- | ------------------------- |
| Llama 3.3 70B | Groq | llama-3.3-70b-versatile |
| GPT OSS 120B | OpenAI | openai/gpt-oss-120b |
| Gemini 2.5 Flash | Google | gemini-2.5-flash |
| Gemini 2.5 Pro | Google | gemini-2.5-pro |
Installation
npm install @ai42/sdkCore Peer Dependencies
For Browser:
npm install x402-solanaFor Node.js:
npm install x402-fetch dotenvQuick Start
Browser Usage (Phantom Wallet)
import { AI42Client } from "@ai42/sdk";
import { connectWallet } from "@ai42/sdk/wallet";
// Connect wallet
const wallet = await connectWallet();
// Create client
const client = AI42Client.fromWallet(
{
apiUrl: "https://ai42.onrender.com",
network: "solana-devnet",
},
wallet
);
// Send a message
const response = await client.chat({
message: "Explain quantum computing",
model: "llama-3.3-70b-versatile",
});
console.log(response.content);
console.log(`Cost: $${response.cost}`);Node.js Usage (Signer/Private Key)
import { AI42Client } from "@ai42/sdk";
import { config } from "dotenv";
config();
// Create client with signer/private key
const signer = await createSigner(config.network, privateKey);
const client = await AI42Client.fromSigner(
{
apiUrl: "https://ai42.onrender.com",
network: "solana-devnet",
},
signer
);
// Send a message
const response = await client.chat({
message: "Write a haiku about AI",
priority: "fast",
});
console.log(response.content);Documentation
Client Creation
Browser with Wallet
AI42Client.fromWallet(config: AI42Config, wallet: Wallet): AI42ClientNode.js with Private Key
AI42Client.fromSigner(config: AI42Config, signer: Signer): Promise<AI42Client>Configuration
interface AI42Config {
apiUrl: string;
network?: "solana-mainnet" | "solana-devnet" | "base-sepolia";
}Chat Methods
Basic Chat
client.chat(options: AI42ChatOptions): Promise<ChatResponse>interface AI42ChatOptions {
message: string;
model?: ModelIdentifier;
priority?: "fast" | "quality" | "cheap";
}Chat with Specific Model
client.chatWithModel(
message: string,
model: ModelIdentifier
): Promise<ChatResponse>Chat with Priority
client.chatWithPriority(
message: string,
priority: 'fast' | 'quality' | 'cheap'
): Promise<ChatResponse>Response Format
interface ChatResponse {
content: string; // AI response text
model: ModelIdentifier; // Model used
tokens: {
prompt: number; // Input tokens
completion: number; // Output tokens
total: number; // Total tokens
};
cost: number; // Cost in USD
cached: boolean; // Whether response was cached
requestId: string; // Unique request ID
timestamp: number; // Unix timestamp
}Examples
Example 1: Simple Question
const response = await client.chat({
message: "What is the capital of France?",
});
console.log(response.content); // "Paris is the capital of France..."Example 2: Code Generation
const response = await client.chatWithModel(
"Write a function to calculate fibonacci numbers",
"llama-3.3-70b-versatile"
);
console.log(response.content);Example 3: Fast Response
const response = await client.chatWithPriority(
"Summarize the news today",
"fast"
);
console.log(response.content);
console.log(`Responded in ${response.tokens.total} tokens`);Example 4: Quality over Speed
const response = await client.chat({
message: "Explain the theory of relativity in detail",
priority: "quality",
model: "gemini-2.5-pro",
});
console.log(response.content);Example 5: Error Handling
import { AI42Error } from "@ai42/sdk";
try {
const response = await client.chat({
message: "Hello AI",
});
console.log(response.content);
} catch (error) {
if (error instanceof AI42Error) {
console.error(`Error [${error.code}]: ${error.message}`);
if (error.code === "PAYMENT_FAILED") {
console.error("Check your wallet balance");
}
}
}Example 6: Browser Integration
<!DOCTYPE html>
<html>
<head>
<title>AI42 Chat</title>
</head>
<body>
<button id="connect">Connect Wallet</button>
<input id="message" placeholder="Ask me anything..." />
<button id="send">Send</button>
<div id="response"></div>
<script type="module">
import { AI42Client } from "@ai42/sdk";
import { connectWallet } from "@ai42/sdk/wallet";
let client;
document.getElementById("connect").onclick = async () => {
const wallet = await connectWallet();
client = AI42Client.fromWallet(
{ apiUrl: "https://ai42.onrender.com" },
wallet
);
alert("Wallet connected!");
};
document.getElementById("send").onclick = async () => {
const message = document.getElementById("message").value;
const response = await client.chat({ message });
document.getElementById("response").innerText = response.content;
};
</script>
</body>
</html>Environment Setup
Node.js .env File
PRIVATE_KEY=your_solana_or_base_private_key_here
API_URL=https://api.ai42.dev
NETWORK=solana-devnetGetting a Private Key
Solana:
solana-keygen new --outfile ~/.config/solana/id.json
solana-keygen pubkey ~/.config/solana/id.jsonBase (Ethereum): Use MetaMask or any Ethereum wallet to export your private key.
⚠️ Never commit private keys to version control!
Error Handling
The SDK throws AI42Error with specific error codes:
| Error Code | Description |
| ------------------ | --------------------------------------- |
| INVALID_REQUEST | Malformed request or invalid parameters |
| PAYMENT_REQUIRED | Payment needed to proceed |
| PAYMENT_FAILED | Payment transaction failed |
| MODEL_ERROR | AI model returned an error |
| RATE_LIMIT | Too many requests |
| INTERNAL_ERROR | Unexpected error occurred |
try {
await client.chat({ message: "Hello" });
} catch (error) {
if (error instanceof AI42Error) {
switch (error.code) {
case "PAYMENT_FAILED":
console.error("Payment issue:", error.message);
break;
case "MODEL_ERROR":
console.error("AI model error:", error.message);
break;
default:
console.error("Error:", error.message);
}
}
}Priority Guidelines
| Priority | Use Case | Speed | Cost | Quality |
| --------- | ----------------------------------- | ------ | ---- | ------- |
| fast | Quick responses, simple queries | ⚡⚡⚡ | $ | ⭐⭐ |
| quality | Complex reasoning, detailed answers | ⚡ | $$$ | ⭐⭐⭐ |
| cheap | Budget-conscious, simple tasks | ⚡⚡ | $ | ⭐⭐ |
Network Support
| Network | Chain | Mainnet | Testnet |
| ------- | ----------- | ---------------- | --------------- |
| Solana | Solana | solana-mainnet | solana-devnet |
| Base | Ethereum L2 | base-mainnet | base-sepolia |
TypeScript Support
Full TypeScript definitions included:
import type {
AI42Config,
AI42ChatOptions,
ChatResponse,
ModelIdentifier,
Priority,
PaymentInfo,
AI42Error,
} from "@ai42/sdk";FAQ
Q: Do I need an API key?
A: No! AI42 uses the X402 protocol for automatic payments. Just connect your wallet or provide a private key.
Q: What cryptocurrencies are supported?
A: Currently Solana (SOL/USDC) and Base (ETH/USDC).
Q: How much does it cost?
A: Pay-per-use pricing based on tokens consumed. Prices vary by model and priority. Check response.cost for exact costs.
Q: Can I use this in production?
A: Yes! Use solana-mainnet or base-mainnet for production. Start with testnets for development.
Q: Is my private key safe?
A: Your private key never leaves your machine. It's only used to sign payment transactions locally.
Q: What if a payment fails?
A: The SDK will throw a PAYMENT_FAILED error. Check your wallet balance and network connectivity.
Contributing
Contributions welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Feat: Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
MIT License - see LICENSE file for details
Support
- 🐦 Twitter: @ai42_dev
- 📖 Docs: docs.ai42.dev
Acknowledgments
Built with:
- X402 Protocol - HTTP 402 payment standard
- x402-solana - Solana payment client
- x402-fetch - Node.js payment wrapper
Made with ❤️ by the AI42 Team
