SDK
Get started with the SDK in 5 minutes
Get up and running with the EasyRAG SDK in 5 minutes.
bashnpm install @easyrag/sdk
javascriptimport { EasyRAG } from '@easyrag/sdk'; const client = new EasyRAG(process.env.EASYRAG_API_KEY);
⚠️ Keep your API key secure - always use environment variables, never hardcode.
javascript// Single file const file = new File(['content'], 'document.pdf', { type: 'application/pdf' }); const upload = await client.upload('my-dataset', file); console.log('Uploaded:', upload.files[0].fileId); // Multiple files await client.upload('my-dataset', [file1, file2, file3]);
javascriptconst results = await client.search('my-dataset', 'What is the refund policy?'); results.data.forEach(result => { console.log(`Score: ${result.score}`); console.log(`Content: ${result.pageContent}`); console.log(`From: ${result.metadata.originalName}`); });
javascriptconst answer = await client.query('my-dataset', 'Summarize the key points'); console.log(answer.data.result);
You now know the basics:
javascriptawait client.upload('dataset', file, { metadata: { 'document.pdf': { userId: 'user_123', department: 'legal', year: 2024 } } });
javascriptconst results = await client.search('dataset', 'contract terms', { filters: [ { key: 'department', match: { value: 'legal' } }, { key: 'year', match: { value: 2024 } } ] });
javascriptfor await (const chunk of client.queryStream('dataset', 'Explain the features')) { if (chunk.delta) { process.stdout.write(chunk.delta); } else if (chunk.done) { console.log('\nComplete!'); } }
javascript// List files const { files } = await client.listFiles('dataset'); console.log(`Total: ${files.length} files`); // Get file details const { file } = await client.getFile('dataset', fileId); console.log('Download:', file.permanentUrl); // Delete file await client.deleteFile('dataset', fileId);
javascriptimport { EasyRAGError } from '@easyrag/sdk'; try { await client.upload('dataset', file); } catch (error) { if (error instanceof EasyRAGError) { if (error.status === 402) { console.log('Out of credits!'); } else { console.log(`Error: ${error.message}`); } } }
The SDK has full TypeScript support:
typescriptimport { EasyRAG, type SearchResponse } from '@easyrag/sdk'; const client = new EasyRAG(process.env.EASYRAG_API_KEY!); const results: SearchResponse = await client.search('dataset', 'query');
Never expose API keys in the frontend. Use tokens:
Backend (Node.js):
javascript// Generate token for user's dataset const { token } = await client.createToken('user-dataset', { ttlSeconds: 3600 }); // Send to frontend res.json({ token });
Frontend (React/Browser):
javascript// Get token from your backend const { token } = await fetch('/api/token').then(r => r.json()); // Use token with SDK const client = new EasyRAG(token); await client.search('user-dataset', 'query');
javascriptimport { EasyRAG } from '@easyrag/sdk'; const client = new EasyRAG(process.env.EASYRAG_API_KEY); async function main() { try { // Upload const file = new File(['Hello world'], 'test.txt', { type: 'text/plain' }); const upload = await client.upload('test-dataset', file); console.log('✅ Uploaded:', upload.files[0].fileId); // Search const results = await client.search('test-dataset', 'hello'); console.log('✅ Found:', results.data.length, 'results'); // Query const answer = await client.query('test-dataset', 'What does it say?'); console.log('✅ Answer:', answer.data.result); // Clean up await client.deleteDataset('test-dataset'); console.log('✅ Cleaned up'); } catch (error) { console.error('❌ Error:', error.message); } } main();
Make sure you've installed the SDK:
bashnpm install @easyrag/sdk
Check that your API key:
sk_Check your credit balance in the dashboard. Each upload costs 1 credit, queries cost 0.1 credit.
Verify:
datasetId is provided