Working with LLMs sometimes means needing to pass custom files whether it’s a dataset, a PDF, or logs—to your model. Anthropic makes this easy with their file upload API, and here’s how you can do it in just a few lines of Node.js!
import fs from 'fs';
import Anthropic from '@anthropic-ai/sdk';
// Initialize the Anthropic client
const client = new Anthropic();
async function main() {
// Upload a file to Anthropic
const file = await client.beta.files.upload({
file: fs.createReadStream('document.pdf'),
});
console.log('📁 File uploaded with ID:', file.id);
}
main().catch(console.error);
🔍 What’s happening here?
fs.createReadStream(‚document.pdf‘) reads the file from your local disk.
client.beta.files.upload() sends it to Anthropic’s servers.
Once uploaded, the returned file.id can be used to reference this file in other API calls.
✅ Use Cases
Augment Claude with private documents.
Enable retrieval-augmented generation (RAG) workflows.
Build smarter assistants with contextual file awareness.