Skip to main content
The @modrinth/api-client is a flexible, type-safe API client for Modrinth’s APIs (Labrinth, Kyros, and Archon). It works across Node.js, browsers, Nuxt, and Tauri with a feature system for authentication, retries, circuit breaking, and other custom request/response processing.

Installation

Platform Support

The client provides three platform-specific implementations:
  • GenericModrinthClient - Uses ofetch, works in Node.js, browsers, and workers
  • NuxtModrinthClient - Uses Nuxt’s $fetch, SSR-aware, blocks uploads during SSR
  • TauriModrinthClient - Uses @tauri-apps/plugin-http for Tauri desktop apps

Creating a Client Instance

Module Structure

Modules are lazy-loaded and accessed as a nested structure organized by service:

Authentication

The AuthFeature automatically injects authentication tokens into request headers. It supports both static and dynamic tokens.

Configuration Options

  • token - Static string or async function returning a token
  • tokenPrefix - Token prefix (default: 'Bearer')
  • headerName - Custom header name (default: 'Authorization')

Making API Requests

You can make requests using either the module methods or the generic request() method:

Request Options

File Uploads with Progress Tracking

File uploads use XMLHttpRequest for progress tracking (not available via fetch). The upload() method returns an UploadHandle:

Upload Modes

  • Single file - { file: File | Blob } sends with Content-Type: application/octet-stream
  • FormData - { formData: FormData } for multipart uploads
Uploads go through the feature chain (auth, retry, etc.). Features can detect uploads via context.metadata.isUpload.

WebSocket Usage

WebSocket support is available on GenericModrinthClient for real-time communication with Modrinth Hosting servers.

Connecting to a Server

Connection Flow

1

Fetch JWT authentication

The client fetches a JWT token via archon.servers_v0.getWebSocketAuth()
2

Open WebSocket connection

Opens a wss:// connection to the server
3

Authenticate

Sends { event: 'auth', jwt: token } to authenticate
4

Receive auth confirmation

Server responds with { event: 'auth-ok' }
5

Ready for events

Connection is ready to send commands and receive events

Subscribing to Events

Available Events

  • log - Server console output
  • stats - Server resource usage (CPU, memory, etc.)
  • power-state - Server power state changes
  • uptime - Server uptime updates
  • backup-progress - Backup creation progress
  • installation-result - Mod installation results
  • filesystem-ops - File system operation events
  • new-mod - New mod detected
  • auth-expiring - Authentication expiring soon
  • auth-incorrect - Authentication failed
  • auth-ok - Authentication successful

Sending Commands

Auto-Reconnection

The WebSocket client automatically reconnects on unexpected disconnection with exponential backoff:
  • Base delay: 1 second
  • Max delay: 30 seconds
  • Max attempts: 10

TypeScript Types

Types are organized in namespaces that mirror the backend services:
Types match 1:1 with the backend API responses. They are not reshaped or renamed.

Error Handling

The client throws ModrinthApiError for API errors and ModrinthServerError for server errors:

Error Properties

Features (Middleware)

Features wrap requests in a chain, allowing you to modify requests, add retry logic, or short-circuit requests.

Retry Feature

Automatically retries failed requests with configurable backoff:

Circuit Breaker Feature

Prevents cascade failures by opening circuits after repeated failures:
For Nuxt, use NuxtCircuitBreakerStorage which persists state across SSR/CSR boundaries.

Combining Features

Features are executed in order. Common pattern:

Custom Base URLs

Override base URLs for staging environments or custom instances:
You can also use custom URLs directly in requests: