@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
- npm
- pnpm
- yarn
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-httpfor Tauri desktop apps
Creating a Client Instance
- Generic (Node.js/Browser)
- Nuxt
- Tauri
Module Structure
Modules are lazy-loaded and accessed as a nested structure organized by service:Authentication
TheAuthFeature automatically injects authentication tokens into request headers. It supports both static and dynamic tokens.
- Static Token
- Dynamic Token
Configuration Options
token- Static string or async function returning a tokentokenPrefix- Token prefix (default:'Bearer')headerName- Custom header name (default:'Authorization')
Making API Requests
You can make requests using either the module methods or the genericrequest() method:
- Module Methods
- Generic Request
Request Options
File Uploads with Progress Tracking
File uploads useXMLHttpRequest for progress tracking (not available via fetch). The upload() method returns an UploadHandle:
- Server File Upload
- Version Creation
Upload Modes
- Single file -
{ file: File | Blob }sends withContent-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 onGenericModrinthClient 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 server3
Authenticate
Sends
{ event: 'auth', jwt: token } to authenticate4
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 outputstats- Server resource usage (CPU, memory, etc.)power-state- Server power state changesuptime- Server uptime updatesbackup-progress- Backup creation progressinstallation-result- Mod installation resultsfilesystem-ops- File system operation eventsnew-mod- New mod detectedauth-expiring- Authentication expiring soonauth-incorrect- Authentication failedauth-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 throwsModrinthApiError 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.