const SnakeQuery = require('snake-query');
const { SchemaBuilder } = SnakeQuery;
async function structuredQuery() {
const client = new SnakeQuery('your-api-key-here');
const products = [
{ name: 'MacBook Pro', price: 2399, category: 'electronics', rating: 4.8 },
{ name: 'iPad Air', price: 599, category: 'electronics', rating: 4.5 },
{ name: 'AirPods', price: 179, category: 'electronics', rating: 4.3 }
];
// Define response structure
const productSchema = SchemaBuilder.create()
.array(
SchemaBuilder.create()
.object()
.addStringProperty('productName')
.addNumberProperty('price', { minimum: 0 })
.addNumberProperty('rating', { minimum: 0, maximum: 5 })
.required(['productName', 'price'])
.build()
)
.build();
try {
const result = await client.query({
query: 'Show all products with their names, prices, and ratings',
data: products,
responseSchema: productSchema
});
// Result is guaranteed to match schema
console.log('Structured results:', result.response);
} catch (error) {
console.error('Error:', error.message);
}
}
structuredQuery();