Overview
StudyFetch supports multiple methods for uploading materials:
- Direct file upload - Upload files directly in the request
- Upload and process - Upload files and wait for processing to complete
- URL import - Import content from web URLs
- Presigned URL upload - Upload large files directly to S3
- Create and process - Create text materials and wait for processing to complete
Processing Time: After uploading, materials require processing time before they can be used. Materials will have status: 'processing'
initially and must reach status: 'active'
before they can be used in components or for grading. Processing time varies based on file size and type (typically 5-30 seconds for PDFs, longer for videos).Use the uploadFileAndProcess
or createAndProcess
methods if you need to use the material immediately after creation - they wait for processing to complete before returning.
Direct File Upload
Upload files directly to the API. Best for files under 10MB.
import StudyfetchSDK from '@studyfetch/sdk';
import fs from 'fs';
import path from 'path';
const client = new StudyfetchSDK({
apiKey: 'your-api-key',
baseURL: 'https://studyfetchapi.com',
});
const fileBuffer = await fs.promises.readFile(
path.join(process.cwd(), 'biology-notes.pdf'),
);
const file = new File([fileBuffer], 'biology-notes.pdf', {
type: 'application/pdf',
});
const material = await client.v1.materials.upload.uploadFile({
file,
name: 'Biology Chapter 1 Notes',
folderId: 'folder_123' // Optional
});
console.log('Material uploaded:', material._id);
console.log('Status:', material.status); // 'processing'
Upload and Process File
Upload a file and wait for processing to complete before returning. This is useful when you need to use the material immediately after upload.
Synchronous Processing: Unlike the regular uploadFile
method which returns immediately while processing happens in the background, uploadFileAndProcess
waits until the material is fully processed and ready to use before returning.
import StudyfetchSDK from '@studyfetch/sdk';
import fs from 'fs';
import path from 'path';
const client = new StudyfetchSDK({
apiKey: 'your-api-key',
baseURL: 'https://studyfetchapi.com',
});
const fileBuffer = await fs.promises.readFile(
path.join(process.cwd(), 'biology-notes.pdf'),
);
const file = new File([fileBuffer], 'biology-notes.pdf', {
type: 'application/pdf',
});
// Upload and wait for processing to complete
const material = await client.v1.materials.upload.uploadFileAndProcess({
file,
name: 'Biology Chapter 1 Notes',
folderId: 'folder_123' // Optional
});
// Material is ready to use immediately
console.log('Material uploaded and processed:', material._id);
console.log('Status:', material.status); // 'active'
Upload from URL
Import content directly from web URLs. Supports documents, videos, and web pages.
const material = await client.v1.materials.upload.uploadFromURL({
url: 'https://example.com/biology-textbook.pdf',
name: 'Biology Textbook',
folderId: 'folder_123' // Optional
});
console.log('Material imported:', material._id);
Upload Text Content
Create materials from plain text content without file upload.
const material = await client.v1.materials.create({
name: 'Biology Notes',
content: {
type: 'text',
text: 'Cell Structure: The cell is the basic unit of life...'
},
folderId: 'folder_123', // Optional
references: [
{
title: 'Introduction to Biology Textbook',
url: 'https://example.com/textbook'
}
] // Optional: source references
});
console.log('Text material created:', material._id);
Create and Process Material
The createAndProcess
method creates a material and waits for it to be fully processed before returning. This is useful when you need to use the material immediately after creation.
Synchronous Processing: Unlike the regular create
method which returns immediately while processing happens in the background, createAndProcess
waits until the material is fully processed and ready to use before returning.
import StudyfetchSDK from '@studyfetch/sdk';
const client = new StudyfetchSDK({
apiKey: 'your-api-key',
});
// Create and wait for processing to complete
const material = await client.v1.materials.createAndProcess({
content: {
type: 'text',
text: 'Chapter 1: Introduction to Biology...'
},
name: 'Chapter 1 - Introduction',
references: [
{
title: 'Biology Course Materials',
url: 'https://example.com/course-materials'
}
] // Optional: source references
});
// Material is ready to use immediately
console.log('Material ID:', material._id);
console.log('Status:', material.status); // 'active'
File Type Examples
PDF Documents
const fileBuffer = await fs.promises.readFile(
path.join(process.cwd(), 'textbook.pdf'),
);
const file = new File([fileBuffer], 'textbook.pdf', {
type: 'application/pdf',
});
const material = await client.v1.materials.upload.uploadFile({
file,
name: 'Biology Textbook'
});
Audio Files (MP3, WAV)
const fileBuffer = await fs.promises.readFile(
path.join(process.cwd(), 'podcast.mp3'),
);
const file = new File([fileBuffer], 'podcast.mp3', {
type: 'audio/mpeg',
});
const material = await client.v1.materials.upload.uploadFile({
file,
name: 'Biology Podcast Episode 1'
});
EPUB Books
// EPUB files are automatically processed into chapters
const fileBuffer = await fs.promises.readFile(
path.join(process.cwd(), 'textbook.epub'),
);
const file = new File([fileBuffer], 'textbook.epub', {
type: 'application/epub+zip',
});
const material = await client.v1.materials.upload.uploadFile({
file,
name: 'Digital Textbook'
});
Processing Status
Check material status:
const material = await client.v1.materials.retrieve(materialId);
if (material.status === 'active') {
console.log('Processing complete!');
} else if (material.status === 'processing') {
console.log('Still processing...');
} else if (material.status === 'error') {
console.log('Processing failed');
}