A flexible, type-safe API client for Modrinth's APIs (Labrinth, Kyros & Archon). Works across Node.js, browsers, Nuxt, and Tauri with a feature system for authentication, retries, circuit breaking and other custom processing of requests and responses.
## Installation
```bash
pnpm add @modrinth/api-client
# or
npm install @modrinth/api-client
# or
yarn add @modrinth/api-client
```
## Usage
### Plain JavaScript/Node.js
```typescript
import { GenericModrinthClient, AuthFeature, ProjectV2 } from '@modrinth/api-client'
You can also use custom URLs directly in requests:
```typescript
// One-off custom URL (useful for Kyros nodes or dynamic endpoints)
await client.request('/some-endpoint', {
api: 'https://eu-lim16.nodes.modrinth.com/',
version: 0,
})
```
## Features
### Authentication
Supports both static and dynamic tokens:
```typescript
// Static token
new AuthFeature({ token: 'mrp_...' })
// Dynamic token (e.g., from auth state)
const auth = await useAuth()
new AuthFeature({
token: async () => auth.value.token,
})
```
### Retry
Automatically retries failed requests with configurable backoff:
```typescript
new RetryFeature({
maxAttempts: 3,
backoffStrategy: 'exponential',
initialDelay: 1000,
maxDelay: 15000,
})
```
### Circuit Breaker
Prevents cascade failures by opening circuits after repeated failures:
```typescript
new CircuitBreakerFeature({
maxFailures: 3,
resetTimeout: 30000,
failureStatusCodes: [500, 502, 503, 504],
})
```
## Documentation
This package is **self-documenting** through TypeScript types and JSDoc comments. Use your IDE's IntelliSense to explore available methods, classes, and configuration options.
For Modrinth API endpoints and routes, refer to the [Modrinth API Documentation](https://docs.modrinth.com).
## Contributing
- Modules are available in the `modules/<api>/...` folders.
- When a module has different versions available, you should do it like so: `modules/labrinth/projects/v2.ts` etc.
- Types for a module's requests should be made available in `modules/<api>/module/types.ts` or `.../types/v2.ts`.
- You should expose these types in the `modules/types.ts` file.
- When creating a new module, add it to the `modules/index.ts`'s `MODULE_REGISTRY` for it to become available in the api client class.
Dont forget to run `pnpm fix` before committing.
## License
Licensed under GPL-3.0 - see the [LICENSE](LICENSE) file for details.