DEV Community

Adeniji Adekunle James
Adeniji Adekunle James

Posted on

πŸš€ Uploading Files to Anthropic AI Using Node.js

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);

Enter fullscreen mode Exit fullscreen mode

πŸ” 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.

Top comments (0)