Getting Started

Backends

Edit this page

Backends are the bridge between the LLM Gateway and your product data. The gateway defines clean interfaces that you implement to connect any data source.

Core Interfaces

ProductBackend

Handles product search, details, and inventory:

1interface ProductBackend {
2 "cmt">// Search products with optional filters
3 searchProducts(
4 query: string,
5 filters?: ProductFilters,
6 options?: { limit?: number; offset?: number }
7 ): Promise<ProductSearchResult>;
8
9 "cmt">// Get detailed product info by ID or slug
10 getProductDetails(productId: string): Promise<Product | null>;
11
12 "cmt">// Check inventory for multiple products
13 checkInventory(productIds: string[]): Promise<InventoryStatus[]>;
14
15 "cmt">// Get product recommendations
16 getRecommendations(context: {
17 productIds?: string[];
18 userId?: string;
19 strategy?: 'similar' | 'complementary' | 'trending' | 'personalized';
20 }): Promise<Recommendation[]>;
21}

CartBackend

Handles shopping cart operations:

1interface CartBackend {
2 "cmt">// Create a new cart
3 createCart(sessionId: string): Promise<Cart>;
4
5 "cmt">// Get existing cart
6 getCart(cartId: string): Promise<Cart | null>;
7
8 "cmt">// Add item to cart
9 addToCart(
10 cartId: string,
11 input: AddToCartInput
12 ): Promise<{ cart: Cart; addedItem: CartItem }>;
13
14 "cmt">// Update item quantity
15 updateCartItem(
16 cartId: string,
17 itemId: string,
18 updates: { quantity?: number }
19 ): Promise<Cart>;
20
21 "cmt">// Remove item from cart
22 removeFromCart(cartId: string, itemId: string): Promise<Cart>;
23
24 "cmt">// Clear all items
25 clearCart(cartId: string): Promise<void>;
26}

OrderBackend

Handles order creation and management:

1interface OrderBackend {
2 "cmt">// Create order from cart
3 createOrder(
4 cart: Cart,
5 shippingAddress: ShippingAddress,
6 paymentInfo?: PaymentInfo
7 ): Promise<Order>;
8
9 "cmt">// Get order by ID
10 getOrder(orderId: string): Promise<Order | null>;
11
12 "cmt">// Update order status
13 updateOrderStatus(
14 orderId: string,
15 status: Order['status']
16 ): Promise<Order>;
17}

Type Definitions

Product

1interface Product {
2 id: string;
3 name: string;
4 description?: string;
5 slug?: string;
6 price: {
7 amount: number;
8 currency: string;
9 compareAtPrice?: number;
10 };
11 imageUrl?: string;
12 images?: string[];
13 category?: string;
14 tags?: string[];
15 variants?: ProductVariant[];
16 attributes?: Record<string, string>;
17 inStock?: boolean;
18 rating?: number;
19 reviewCount?: number;
20}

Implementation Examples

In-Memory Backend

Perfect for development and testing:

1const products = [
2 { id: '1', name: 'Product A', price: 29.99 },
3 { id: '2', name: 'Product B', price: 49.99 },
4];
5 
6const backend: ProductBackend = {
7 async searchProducts(query) {
8 const q = query.toLowerCase();
9 const results = products.filter(p =>
10 p.name.toLowerCase().includes(q)
11 );
12 return { products: results, total: results.length, hasMore: false };
13 },
14 "cmt">// ... other methods
15};

Database Backend (Prisma)

Connect to your existing database:

1import { PrismaClient } from '@prisma/client';
2 
3const prisma = new PrismaClient();
4 
5const backend: ProductBackend = {
6 async searchProducts(query, filters, options) {
7 const products = await prisma.product.findMany({
8 where: {
9 OR: [
10 { name: { contains: query, mode: 'insensitive' } },
11 { description: { contains: query, mode: 'insensitive' } },
12 ],
13 ...(filters?.category && { category: filters.category }),
14 ...(filters?.priceMin && { price: { gte: filters.priceMin } }),
15 ...(filters?.priceMax && { price: { lte: filters.priceMax } }),
16 },
17 take: options?.limit ?? 10,
18 skip: options?.offset ?? 0,
19 });
20
21 return {
22 products: products.map(mapProduct),
23 total: products.length,
24 hasMore: products.length === (options?.limit ?? 10),
25 };
26 },
27 "cmt">// ... other methods
28};

Best Practices

Implement Partial Backends

You don't need to implement everything. Start with ProductBackend, add CartBackend when needed.

Use TypeScript

The type definitions catch errors early and provide excellent IDE support.

Handle Errors Gracefully

Return null for not found items rather than throwing. Throw for actual errors.