Skip to content

Getting Started

This guide will walk you through setting up Torrin in your project, from installation to your first upload.

Installation

Torrin is split into separate packages for modularity. Choose the packages you need:

Server Packages

Install the core server package, a framework integration, and a storage driver:

bash
npm install @torrin-kit/server @torrin-kit/server-express @torrin-kit/storage-local
bash
yarn add @torrin-kit/server @torrin-kit/server-express @torrin-kit/storage-local
bash
pnpm add @torrin-kit/server @torrin-kit/server-express @torrin-kit/storage-local
bash
bun add @torrin-kit/server @torrin-kit/server-express @torrin-kit/storage-local

Available integrations:

  • @torrin-kit/server-express - Express.js middleware
  • @torrin-kit/server-nestjs - NestJS module

Available storage drivers:

  • @torrin-kit/storage-local - Local filesystem storage
  • @torrin-kit/storage-s3 - AWS S3 and S3-compatible storage (requires @aws-sdk/client-s3)

Client Package

bash
npm install @torrin-kit/client
bash
yarn add @torrin-kit/client
bash
pnpm add @torrin-kit/client
bash
bun add @torrin-kit/client

Quick Start

1. Set up the Server

Create an endpoint that handles upload operations:

typescript
import express from 'express';
import { createTorrinExpressRouter } from '@torrin-kit/server-express';
import { createLocalStorageDriver } from '@torrin-kit/storage-local';
import { createInMemoryStore } from '@torrin-kit/server';

const app = express();

// Important: Parse JSON bodies for upload metadata
app.use(express.json());

// Create Torrin router
const torrinRouter = createTorrinExpressRouter({
  storage: createLocalStorageDriver({ 
    baseDir: './uploads' 
  }),
  store: createInMemoryStore(),
});

// Mount at any path you prefer
app.use('/api/uploads', torrinRouter);

app.listen(3000, () => {
  console.log('Server running on http://localhost:3000');
});
typescript
import { Module } from '@nestjs/common';
import { TorrinModule } from '@torrin-kit/server-nestjs';
import { createLocalStorageDriver } from '@torrin-kit/storage-local';
import { createInMemoryStore } from '@torrin-kit/server';

@Module({
  imports: [
    TorrinModule.forRoot({
      storage: createLocalStorageDriver({ 
        baseDir: './uploads' 
      }),
      store: createInMemoryStore(),
    }),
  ],
})
export class AppModule {}

2. Set up the Client

Initialize the client pointing to your server endpoint:

typescript
import { createTorrinClient } from '@torrin-kit/client';

const torrin = createTorrinClient({
  endpoint: 'http://localhost:3000/api/uploads',
});

3. Upload a File

Create an upload and start the transfer:

typescript
// Get file from input
const fileInput = document.querySelector<HTMLInputElement>('#file-input');
const file = fileInput.files[0];

// Create upload
const upload = torrin.createUpload({ file });

// Listen to progress
upload.on('progress', (progress) => {
  console.log(`${progress.percentage}% complete`);
  console.log(`${progress.chunksCompleted}/${progress.totalChunks} chunks uploaded`);
});

// Listen to status changes
upload.on('status', (status) => {
  console.log('Status:', status); // idle, uploading, paused, completed, etc.
});

// Handle errors
upload.on('error', (error) => {
  console.error('Upload failed:', error.message);
});

// Start the upload
try {
  const result = await upload.start();
  console.log('Upload complete!');
  console.log('File location:', result.location);
} catch (error) {
  console.error('Upload failed:', error);
}

Next Steps

Now that you have a basic upload working, explore more features:

  • Frontend Guides: Learn framework-specific implementations

    • React - Hooks and components
    • Vue - Composition API examples
    • Svelte - Reactive stores
  • Backend Guides: Deep dive into server configuration

  • Core Concepts:

  • API Reference: