Documentation
Everything you need to integrate TerseJSON into your application.
Getting Started
Client Setup
Installation
Install TerseJSON from npm:
npm install tersejsonQuick Start
1. Add the Express middleware
import express from 'express'
import { terse } from 'tersejson/express'
const app = express()
// Add TerseJSON middleware
app.use(terse())
app.get('/api/users', (req, res) => {
res.json([
{ firstName: 'John', lastName: 'Doe' },
{ firstName: 'Jane', lastName: 'Smith' },
])
})
app.listen(3000)2. Use the client wrapper
import { fetch } from 'tersejson/client'
// Drop-in replacement for fetch
const users = await fetch('/api/users').then(r => r.json())
// Access data normally
console.log(users[0].firstName) // "John"How It Works
TerseJSON compresses repetitive JSON keys in API responses:
- Server middleware intercepts JSON responses
- Arrays of objects have their keys compressed to short aliases
- A key mapping is sent with the response
- Client wrapper transparently expands data using JavaScript Proxies
- Your code continues to access original key names
This process is completely transparent - no changes needed to your business logic.
Configuration Options
app.use(terse({
// Only compress arrays with 2+ items (default: 2)
minArrayLength: 2,
// Only compress keys with 3+ characters (default: 3)
minKeyLength: 3,
// Max nesting depth to traverse (default: 10)
maxDepth: 10,
// Key pattern: 'alpha' | 'numeric' | 'short' | 'prefixed'
keyPattern: 'alpha',
// Skip compression for specific routes
shouldCompress: (req) => !req.path.includes('/graphql'),
// Enable analytics
analytics: {
apiKey: 'your-api-key',
projectId: 'my-api',
reportToCloud: true
}
}))For the complete API reference, visit the GitHub repository.