Observed since 20 Oct, 17:40 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
0g-kit
A simple 2 liner for integrating any 0G infrastructure
A developer-friendly SDK that makes it easy to interact with the 0G decentralized AI network. Go from multiple lines of complex setup to just 2 lines of code and get any 0G infrastructure integrated into your app!
The 0G Kit abstracts away the following complex technical details:
• Broker Initialization - No need to manually create and configure the 0G Compute Network broker with ethers.js providers, wallets etc
• Provider Discovery - Automatic service discovery and provider selection instead of manually calling broker.inference.listService()
• Provider Acknowledgment - Automatic on-chain provider acknowledgment via broker.inference.acknowledgeProviderSigner()
• Service Metadata Retrieval - No need to manually fetch endpoint and model information with broker.inference.getServiceMetadata()
• Request Header Generation - Automatic generation of single-use authenticated headers via broker.inference.getRequestHeaders()
• HTTP Request Management - Handles all the fetch API calls, error handling, and response parsing
• Response Verification - Automatic response processing and verification for TEE-enabled services
• Fee Settlement - Automatic micropayment handling and fee settlement
• Account Management - Simplified balance checking, deposits, and withdrawals without direct ledger interactions
• Error Handling - Comprehensive error handling for network issues, insufficient funds, and provider failures
• Retry Logic - Built-in retry mechanisms for failed requests
• Timeout Management - Automatic request timeout handling and cleanup
• Indexer Initialization - No need to manually create and configure the 0G Storage indexer with ethers.js providers, wallets etc
• File Processing - Automatic file handling, Merkle tree generation, and buffer management
• Upload Management - Handles all upload operations, transaction signing, and blockchain interactions
• Download Management - Automatic file retrieval and verification using root hashes
• Gas Management - Automatic gas price optimization (5x default) and transaction fee handling
• Error Handling - Comprehensive error handling for network issues, file operations, and blockchain failures
• Retry Logic - Built-in retry mechanisms for failed uploads/downloads
• Timeout Management - Automatic operation timeout handling and cleanup
TLDR;
npm install 0g-kit⚠️ Important: For first time usage, it automatically deposits 0.05 OG to activate your broker account. Subsequent calls won't auto-deposit unless you explicitly call the deposit function!
// Compute-only imports
import { chat, useDeepseek, useLlama, useQwen } from '0g-kit';
// Storage-only imports
import { uploadFile, downloadFile, uploadFileWithGas, uploadKeyValueFile, downloadKeyValueFile, uploadStream, downloadStream } from '0g-kit/storage';
// AI Chat
import { chat } from '0g-kit';
const response = await chat('Hello, how are you?');
// Use DeepSeek model specifically
import { useDeepseek } from '0g-kit';
const response = await useDeepseek('Hello, how are you?')
// Use Llama model specifically
import { useLlama } from '0g-kit';
const response = await useLlama('Hello, how are you?')
// Use Qwen model specifically
import { useQwen } from '0g-kit';
const response = await useQwen('Hello, how are you?')
// Get list of available models and providers
import { getAvailableModels } from '0g-kit';
await getAvailableModels().then(console.log());
// Get detailed service information
import { listServices } from '0g-kit';
await listServices().then(console.log());
// Check balance
import { getBalance } from '0g-kit';
await getBalance().then(console.log());
// Deposit 0.5 0G from wallet to broker account
import { deposit } from '0g-kit';
await deposit(0.5);
// Withdraw 0.2 0G from your broker account to wallet
import { withdraw } from '0g-kit';
await withdraw(0.2);
import { initZeroG } from '0g-kit';
// Advanced setup with custom options
initZeroG({
privateKey: '0x1234567890abcdef...',
rpcUrl: 'https://evmrpc-testnet.0g.ai',
autoDeposit: true,
defaultModel: 'deepseek-chat',
timeout: 30000,
retries: 3,
logLevel: 'info'
});
| Option | Type | Default | Description |
|---|---|---|---|
model | string | auto | AI model to use |
provider | string | auto | Specific AI provider address |
temperature | number | 0.7 | Response creativity (0.0-1.0) |
maxTokens | number | 1000 | Maximum response length |
timeout | number | 30000 | Request timeout |
retries | number | 3 | Retry attempts |
// Upload a file (2 lines!)
import { uploadFile } from '0g-kit';
const result = await uploadFile('./my-file.txt');
console.log('Root Hash:', result.rootHash);
// Download a file (2 lines!)
import { downloadFile } from '0g-kit';
await downloadFile(result.rootHash, './downloaded-file.txt');
// Upload with custom gas price
import { uploadFileWithGas } from '0g-kit';
const result = await uploadFileWithGas('./my-file.txt', '10000000'); // 10 gwei
console.log('Root Hash:', result.rootHash);
// Upload with custom options
import { uploadFile } from '0g-kit';
const result = await uploadFile('./my-file.txt', {
timeout: 120000, // 2 minutes
retries: 5
});
// Get file information
import { getFileInfo } from '0g-kit';
const info = await getFileInfo(result.rootHash);
console.log('File size:', info.fileSize);
// Upload key-value data (2 lines!)
import { uploadKeyValueFile } from '0g-kit';
const result = await uploadKeyValueFile('my-stream', 'user-settings', JSON.stringify({theme: 'dark'}));
console.log('Transaction Hash:', result.txHash);
// Download key-value data (2 lines!)
import { downloadKeyValueFile } from '0g-kit';
const value = await downloadKeyValueFile('my-stream', 'user-settings');
const settings = JSON.parse(value);
console.log('User theme:', settings.theme);
// KV operations with custom options
import { uploadKeyValueFile } from '0g-kit';
const result = await uploadKeyValueFile('my-stream', 'config', 'value', {
timeout: 60000, // 1 minute
retries: 3
});
// Upload a stream (2 lines!)
import { uploadStream } from '0g-kit';
const result = await uploadStream(myReadableStream, { filename: 'data.txt' });
console.log('Transaction Hash:', result.txHash);
// Download as stream (2 lines!)
import { downloadStream } from '0g-kit';
const stream = await downloadStream(result.rootHash);
stream.pipe(fs.createWriteStream('output.txt'));
// Stream operations with custom options
import { uploadStream } from '0g-kit';
const result = await uploadStream(myReadableStream, {
filename: 'large-file.bin',
timeout: 300000, // 5 minutes
retries: 5
});
| Option | Type | Default | Description |
|---|---|---|---|
timeout | number | 300000 | Upload/download timeout in milliseconds |
retries | number | 3 | Number of retry attempts |
gasPrice | string | - | Custom gas price for upload transactions (in wei as string) |
| Option | Type | Default | Description |
|---|---|---|---|
timeout | number | 300000 | KV operation timeout in milliseconds |
retries | number | 3 | Number of retry attempts |
gasPrice | string | - | Custom gas price for KV transactions (in wei as string) |
| Option | Type | Default | Description |
|---|---|---|---|
timeout | number | 300000 | Stream operation timeout in milliseconds |
retries | number | 3 | Number of retry attempts |
gasPrice | string | - | Custom gas price for stream transactions (in wei as string) |
filename | string | stream-data | Optional filename for stream uploads |
// Default gas price (automatic)
const result = await uploadFile('./file.txt');
// Custom gas price via options
const result = await uploadFile('./file.txt', {
gasPrice: '10000000' // 10 gwei
});
// Direct gas price control
const result = await uploadFileWithGas('./file.txt', '10000000'); // 10 gwei
| Option | Type | Default | Description |
|---|---|---|---|
privateKey | string | required | Your EVM private key (0x-prefixed) |
rpcUrl | string | https://evmrpc-testnet.0g.ai | 0G network RPC endpoint |
indexerRpcUrl | string | https://indexer-storage-testnet-turbo.0g.ai | 0G storage indexer RPC endpoint |
autoDeposit | boolean | true | Automatically deposit 0.1 OG on initialization |
defaultModel | string | deepseek-chat | Default AI model to use |
timeout | number | 30000 | Request timeout in milliseconds |
retries | number | 3 | Number of retry attempts |
logLevel | string | info | Logging level (debug, info, warn, error, silent) |
The SDK provides clear error types:
import { ConfigurationError, NetworkError, InsufficientFundsError } from '0g-kit';
try {
const response = await chat('Hello');
} catch (error) {
if (error instanceof ConfigurationError) {
console.log('Configuration issue:', error.message);
} else if (error instanceof InsufficientFundsError) {
console.log('Need more funds:', error.message);
} else if (error instanceof NetworkError) {
console.log('Network issue:', error.message);
}
}
# Install dependencies
npm install
# Build the project
npm run build
# Run tests
npm test
# Development mode
npm run dev
Contributions are welcome! Please feel free to submit a Pull Request.
Download counts and popularity will appear here as the collector receives them.
| Package / target | Requirement | Relationship |
|---|---|---|
ethersEnvironment{
"local_name": "ethers",
"alias": false,
"peer_metadata": null
} | ^6.13.1 | runtime |
@0glabs/0g-serving-brokerEnvironment{
"local_name": "@0glabs/0g-serving-broker",
"alias": false,
"peer_metadata": null
} | ^0.3.1 | runtime |
Source distribution
{
"shasum": "99561c64a8cd3f8d9d97be0b78a28a90394623c0",
"tarball": "https://registry.npmjs.org/0g-kit/-/0g-kit-1.0.1.tgz",
"fileCount": 27,
"integrity": "sha512-jwLKlQ92oMaBxThUwDOJip1VymQDj/CEAZapMVt0/w8DuWVeP2eYDyvNRWZh7x75UWN3Rznq4un7UNn2pZQNsg==",
"signatures": [
{
"sig": "MEQCIFTDWqjZ8OIkoIvJuJIHSgNmzHqNgQHuXFHlk1pz8rv1AiAPsq93fafMWPrbxrHwhGpxaM2gv0RataxEt1n55GoS9g==",
"keyid": "SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"
}
],
"unpackedSize": 123796
}Observed since 20 Oct, 17:40 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
package_version:0755f0098b1604ff6e0c4e44ef91dfeaffea374786bf74f4ff3065c8eb637edb