Basic Encoding
The simplest way to use AXON is with the encode() and decode() functions:
import { encode, decode } from '@axon-format/core';
// Your data
const data = {
users: [
{ id: 1, name: "Alice", active: true },
{ id: 2, name: "Bob", active: true },
{ id: 3, name: "Charlie", active: false }
]
};
// Encode to AXON
const axonString = encode(data);
console.log(axonString);
// Output: users::[3] active:bool|id:u8|name:str
// true|1|Alice
// true|2|Bob
// false|3|Charlie
// Decode back to original
const decoded = decode(axonString);
// Perfect round-trip! ✅ With Schema Validation
Add type validation to catch errors before sending data to expensive LLM APIs:
import { encode, decode, registerSchema, type Schema } from '@axon-format/core';
// Define schema as a plain object
const userSchema: Schema = {
name: 'User',
fields: [
{ name: 'id', type: 'u8' },
{ name: 'name', type: 'str' },
{ name: 'email', type: 'str' },
{ name: 'verified', type: 'bool' }
]
};
// Register the schema globally (optional)
registerSchema(userSchema);
const userData = {
id: 1,
name: "Alice",
email: "alice@example.com",
verified: true
};
// Encode with validation
const encoded = encode(userData, { schemas: [userSchema] });
// Type-safe decode
const decoded = decode(encoded, { schemas: [userSchema] }); Real-World Example: LLM API Call
Here's how to use AXON in a real LLM API call to save tokens:
import { encode } from '@axon-format/core';
// Your database query results
const results = {
products: [
{ id: 1, name: "Widget", price: 19.99, stock: 50 },
{ id: 2, name: "Gadget", price: 29.99, stock: 30 },
// ... more products
]
};
// Encode before sending to LLM
const axonData = encode(results);
// Send to your LLM (60-95% smaller!)
const response = await fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + apiKey
},
body: JSON.stringify({
model: 'gpt-4o',
messages: [
{
role: 'user',
content: "Here's the product data in AXON format: " + axonData
}
]
})
}); Pro Tip: AXON automatically selects the best encoding mode and compression algorithms. You don't need to configure anything for basic usage!
CLI Usage
You can also use AXON from the command line:
# Convert JSON to AXON
axon encode data.json -o data.axon
# See token savings
axon stats data.json data.axon
# Convert back to JSON
axon decode data.axon -o output.json Next Steps
- Learn about the Type System - Understand AXON's 13 validated types
- Explore Compression - See how the 5 algorithms work
- Use Schemas - Add validation to your data