Compactr
OpenAPI SerializationLeverage your existing OpenAPI 3.x schemas to dramatically reduce API response sizes
Quick Start
import { schema } from 'compactr';
const userSchema = schema({
type: 'object',
properties: {
id: { type: 'string', format: 'uuid' },
name: { type: 'string' },
age: { type: 'integer', format: 'int32' },
balance: { type: 'number', format: 'double' },
created: { type: 'string', format: 'date-time' },
tags: { type: 'array', items: { type: 'string' } }
}
});
// Encode data
const encoded = userSchema.write(data);
// Decode data
const decoded = userSchema.read(buffer);
How Does It Compare?
Compactr offers a unique balance of speed and compression efficiency. Best of all, if your API already has a JSON-API schema, you don't need to redefine anything. It will just work!*
| Feature | JSON | Compactr | MessagePack | Protobuf |
|---|---|---|---|---|
| Schema Definition | โ | OpenAPI 3.x | โ | Custom .proto |
| Compression Ratio (Realistic scenario) | 277 bytes | 78 bytes (~3.5x) | 227 bytes (~1.2x) | 129 bytes (~2.1x) |
| Bundle Size | Native | ~10KB | ~10KB | ~20KB |
| API-Optimized Types | โ | UUID, IPv4/6, DateTime, Binary | Limited | โ |