# Building REST API v2: Architecture Decisions Behind the Redesign

> A technical look at why we rewrote our API, the patterns we chose, and the trade-offs we made along the way.

- Source: https://ermlib.com/en/updates/building-rest-api-v2
- Publisher: Horato & Co Ltd
- Published: 2024-05-18
- Category: Technical Deep Dives
- Tags: API, REST, architecture, developer-experience, technical

## Why a v2

The ERMLib v1 API worked. It covered the core resources -- records, patrons, circulation transactions -- and early integrators built useful things with it. But as usage grew, the limitations became clear.

Pagination was inconsistent. Error responses varied across endpoints. Filtering used a custom query syntax that nobody enjoyed learning. Batch operations were missing entirely. We could have patched v1, but the debt was structural. A clean redesign was the right call.

## Design Principles

We established four principles before writing a single endpoint:

**Consistency over cleverness.** Every endpoint follows the same patterns for pagination, filtering, sorting, and error handling. Learn one, and you know them all.

**Predictable responses.** Every successful response wraps data in the same envelope structure. Every error includes a machine-readable code, a human-readable message, and a documentation link.

**Batch-friendly.** Real integrations process hundreds or thousands of records. The API supports bulk create, update, and delete operations natively, not as afterthoughts.

**Backward compatible within v2.** We use additive changes only. New fields may appear, but existing fields will not be removed or renamed without a major version bump.

## Key Technical Decisions

### JSON:API Specification

We adopted JSON:API as our response format. It handles relationships, pagination, sparse fieldsets, and includes in a standardized way. This means fewer decisions for us and fewer surprises for integrators who have used JSON:API elsewhere.

### Cursor-Based Pagination

Offset pagination breaks at scale. When a catalog has 500,000 records, requesting page 10,000 is slow and unstable if records are being added or deleted. Cursor-based pagination solves both problems. Each response includes a cursor pointing to the next page, and results remain stable regardless of concurrent modifications.

### Field-Level Filtering

The v2 API supports filtering on any indexed field using a consistent syntax:

```
GET /api/v2/records?filter[author]=Borges&filter[date][gte]=2020-01-01
```

Nested filters, comparison operators, and logical grouping are all supported. The syntax is documented exhaustively, with examples for every resource.

### Rate Limiting and Authentication

API access uses OAuth 2.0 with scoped tokens. Each token specifies which resources and operations it can access. Rate limits are per-token, with configurable tiers for different integration profiles.

### Webhooks

Polling is wasteful. The v2 API introduces webhooks for real-time notifications. Subscribe to events like `record.created`, `patron.updated`, or `circulation.checkout`, and ERMLib will POST to your endpoint within seconds.

## Developer Experience

We built a Swagger-based interactive API explorer into the ERMLib admin panel. Developers can test endpoints, see real responses, and generate client code without leaving the browser.

Documentation lives at `docs.ermlib.com/api/v2` and is versioned alongside the API itself.

## Migration from v1

The v1 API will remain available through the end of 2024. We are providing a migration guide with endpoint-by-endpoint mappings and a compatibility shim for common patterns. If you are on v1 today, start planning your migration -- v2 is where all new features will land.
