Compactr

Compactr

OpenAPI Serialization

Leverage 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 โœ—
*Compactr support Swagger/OpenAPI 3.x schema definitions and features, such as `oneOf`, `anyOf` and local references `$ref`. All definitions must be provided in JS object format, compactr will not parse YAML.